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
CBNCBN 

Help me to write a test class for trigger

trigger CaseUpdate on Case (before update) {
    
    for(Case c:trigger.New){
      if( Trigger.oldMap.get( c.id ).Status != Trigger.newMap.get(c.id ).Status && Trigger.newMap.get(c.id ).Status =='Working')   
            c.OwnerId=Userinfo.getUserID();
  }

}
Best Answer chosen by CBN
GovindarajGovindaraj
Hi CBN,

Please try below test class,
@isTest
public class caseTestClass {
    @isTest
    public static void test_method_one() {
		Case caseObj = new Case();
		caseObj.Subject = 'Test Case';
		caseObj.Status = 'New';
        caseObj.Origin = 'Email';
		insert caseObj;
		
		Test.startTest();
		caseObj.Status = 'Working';
		update caseObj;
		Test.stopTest();
	}
}
Thanks,
Govindaraj.S

All Answers

GovindarajGovindaraj
Hi CBN,

Please try below test class,
@isTest
public class caseTestClass {
    @isTest
    public static void test_method_one() {
		Case caseObj = new Case();
		caseObj.Subject = 'Test Case';
		caseObj.Status = 'New';
        caseObj.Origin = 'Email';
		insert caseObj;
		
		Test.startTest();
		caseObj.Status = 'Working';
		update caseObj;
		Test.stopTest();
	}
}
Thanks,
Govindaraj.S
This was selected as the best answer
Andrew GAndrew G
@isTest
private class CaseUpdate_test {
    
    @isTest static void test_method_one() {
        // Implement test code

        User oldUser = TestClassHelper.createUser('user@name.net','old','dummy@email.net','noalias');
        Insert oldUser;

        //Create Accounts
        List<Account> accts = TestClassHelper.createStandardAccs(1);
        Insert accts;
        List<Account> insAcct = [SELECT Id FROM Account WHERE Name like 'Account%'];
        //Create cases
        List<Case> cases = TestClassHelper.createCases(1,'Low',insAcct[0].Id);
        for (Case c : cases ) {
            c.OwnerId = oldUser.Id;
        }
        Insert cases;
        List<Case> insCase = [SELECT Id, OwnerId,Status FROM Case WHERE AccountId = :insAcct[0].Id];
        System.assertEquals(insCase.size(),1);
        System.assertEquals(insCase[0].OwnerId, oldUser.Id);

        insCase[0].Status = 'Working';
        update insCase;

        insCase = [SELECT Id, OwnerId,Status FROM Case WHERE AccountId = :insAcct[0].Id];
        System.assertEquals(insCase.size(),1);
        System.assertEquals(insCase[0].OwnerId, UserInfo.getUserId());
        
    }
    
}
I use a Helper Class for my test data, but this should give you the idea.

Note, you need to test both the before and after aspects, that is to be sure the code works as expected with the Change of status.  
You may want to break that into 3 x test methods to be more thorough. To ensure you test each of the criteria for your IF statement.

1 for status not working.
1 for status changed to working
1 for case updated but status not changed.  


Regards
Andrew
 
Andrew GAndrew G
@Govindaraj 

Where are the Asserts?
The purpose of test code is to check that the code that is written is doing what it is intended to do.  With no "Assertions", how do you know the code wont be broken the next time someone pushes an update?  Yes, you have covered the lines of code, as in they will be "actioned" by the test class, but coverage without Assertions is useless (IMHO).

Regards
Andrew