+ Start a Discussion
Semira@gmail.comSemira@gmail.com 

Test coverage 47% in my test class - Map Statement

Hi all, 

I'm having trouble writing unit test to cover my Conditional statement. Especially checking the map. 
 
Set<id> office = new set<id>();

for(Opportunity opp: OppMap.values())
		{
			if(opp.Office2__c == null)
			{
				continue;
			}
            else
            {
                OfficeID.add(opp.Office2__c);
            }
		}
I'm having trouble for checking the Office__c == null statement and the setID. 

Also, 
Opportunity opp = OppMap.get(suv.Opportunity__c);
         	if(opp == null)
         	{
         		continue; 
         	}
         	else
            {
            	 //field update from Opportunity to Customer Survey
		         
            	//check if the id is found
                office__c off = offices.get(OppMap.get(suvey.Opportunity__c).Office2__c);

                if(off == null)
                {
                    continue;
                }
                else 
                {
                    //field update from Office__c to Customer Survey. 
                }

My unit test is not going through the conditional statement. 
 
public static TestMethod void TestforNullOFfice()
    {
        Office__c office = addOffice();  
                
        Opportunity opp = AddOpportunity(office);
        
        Customer_Survey__c survey = runTestOnCustomerSurvey(opp);
        
        
        List<Customer_Survey__c> surveys = new List<Customer_Survey__c>();
        surveys.add(survey);
        
       //calls the main class to run the rest. 
        CustomerSurveyFieldUpdate.CustomerSurveyFieldUpdate(surveys);
    }
public static Customer_Survey__c runTestOnCustomerSurvey(opp)
{
        //created the customer survey with opp.
}

public static Opportunity AddOpportunity(office)
{
    Opportunity opp = new opportunity();
    opp.office2__c = office;
    
   insert opp;
   
  return opp;
}

public static Office__c addOffice() 
{
    Office__c office = new Office__c();

    //assigns roles
     office.branch_manager = 'some user.id'; 
    
   insert office;
   
  return office;
}

What exactly am I missing? Please any pointer would help. I'm not sure how exactly go around checking the map. Also, for future reference, is there a way to check the if and else condition to write better unit test, please kindly share your tips. 

Thank you. 



 
Best Answer chosen by Semira@gmail.com
R Z KhanR Z Khan
Hi Semira,

Create another test emthod, where you don't call AddOpportunity(). but instead insert an empty opportunity and call your main method with that opportunity with no set fields. It will go into your conditions then. 
Also when you call  CustomerSurveyFieldUpdate.CustomerSurveyFieldUpdate(). populate the lsit of surveys with 1 survey where opportunity is set to null. That should give you 100% coverage. 

Please rememebr to add assert sttements to make sure that your code works as expected. Getting 100% coevrage is not enough. 

All Answers

R Z KhanR Z Khan
Hi Semira,

Create another test emthod, where you don't call AddOpportunity(). but instead insert an empty opportunity and call your main method with that opportunity with no set fields. It will go into your conditions then. 
Also when you call  CustomerSurveyFieldUpdate.CustomerSurveyFieldUpdate(). populate the lsit of surveys with 1 survey where opportunity is set to null. That should give you 100% coverage. 

Please rememebr to add assert sttements to make sure that your code works as expected. Getting 100% coevrage is not enough. 
This was selected as the best answer
Semira@gmail.comSemira@gmail.com
Hi RZ

I have tried writing 3 different testmethod and assert statement. However, the condition, when Office is not null, that else statement is not getting covered at all! 
R Z KhanR Z Khan
can you post your updated code?
R Z KhanR Z Khan
Where is the code tha toyu use to populate the offices lsit? Can you put the map and OppMap in the debug adn see if its in fact populated correctly?
Semira@gmail.comSemira@gmail.com
Hi, 

I realized that I forgot to insert my opportunity or office. Which is why it was reading null value. After I inserted the opportunity, I got 100 coverage with the 3 different methods to check my conditional statement. 

Thank you for all your help!