+ Start a Discussion
Eric DelgadoEric Delgado 

My first unit test for trigger

Hi,
I am trying to create a new unit test for a trigger I am creating based on this example. 
https://developer.salesforce.com/docs/atlas.en-us.196.0.sos.meta/sos/service_cloud_guides-auto_case_pop.htm
trigger SOSCreateCaseCustom on SOSSession (before insert) {
    List<SOSSession> sosSess = Trigger.new;
    for (SOSSession s : sosSess) {
        try {
            Case caseToAdd = new Case();
            caseToAdd.Subject = 'SOS Video Chat';
            if (s.ContactId != null) {
                caseToAdd.ContactId = s.ContactId;
            } else {
                List<Contact> contactInfo = [SELECT Id from Contact WHERE Email = :s.AppVersion];
                if (!contactInfo.isEmpty()) {
                    caseToAdd.ContactId = contactInfo[0].Id;
                    s.ContactId = contactInfo[0].Id;
                }
            }
            insert caseToAdd; s.CaseId = caseToAdd.Id;
        }catch(Exception e){}
    }
}

Here is the unit test I created for the trigger.
@isTest
private class SOSCreateCaseCustomTest {
	static testMethod void validateSOSCreateCase() {
        init();
        Test.startTest();
        
        String caseSubject = 'SOS Video Chat';
        
		// set up case to add 
        Case caseToAdd = new Case(Subject='SOS Video Chat');
        insert caseToAdd;
        
		Case ca = [SELECT Subject, ContactId from Case where Subject =: caseSubject];
		System.assertEquals(caseSubject, ca.Subject);
        
        Test.stopTest();
	}
}

The unit test doesn't seem to test against the trigger I created above. How can I map this unit test to the trigger? Thanks.
 
Best Answer chosen by Eric Delgado
venkat-Dvenkat-D
Based on your logic you should create a new SOSession record in test class not case.

All Answers

REVNAREVNA
all you need to do it , just create a case record from test class and insert it. And the trigger will get covered.
Eric DelgadoEric Delgado
Can you give me an example? I am totally new to apex.
Eric DelgadoEric Delgado
Thanks for the example. I did the insert case here: 
Case caseToAdd = new Case(Subject='SOS Video Chat');
insert caseToAdd;
But it still doesn't cover it. I am not sure if I understand what you mean.
REVNAREVNA
@isTest() 
private class test_sampleCase {
 
    private static testMethod void testCase(){
        List<Case> tstCase = createTestCase();
         insert tstCase;
        System.assertEquals();        
    }
    
    public static Case createTestCase(){
        Case c=new Case(Subject='test',contactId='Set a contact id ');
        return c;
    }
    
    
    }

somehting like this. But since yours is before insert you dont even need to do a DML in trigger. Just set the value of the field to what to you want , and it will be handled during insert. there is no need for an explicit DML.
Eric DelgadoEric Delgado
Do you mean that I don't need to specify which class or function I am testing and it will automatically cover it? When I look at the Code Coverage for the trigger, it doesn't seem to cover anything.
REVNAREVNA
trigger SOSCreateCaseCustom on Account (before insert) {
    List<Account> sosSess = Trigger.new;
    for (Account s : sosSess) {
        try {
            Case caseToAdd = new Case();
            caseToAdd.Subject = 'SOS Video Chat';
            if (s.Name!= null) {
                caseToAdd.subject= s.Name;
            } else {
                List<Contact> contactInfo = [SELECT Id from Contact WHERE Name LIKE '%test%'];
                if (!contactInfo.isEmpty()) {
                    caseToAdd.ContactId = contactInfo[0].Id;
                    s.Name= contactInfo[0].Id;
                }
            }
            s.Name=s.name+' Added From Trigger';
            //insert caseToAdd; s.CaseId = caseToAdd.Id;
        }catch(Exception e){}
    }
}
 
@isTest() 
private class test_sampleCase {
 
    private static testMethod void testCase(){
        Account tstCase = createTestCase();
         insert tstCase;
        //System.assertEquals();        
    }
    
    public static Account createTestCase(){
        Account a=new Account(Name='test');
        return a;
    }
    
    
    }

Try this . And modify accordingly. I have used Account object , since I did not have your custom object in place.

 
Eric DelgadoEric Delgado
The unit test doesn't seem to cover it:
User-added image

This is my code based on what you gave me:
@isTest
private class SOSTriggerTest {
    private static testMethod void testCase(){
        Case tstCase = createTestCase();
        insert tstCase;
        System.assertEquals('SOS Video Chat', tstCase.Subject);       
    }
    
    public static Case createTestCase(){
        Case c = new Case(Subject='SOS Video Chat');
        insert c;
        return c;
    }    
}

 
venkat-Dvenkat-D
Based on your logic you should create a new SOSession record in test class not case.
This was selected as the best answer
REVNAREVNA
SOSSession  - create recs for this object in test class
Eric DelgadoEric Delgado
You are right! SOSSession is the record I need to create. Thanks!