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
uzairuzair 

Test method is not working fine.

Hi All,

 

I have a trigger for which I have written testmethod. The code coverage of trigger is not increasing. I'm New to this thing.

 

The following is the trigger code for which I wrote Test method.

 

datetime myDate =  datetime.newInstance(2009, 07, 02);
      if(trigger.new[i].Field1__c ==   'No' && trigger.new[i].CreatedDate > myDate){
        if(trigger.new[i].Field2__c != trigger.new[i].Field3__c){
          trigger.new[i].adderror('The Field two and three should be equal');
        }
        
      }

 

The test method which I wrote is :

 

public static testMethod void OppBeforeUpdate1(){
    Opportunity newOpp = new Opportunity();
    newOpp = setupOpp(); // In this method I have Inserted an Opportunity, Account, Contact.
    newOpp.Field1__c = 'No';
    newOpp.Field2__c = 'Gas';
    newOpp.Field3__c = 'Electricity';
    update newOpp; // Updating the Opportunity.
}

 

 

When I'm Clicking "Run Test" on the Classes folder.

 

Under the trigger a warning message is displaying like "Line 30 , Column 30 Not Covered"

 

Any help regarding this will be appreciated.

 

 

Thanks in Advance.

Best Answer chosen by Admin (Salesforce Developers) 
sales4cesales4ce

Line 30 of your trigger is not covered when you are trying to update the opportunity.

 

for example:

if it is a condition check like

If

{

stmts;

}

 

else

{

 stmts;

}

 

your test case(s) should handle both "If" and "Else" conditions. make sure you provide 2 inputs to suffice code coverage.

 

Hope it helps!

 

Sales4ce

All Answers

sales4cesales4ce

Line 30 of your trigger is not covered when you are trying to update the opportunity.

 

for example:

if it is a condition check like

If

{

stmts;

}

 

else

{

 stmts;

}

 

your test case(s) should handle both "If" and "Else" conditions. make sure you provide 2 inputs to suffice code coverage.

 

Hope it helps!

 

Sales4ce

This was selected as the best answer
ngabraningabrani

It is not clear from the code which is line 30.

 

You may set different values to field1, field3 and field2 of newOpp, and try to upsert newOpp multiple times to simulate different scenarios.

 

Also you may want to add assert statements to make sure that the trigger is working as designed.