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
ismyhcismyhc 

Having trouble with test coverage

Im new to Apex coding and programing in general. Im having a hard time figureing out the test coverage etc.. Ive been succsefull in getting salesforce to do everything I want, but when it comes to code coverage and testing, I am having a terrible time. Ive looked around at the various documents, but Im just having a hard time grasping how to test what I am doing from the examples Ive seen. Any help would be much appreciated.


Here is the class that gets triggered. It also calls out to another class.

 

public class xOpportunityGrab {
             
    public static void addOpportunityGrab(Opportunity[] opps) {
                 
        for (Opportunity o: opps) {
            if (o.Probability >= 90 && o.campaign_created_in_x__c != true) {
                
                // Set campaign checkbox to true if flase
                o.campaign_created_in_x__c = true;
                
                // Use Opportunity name as campaign name
                String xCampaignName = o.name;
                
                // Use Opportunity owner as campaign salesperson
                String xCampaignSalesperson = String.valueOf(o.owner);
                
                // Campaign start date from custom field
                String xCampaignStartDate = String.valueOf(o.campaign_start_date__c);
                
                // Campaign end date from custom field
                String xCampaignEndDate = String.valueOf(o.campaign_end_date__c);
                
                // Callout to xml api process
                xXmlApiCall.sendRequest(xCampaignName, xCampaignSalesperson, xCampaignStartDate, xCampaignEndDate);
                
            }
        }
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

To create test Method Structure Read This :

http://forceschool.blogspot.com/search/label/Test%20Apex

 

For more examples see 

http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods

 

And here is your test method

try this 

 

@isTest
private class testxOpportunityGrab 
{

  private static xOpportunityGrabTest()
  {
    
      Opportunity opportunity = new Opportunity (name = 'testo',  stagename = 'Renew', closedate = system.today(), Probability = 100 , campaign_created_in_x__c = false , campaign_start_date__c = Date.today() , campaign_end_date__c = Date.today() + 30);
        insert Opportunity;
  
     List<Opportunity> listOpportunity = new List<Opportunity>();
     listOpportunity.add(Opportunity);
     xOpportunityGrab cls = new xOpportunityGrab();
     cls.addOpportunityGrab(listOpportunity);
  }


}

 In case this is a webservice callout 

// Callout to xml api process
 xXmlApiCall.sendRequest(xCampaignName, xCampaignSalesperson, xCampaignStartDate, xCampaignEndDate);

 then change it to

// Callout to xml api process
if(!Test.isRunningTest())
{
                xXmlApiCall.sendRequest(xCampaignName, xCampaignSalesperson, xCampaignStartDate, xCampaignEndDate);
}

 

All Answers

Jake GmerekJake Gmerek

OK to test something like that there are several steps you have to take:

 

1.  Create your data.  In your case create a list of opprotunity records and insert them into the database.  Pseudocode:

 

opprotunity o = new opprotunity(field1='value1', ...);

List<opprotunity> ol = new List<opprotunity>();

for (integer i = 0; i<10; i++){ol.add(o);}

insert ol;

 

2.  Instantiate your class. (i.e. xopprotunityGrab ob = new xopprotunityGrab();)

3.  Now here is the tricky part and may require some careful thought when you execute step 1 above.  Your list of opprotunites that you pass to your method in a second here, has to cover every case in your method.  So for every if statement and every case in your if statements you need to have an opprotunity that you are passing that both satisfies and does not satisfy the requirement, then you can call your method.  (ob.addOpportunityGrab(ol);)

 

This should give you the test coverage that you need, if you are not getting the test coverage that is because you do not have an opprotunity in ol that satisfies (or does not satisfy as the case may be) the condition in the code to be able to execute the uncovered statements.  To say it a little differently, your test cases (ol from above) do not cover all the possible scenarios available in your code.  But even if you have 100% coverage you are not done.

 

4.  Check your results.  At this point you know your code will run, but will it run properly?  That is what you need to find out next using one of three methods, system.assert(), system.assertequals(), and system.notassertequals().  For example in your code you could do:

 

system.assertEquals(o.campaign_created_in_x__c, TRUE); 

 

This will test if the update took place, but before you test that you want the get the data fresh from the database with an soql query.  For example say you had set ol[1].Probability__c to 95 and ol[1].campaign_created_in_x__c to FALSE in step one before you inserted the records.  Now after step 3, ol[1].campaign_created_in_x__c should be true. so you could do the following to check it:

 

Opprotunity oTest = new Opprotunity([select campaign_created_in_x__c from Opprotunity where ID =: ol[1] LIMIT 1]);

system.assertEquals(ol[1].campaign_created_in_x__c, TRUE); 

 

However the way your code is constructed now this will fail.  After your for loop you need to call:

 

update opps; 

 

This will commit the changes you made to the database.  Good luck and let me know if you have more questions.

 

Shashikant SharmaShashikant Sharma

To create test Method Structure Read This :

http://forceschool.blogspot.com/search/label/Test%20Apex

 

For more examples see 

http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods

 

And here is your test method

try this 

 

@isTest
private class testxOpportunityGrab 
{

  private static xOpportunityGrabTest()
  {
    
      Opportunity opportunity = new Opportunity (name = 'testo',  stagename = 'Renew', closedate = system.today(), Probability = 100 , campaign_created_in_x__c = false , campaign_start_date__c = Date.today() , campaign_end_date__c = Date.today() + 30);
        insert Opportunity;
  
     List<Opportunity> listOpportunity = new List<Opportunity>();
     listOpportunity.add(Opportunity);
     xOpportunityGrab cls = new xOpportunityGrab();
     cls.addOpportunityGrab(listOpportunity);
  }


}

 In case this is a webservice callout 

// Callout to xml api process
 xXmlApiCall.sendRequest(xCampaignName, xCampaignSalesperson, xCampaignStartDate, xCampaignEndDate);

 then change it to

// Callout to xml api process
if(!Test.isRunningTest())
{
                xXmlApiCall.sendRequest(xCampaignName, xCampaignSalesperson, xCampaignStartDate, xCampaignEndDate);
}

 

This was selected as the best answer
ismyhcismyhc

Thanks to both of you guys that responded. I worked through your examples and was able to get the coverage I needed.

 

Shashikant SharmaShashikant Sharma

Your Welcome Mate