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);
}
}