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
Mehul pMehul p 

How to increase the code coverage for the this trigger?

Hi guys,
       My code coverage is at 77% at this time and I need to increase it or try to get it 100%. Here' s the red section which is not covered. 

User-added image

and here 's the test code.
 
Account acc = new Account();
		acc.Name ='Test';
		insert acc;
		
		Contact cont = new Contact();
		cont.FirstName ='Test';
		cont.LastName ='testContact';
        cont.Last_Activity_Subject__c='Run Test Trigger';
		cont.accountid =acc.id;
        cont.Last_Activity_Date__c = Date.parse('6/15/2019');
        
		insert cont;
		
	
		Task u = new Task();
        	u.ownerId = UserInfo.getUserId();
			u.whatid = acc.id;
			u.whoid = cont.id;
			u.Subject='Run Test Trigger';
			u.Status='Not Started';
			u.Priority='Normal';
            u.ActivityDate = Date.parse('6/15/2019');
        	
        insert u;
		
	
		Test.startTest();
		
			update u;
            for (Task t : [SELECT Id, Subject, Status FROM Task WHERE Id = :cont.Id]) {
   
       		 	System.assertEquals('Run Test Trigger',t.Subject); // asserts that your test worked properly
        		System.assertEquals('Not Started', t.Status); // asserts that your test worked properly
   				 }
        
            for (Contact u1 : [SELECT Id, Last_Activity_Subject__c, Last_Activity_Date__c FROM Contact WHERE Id = :cont.Id]) {
                
       		 	System.assertEquals('Run Test Trigger', u1.Last_Activity_Subject__c); // asserts that your test worked properly
        		
   				 }
        	
		Test.stopTest();

How do I increase the code coverage from here? What am I missing the test class?
Best Answer chosen by Mehul p
Anant KamatAnant Kamat
Update the if condition in the class as below 
 
if(t.ActivityDate >= l.Last_Activity_Date__c)

Also update the query for the assert statement for Task as below

for (Task t : [SELECT Id, Subject, Status FROM Task WHERE whoId = :cont.Id])

Let me know if it working and covering the lines highlighted in red.
 

All Answers

Anant KamatAnant Kamat
Update the if condition in the class as below 
 
if(t.ActivityDate >= l.Last_Activity_Date__c)

Also update the query for the assert statement for Task as below

for (Task t : [SELECT Id, Subject, Status FROM Task WHERE whoId = :cont.Id])

Let me know if it working and covering the lines highlighted in red.
 
This was selected as the best answer
Anant KamatAnant Kamat
Did you make the change in the if block as well since I could see that the API names are different. Just want to make sure.
Anant KamatAnant Kamat
The Overall %age value of 54 is for your entire organization. Since you have lot of classes with 0% coverage, it is affecting your overall coverage. try adding more test classes to cover the specific class with 0% coverage and it would improve that value as well.
Mehul pMehul p
Do I have to improve other test classes? Can I just go ahead and deploy this trigger? 

Thanks again!
Anant KamatAnant Kamat
Since your overall is 54%, you will have to deploy it by running selective tests instead of running all the tests. In the backend start writing test classes for classes having 0% coverage and also update existing ones to improve the coverage.