+ Start a Discussion
shikher jainshikher jain 

How can i assert trigger error in my test class ?

Here is my test class please help me out how can i assert trigger error "you cant insert teacher with subject Hindi" in my test class.
@isTest
public class NotInsertTeacherTest {

    @Istest static void NotInsertTeacherHavingSubjectHindi(){
        Class__c testClass = new Class__c(Name = 'Hindi', MaxSize__c = 20);
        insert testClass;
        Contact testContact = new Contact(LastName = 'jain');
        insert testContact;
        Teach__c teacher = new Teach__c(Name = 'Shikher',Subjects__c = 'English',
                                        Teacher__c = testContact.id ,Class__c = testClass.id );

        
        
        test.startTest();
        insert teacher;
        test.startTest();
    }
}
Best Answer chosen by shikher jain
ANUTEJANUTEJ (Salesforce Developers) 
Hi Shikhar,

If I get your question correctly you would like to test for the error condition as well in the trigger if that is the scenario can you please have a look at the below link wherein there is a sample scenario for the testing of exception case.

>> https://developer.salesforce.com/forums/?id=906F00000008xGuIAI

In case if this helps or comes in handy can you please choose this as the best answer so that it can be used by others in the future.

Regards,
Anutej