function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Allen ManamelAllen Manamel 

How to write test cases to cover both if and else condition

I have the following code in my constructor method in apex class.

public Page4Controller() {

  User currentUser = [SELECT contactId  FROM User WHERE Id = :UserInfo.getUserId()];
        System.debug('Current user' + currentUser);
         //Fetch the current contact object
       if(String.isNotBlank(currentUser.ContactId)) {

            contact  = [
                SELECT  AccountId, firstName, lastName, MiddleName,  Nickname__c, Suffix, MailingStreet, MailingCity,
                MailingStateCode, Certify_Complete_and_Correct__c,  Certify_Download_Instructions_to_PreCan__c
                FROM Contact
                WHERE Id = :currentUser.ContactId
                LIMIT 1
            ];

            System.debug('contact information: ' + contact );

             //Fetch the current application object  
           application = [
                        
                        SELECT Id, Contact__c, Attend_College_after_High_School__c
                        FROM Application__c 
                        WHERE Contact__c =: currentUser.contactId
                        LIMIT 1];
            
                        
              }
}
And the following submit method

public pagereference submit() {
        
        if (
             contact.Certify_Complete_and_Correct__c == false ||
             contact.Certify_Download_Instructions_to_PreCan__c == false
                      
           ) 
        
              {
                 ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Please make sure all required fields are populated.');
                 ApexPages.addMessage(myMsg);
                 return null;
              }
        
       
                
        // ALO Code
        
         
         if(application.Attend_College_after_High_School__c == 'No')
         
            {
            
                acc = [
                        
                        SELECT Id, OwnerId
                        FROM Account
                        WHERE Id =: contact.AccountId
                        ];
                        
                
                contact.ALO__c = acc.OwnerId;
            
                        
              }
              
              
              
          else if(application.Attend_College_after_High_School__c == 'Yes')
          
              {
          
                  
                                 
                  ziptoLOD = [
                        
                        SELECT Id, Name, LOD__c
                        FROM Zip_3_to_LOD__c
                        WHERE Name =: contact.Extract_Zipcode__c
                        
                        ];
                        
                
                        
                  contact.ALO__c = ziptoLOD.LOD__c;
          
                      
             }
              
I don't know how to write test cases for the above conditions so that it covers both the if and else code.

Here is the test class I am working on

static testMethod void submit_Test() {

    Profile AdminProfile = [select id from profile where name='System Administrator']; 
    UserRole AdminRole = [SELECT id from UserRole where name = 'Admin'];


    User currentuser = new User();
      currentuser.firstname      ='James';
      currentuser.lastname      ='Dobb';
      currentuser.email         ='abcdefg@gmail.com';
      currentuser.username      ='abcdefg@gmail.com';
      currentuser.alias        ='jdobb';
      currentuser.TimeZoneSidKey  ='GMT';
      currentuser.LocaleSidKey    ='en_US';
      currentuser.emailencodingkey  ='UTF-8';
      currentuser.languagelocalekey  ='en_US';
      currentuser.ProfileId     = AdminProfile.Id;
      currentuser.userroleid     = AdminRole.Id;
      insert currentuser;
      
    
     Account acc= new Account();
     acc.OwnerId='005r0000001i9xA';
     acc.Name='Atlanta Area Tech School';
     acc.RecordTypeId='012r00000005DsA';
     
     
     Zip_3_to_LOD__c zip= new Zip_3_to_LOD__c();
     zip.Name = '480';
     zip.LOD__c = '005t0000001LDpi';
    
    Contact contact   = new Contact(); 
    contact.firstName = 'Alex';
    contact.lastName  = 'Cauller';
    contact.email     = 'alexcauller@gmail.com';
    contact.AccountId = acc.Id;
    
    
        
    system.runas(currentuser){
      insert acc;
      insert contact;
      insert zip;

      
    }
    
    
      
    Application__c app= new Application__c();
    app.Contact__c = contact.Id;
    app.Attend_College_after_High_School__c= 'No';
    
    
    system.runas(currentuser){
      
      insert app;
    }

    Profile pr = [select id from profile where name='......']; 
    User u = new User();
      u.firstname      ='James';
      u.lastname      ='Dobb';
      u.email         ='xyz@gmail.com';
      u.username      ='xyz@gmail.com';
      u.alias        ='jdobb';
      u.TimeZoneSidKey  ='GMT';
      u.LocaleSidKey    ='en_US';
      u.emailencodingkey  ='UTF-8';
      u.languagelocalekey  ='en_US';
      u.ProfileId     = pr.Id;
      u.contactId      =contact.id;
    insert u;

    system.runAs(u){
      System.Test.startTest();
      Page4Controller controllerClass = new Page4Controller();
      controllerClass.contact.Certify_Complete_and_Correct__c = false;
      controllerClass.contact.Certify_Download_Instructions_to_PreCan__c = false;
      controllerClass.submit();
      System.AssertEquals(controllerClass.contact.ALO__c,acc.OwnerId);
      controllerClass.application.Attend_College_after_High_School__c = 'Yes';
      controllerClass.submit();
      System.AssertEquals(controllerClass.contact.ALO__c,zip.LOD__c);
      controllerClass.previouspage();
      System.Test.stopTest();
      
           
    }
    
    
    When I run the test case, the test case fails and generates the following error on both the assert statements

System.AssertException: Assertion Failed: Expected: null, Actual: 005r0000001i9xAAAQ

    
    
    

    
  }
NagendraNagendra (Salesforce Developers) 
Hi Anjali,

May I suggest you please go through the below trailhead module on how to write the test classes and its best practices.Once you are done with the above module you will be able to understand on how to write the test classes. Then please let us know if you are stuck somewhere in the middle so that we can look into it and can help you accordingly.

Happy to help further.

Thanks,
Nagendra