+ Start a Discussion
MayeUPAEPMayeUPAEP 

Test Method Problem - If clauses

 

I need Help!!!! I have some problems with my Test Method  

 

 

  • My VF Page: I have several fields which i need to create them as "required" using "Error messages".  Here is an example:

 

 

 

<apex:pageBlockSectionItem id="BSI_Status">
 <apex:outputLabel value="Estatus" for="estatuspicklist" />
 <apex:outputPanel styleClass="requiredInput" layout="block">  
  <apex:outputPanel styleClass="requiredBlock" layout="block"/>
  <apex:selectList value="{!caso.Status}" multiselect="false" size="1" id="estatuspicklist">
    <apex:selectOption itemValue="" itemLabel="" />
      <apex:selectOption itemValue="Abierto" itemLabel="Abierto" />
      <apex:selectOption itemValue="Cerrado" itemLabel="Cerrado" />
      <apex:selectOption itemValue="Cancelado por error" itemLabel="Cancelado por error" />
    </apex:selectList>
  <apex:outputPanel styleClass="errorMsg" layout="block" rendered="{!errorEstatus}">
    <strong>Error: </strong>{!errorMessage}</apex:outputPanel>
  </apex:outputPanel>
</apex:pageBlockSectionItem>

 

  • My Controller:  When the user clic on "Save" button, I make a validation on the class, if the field is empty, the error is activated, if not the record is saved.
	public PageReference guardar()
		{
		if(caso.Status == '' || caso.Status == null) errorEstatus = true;  else errorEstatus = false; 
		if(caso.SUI_Departamento__c != 'Otro' && (servicio == '' || servicio == null || servicio == ID_OTRO)) errorServicio = true;  else  errorServicio = false;
		if(caso.SUI_Departamento__c == 'Otro' && (caso.SUI_Area__c == '' || caso.SUI_Area__c == null)) errorArea = true; else  errorArea = false;
		if(caso.SUI_Departamento__c == 'Otro' && (caso.SUI_OtroServicio__c == '' || caso.SUI_OtroServicio__c == null)) errorDesc = true;  else errorDesc = false;  
		if((caso.SUI_QuienSolicita__c == 'Departamento Interno' || caso.SUI_QuienSolicita__c == 'Organismo externo') && (caso.SUI_Solicitante__c == '' || caso.SUI_Solicitante__c == null)) errorSolic = true; else errorSolic = false;
		
		if(errorEstatus == false && errorServicio == false && errorArea == false && errorDesc == false && errorSolic == false)
		{
		try { GuardaCaso(); }
	 	catch (Dmlexception e)
			{
			ApexPages.addMessages(e);	
   		return null;
			}
			String retUrl = Apexpages.currentPage().getParameters().get('retURL');
			if(retUrl=='' || retUrl==null) retUrl='/500/o';
			Pagereference ref = new Pagereference(retUrl);
    	ref.setRedirect(true);
    	return ref;
		}
		else return null; 
	}

 

 

The problem is that no matter what I do in my test, I can't make to execute the "If" lines

 

I Have tried this:

 

 

	  
          
          Case c = new Case(AccountId = ID_CUENTA, ContactId = contacto.Id, Description = 'Prueba', RecordTypeId= '0124000000013PGAAY'); 
				
	  ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(c);
	  CONTROL_CasoTramite controller = new CONTROL_CasoTramite(sc);	
	  Test.setCurrentPage(new PageReference('/apex/CasT'));
	  
	  test.startTest();
	  controller.getCaso();
	  controller.caso.Status = '';
	  controller.caso.SUI_Departamento__c = 'CIAC';
	  controller.servicio = '';
	  controller.guardar();
	  Test.stopTest();
              

 

Even I tried to put an System.Debug to print the values of controller.caso.Status, controller.caso.SUI_Departamento__c and controller.servicio, and they have values but my test doesn't work in the red lines

 

 Please any idea!!!!

 

 

 

Pradeep_NavatarPradeep_Navatar

Try this format of testmethod for your controller :

 

                          @isTest

                          private class Testmethod

                         {

                                static testMethod void CONTROL_CasoTramite()

                                {

                                Case c = new Case(AccountId = ID_CUENTA, ContactId = contacto.Id, Description = 'Prueba', RecordTypeId= '0124000000013PGAAY');

                                insert c;

                                ApexPages.CurrentPage().getParameters().put('id', c.Id);

                                ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(c);

                               CONTROL_CasoTramite controller = new CONTROL_CasoTramite(sc);     

                                PageReference prc = controller.guardar();

                                }

                                static testMethod void CONTROL_CasoTramite()

                                {

                                // You will test your 'if' condition according to your controller and whatever your requirement ,you will give a field for insert a case

                                                object.

 

                                 }

                         }

 

Did this answer your question? if so, please mark it solved.

MayeUPAEPMayeUPAEP

Hi,

Thanks for your help, the problem is about my "If" conditions.

Example: Although I give a value null to caso.Status (case.Status),  I can't get test this line:

 

if(caso.Status == '' || caso.Status == null) errorEstatus = true;  else errorEstatus = false; 

 

any Idea how could I solve it??

Thanks

 

MayeUPAEPMayeUPAEP

In my test I've try this:

 

 

	static testMethod void CasoTramiteGuardar()
   {
   init();
   Case c = new Case(AccountId = ID_CUENTA, ContactId = contacto.Id, Description = 'Prueba', RecordTypeId= '0124000000013PGAAY');
   insert c;
   ApexPages.CurrentPage().getParameters().put('id', c.Id);
   ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(c);
   CONTROL_CasoTramite controller = new CONTROL_CasoTramite(sc);     
   controller.caso.Status = null;
   PageReference prc = controller.guardar();
   }

 

 

 The result is:

 

 

	public PageReference guardar()
		{
		if(caso.Status == '' || caso.Status == null) errorEstatus = true;  else errorEstatus = false; 
		if(caso.SUI_Departamento__c != 'Otro' && (servicio == '' || servicio == null || servicio == ID_OTRO)) errorServicio = true;  else  errorServicio = false;
		if(caso.SUI_Departamento__c == 'Otro' && (caso.SUI_Area__c == '' || caso.SUI_Area__c == null)) errorArea = true; else  errorArea = false;
		if(caso.SUI_Departamento__c == 'Otro' && (caso.SUI_OtroServicio__c == '' || caso.SUI_OtroServicio__c == null)) errorDesc = true;  else errorDesc = false;  
		if((caso.SUI_QuienSolicita__c == 'Departamento Interno' || caso.SUI_QuienSolicita__c == 'Organismo externo') && (caso.SUI_Solicitante__c == '' || caso.SUI_Solicitante__c == null)) errorSolic = true; else errorSolic = false;
		
		if(errorEstatus == false && errorServicio == false && errorArea == false && errorDesc == false && errorSolic == false)
		{
		GuardaCaso();
		String retUrl = Apexpages.currentPage().getParameters().get('retURL');
		if(retUrl=='' || retUrl==null) retUrl='/500/o';
		Pagereference ref = new Pagereference(retUrl);
    ref.setRedirect(true);
    return ref;
		}
		else return null; 
	}