1

As the title suggests, I am struggling with unit testing. This is the very first time I have create a unit test in SalesForce.

How would I create a unit test for the following function?

public with sharing class CaseStatusSearchMainController {

        public Case searchResults{get;set;}
        public Tree__c treeInCase{get;set;}


        public String searchCaseNumber { get; set; }
        public string searchResultsMessage { get; set; }
        public CaseStatusSearchMainController() {

        searchResultsMessage = 'Search Results';

        }

        public void searchForCases() {

        try
            {       
            searchResults = [Select Id, CaseNumber, Status, Tree__c, Subject, Description from Case WHERE caseNumber LIKE :searchCaseNumber ORDER BY Id DESC LIMIT 1];
            treeInCase = [Select Action__c, Diseases__c, Status__c from Tree__c WHERE Id = :searchResults.Tree__c];
            }
        catch (Exception e)
            {
            ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'Please ensure you enter your case number, you will find this on your case confirmation email or letter');
            ApexPages.addMessage(errorMessage);
            }
       }
}

I have setup a new class called test, used the @isTest syntax, this test class also has a function to create a few test objects.

How would I write a piece of code that executes the functions on my class?

Edit: As requested I have posted my test class.

public with sharing class CaseStatusSearch_TEST {

   static testMethod void testCaseSearch(){

       // Perform our data preparation.
    List<Case> cases = new List<Case>{};

    for(Integer i = 0; i < 200; i++){
        Case c = new Case(Subject = 'Test Case ' + i );
        c.Description = 'This Case is probably left over from testing. It should probably be deleted.';
        c.Tree__c = 'a00240000011lA8';
        c.Status = 'New';
        c.Origin = 'Phone';
        cases.add(c);
    }

    // Start the test, this changes governor limit context to 
    // that of trigger rather than test. 
    test.startTest();

    // Insert the Account records that cause the trigger to execute.
    insert cases; 

    // Stop the test, this changes limit context back to test from trigger.
    test.stopTest();

    // Query the database for the newly inserted records.
    List<Case> insertedCases = [SELECT Subject, Description 
                                      FROM Case
                                      WHERE Id IN :cases];

    // Assert that the Description fields contains the proper value now.
    for(Case c : insertedCases){
      System.assertEquals(
        'This Case is probably left over from testing. It should probably be deleted.', 
        c.Description);
    }

   }
4
  • 1
    Post your test method as well. This seems like a very simple class, with just one method. So it shouldn't be too difficult to write test code for this.
    – AslamK
    Dec 5, 2014 at 12:25
  • You will need to actually use your class somewhere within your test to actually get any coverage. Dec 5, 2014 at 13:37
  • 3
    You aren't Instantiating the controller therefore your method isn't getting called. Have a look at: salesforce.com/us/developer/docs/pages/Content/…
    – Girbot
    Dec 5, 2014 at 14:40
  • @Girbot You should post this as your answer to this question.
    – crmprogdev
    Dec 5, 2014 at 17:17

0

You must log in to answer this question.

Browse other questions tagged .