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
DannyK89DannyK89 

Test Methods

I have been trying to understand test methods but I can't seem to grasp them.  Here is the controller that I have if anyone could give me some tips or explain how test methods work that would be great thanks. 

 

Controller that needs a test method:

 

 

public class SearchController {
    
    public String Searchtxt {get; set;}
    public List<refWrapper> refWraps {get; set;}
    List<Need__c> needlist = [SELECT Name, Id, Requirement_Number__c, Bounty__c, Bounty_Other__c, Bounty_Rules__c FROM Need__c ORDER BY Name];
    public Referral__c referral{
    get{if(referral == null) referral = new Referral__c();
    referral.Partner__c = ApexPages.currentPage().getParameters().get('id');
    return referral;} set;}
    
    public PageReference SearchId() {
        needlist = [SELECT Name, Id, Requirement_Number__c, Bounty__c, Bounty_Other__c, Bounty_Rules__c FROM Need__c WHERE Requirement_Number__c =:Searchtxt];
        if(Searchtxt == '' || Searchtxt == null) needlist = [SELECT Name, Id, Requirement_Number__c, Bounty__c, Bounty_Other__c, Bounty_Rules__c FROM Need__c ORDER BY Name];
            refWraps = new List<refWrapper>();
            for(Need__c N : needList){
               refWrapper recWrap = new refWrapper();
               recWrap.need=n;
               recWrap.checked = false;
               refWraps.add(recWrap);
            }
        return null;
    }
    public SearchController(){
         refWraps = new List<refWrapper>();
           for (Need__c n : needList)
           {
               refWrapper recWrap = new refWrapper();
               recWrap.need=n;
               recWrap.checked = false;
               refWraps.add(recWrap);
           }
    }
        public PageReference ReferralSelect() {
            
            for(refWrapper ref : refWraps){
                if(ref.checked){
                    referral.Need__c = ref.need.id;
                }
            }
            insert referral;
            PageReference tosecondpage = Page.leadstep1;
            tosecondpage.getParameters().put('Id',referral.Id);
            tosecondpage.setRedirect(true);
            return tosecondpage;
    }
    
    
    public class refWrapper{
        public Referral__c ref {get; set;}
        public Need__c need {get; set;}
        public Boolean checked {get; set;}
    }
    
    public PageReference Logout(){
        return Page.LeadRegistration;
    }
    
    
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
DannyK89DannyK89

Well it took longer then I thought it would be I got a test method that has 100% code coverage. If you can see any other way to optimize it then let me know otherwise here it is. 

 

Test Method:

 

 

/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class SearchTestController {

    static testMethod void SearchControllerTest() {
        // TO DO: implement unit test
        
        SearchController controller = new SearchController();
        controller.SearchId();
        controller.Searchtxt = 'IL11001';
        controller.SearchId();
        controller.refWraps[0].checked = true;
        controller.ReferralSelect();
        controller.Logout();
    }
}

 

 

All Answers

saraha@groupesci.comsaraha@groupesci.com

Have you read the appropriate sections in the Apex Developer Guide?

DannyK89DannyK89

I have but from what I have seen in the Developer Guide they use simple methods and mine are a bit more complex. Would there be anything that I would have to take into account when I am creating my own test method?

saraha@groupesci.comsaraha@groupesci.com

Not necessarily, you will probably have more complex data to setup.

 

As it says in your sig line, the more you code the more you know, so just start and see what happens :)

 

DannyK89DannyK89

Well it took longer then I thought it would be I got a test method that has 100% code coverage. If you can see any other way to optimize it then let me know otherwise here it is. 

 

Test Method:

 

 

/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class SearchTestController {

    static testMethod void SearchControllerTest() {
        // TO DO: implement unit test
        
        SearchController controller = new SearchController();
        controller.SearchId();
        controller.Searchtxt = 'IL11001';
        controller.SearchId();
        controller.refWraps[0].checked = true;
        controller.ReferralSelect();
        controller.Logout();
    }
}

 

 

This was selected as the best answer
saraha@groupesci.comsaraha@groupesci.com

Your test method needs to also verify that your code is working as expected. For this you will need some System.assert methods. For example verify that ReferralSelect actually inserts the referral record and returns a properly defined PageReference object.

DannyK89DannyK89

How about this one.

 

Test Method:

 

 

/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class SearchTestController {

    static testMethod void SearchControllerTest() {
        // TO DO: implement unit test
        
        SearchController controller = new SearchController();
        controller.SearchId();
        controller.Searchtxt = 'IL11001';
        controller.SearchId();
        controller.refWraps[0].checked = true;
        controller.ReferralSelect();
        controller.Logout();
        Referral__c[] referral = [SELECT NAME, ID, Need__c FROM Referral__c
        							WHERE Need__c =: 'a0hQ0000000vlxF'];
        System.assertEquals('a0hQ0000000vlxF', referral[0].Need__c);
    }
}

 

 

saraha@groupesci.comsaraha@groupesci.com

Yes, like that, but you should do more - to cover all the expected functionality.