+ Start a Discussion
NatureGodNatureGod 

Proper Test Class

Hi all,

I have the following Trigger:
trigger SeekSerialNumberinCase on Case(before insert, before update)  {

Map<String, Case> CaseMap = new Map<String, Case>();
    for (Case Ca : System.Trigger.new) {
//        if ((Ca.Serial_Number__c != null) &&
//                (Ca.Serial_Number__c != System.Trigger.oldMap.get(Ca.Id).Serial_Number__c)) {
            if (CaseMap.containsKey(Ca.Serial_Number__c)) {
//                Ca.Serial_Number__c.addError('Another Case has the ' + 'same Serial_Number__c.');
           } else {
               CaseMap.put(Ca.Serial_Number__c, Ca);
//            }
       }
    }
    for (Case Ca : [SELECT Serial_Number__c FROM Case WHERE Serial_Number__c IN :CaseMap.KeySet()]) {
        Case newCase = CaseMap.get(Ca.Serial_Number__c);
          newCase.Rerepair__c = TRUE;
//        newCase.Serial_Number__c.addError('A Case with this Serial_Number__c ' + 'already exists.');
    }
}

Which seeks if a Serial number exists in cases.
If yes , it assigns a pic sotred in the documents.

Any suggestions for a Test class worth > 75% ?

Kind Regards,
Stefanos
sfdc_ninjasfdc_ninja

Test Classes in general need to do 3 basic things.
  1. Create your test model (data)
  2. Perform your class logic (in this case, fire the trigger)
  3. Assert the post conditions
I have written 2 test methods below, I would not consider this done however.  You need to test other logical possiblities such as bulk inserts, updates, etc.  This will show though how you should approach your testing methods and begin to create a testing strategy for your org.


@isTest
private class CaseTriggerHelperTest {

private static final Account myAccount;
private static final Contact myContact;

static {
myAccount = new Account(Name = 'Test');  //Ensure all attributes are set so it passes validtion in your org
insert myAccount;
myContact = new Contact(FirstName = 'John', LastName = 'Doe', AccountId = myAccount.Id);  
insert MyContact;
}

private static void createCase(string serNum){

Case myCase = new Case(AccountId = myAccount.Id, ContactId = myContactId);
if(serNum != null && serNum != ''){
myCase.Serial_Number__c = serNum;
}
return myCase;
}

public static testmethod void BasicTestNoDup() {

Case case1 = createCase('1234');
insert case1;

//Try adding another case with a different Serial Number
Case case2 = createCase('5678');

//Since it s different serial number, there should be no issue and the case should be inserted without error
try{
insert case2;
}catch(Exception e){
system.assert(false, 'No error was expected, but received one');
}
}

public static testmethod void BasicTestWithDup() {
Case case1 = createCase('1234');
insert case1;

//Try adding another case with the same Serial Number
Case case2 = createCase('1234');

//Since it's the same serial number, there should be an error and the insert should not happen
try{
insert case2;
system.assert(false, 'An exception was expected, and wasn\'t thrown');
}catch(Exception e){
system.assertEquals(true, e.getMessage().contains('Your Error message here'));
}
}

}