3
trigger CaseTri on Case (after insert) {
    if(Trigger.isAfter){
        if(Trigger.isUpdate){
             if(classA.Method1){    
                webServices.notify(Trigger.New[0].Id);
                ClassA.Method1 = true;                
             }
        }
        if(Trigger.isAfter){
            if(Trigger.isInsert){
                if(ClassA.method1) {  
                    webservice.CcNotification(Trigger.New[0].Id);
                    ClassA.method1 = true;
                }
            }
        }

webservice.CcNotification(Trigger.New[0].Id);->line which calls future method

My testclass:

@isTest(SeeAllData=true)
public class CaseTri{
public static testMethod void testnotif(){
Case c = new Case();
insert c;
Test.startTest();
Test.setMock(WebServiceMock.class, new WebServiceMockImplnotif());


  Test.stopTest();
}
}
1
  • Best practice - get rid of SeeAllData=true. Mock all sobjects and custom settings that your test needs. You or your successor will appreciate this effort when it comes time to do deployment
    – cropredy
    Commented Dec 12, 2015 at 16:30

2 Answers 2

3

When performing tests, you only have one bite at the apple when it comes to executing Async methods.

Since you have two different conditions in your trigger it is wise to write two separate test methods so that you can properly assert the results. It gets complex if you do both the insert and update before the stop test. Maybe not in this case but in larger implementations so I am going to show you the multi method way

Note I do not know what the ClassA.method1 criteria is so ensure the records meet the criteria.

Important Point: Any future methods hit before the test.startTest will not be executed when the execution hits test.stoptest

Update Your Trigger: trigger CaseTri on Case (after insert, after update)

Test Class Framework

@isTest
public class CaseTri{

    public static testMethod void insertTest(){
        Case c = new Case();

        Test.startTest();
        Test.setMock(WebServiceMock.class, new WebServiceMockImplnotif());
        //You need to do the Insert inside of the Test.start and test.stoptest or the future method will never be executed
        insert c;

        Test.stopTest();

        //Query for your records
        .....
        //Assert for proper results

    }

    public static testMethod void insertTest(){
        Case c = new Case();
        //Do this here so the future method for the insert does NOT get queued up after the stop test
        insert c;
        Test.startTest();
        Test.setMock(WebServiceMock.class, new WebServiceMockImplnotif());
        //You need to do the update inside of the Test.start and test.stoptest or the future method will never be executed
        update c;

        Test.stopTest();

        //Query for your records
        .....
        //Assert for proper results

    }


}
1
  • 1
    Just curious here, were you just copying the OP's code when including (SeeAllData=true) or is it necessary for some other reason, such as the web services callout which happens to be mocked, presumably preventing it from being exposed?
    – crmprogdev
    Commented Dec 12, 2015 at 16:31
0

Include a Test.stopTest() in your test method; that will ensure that the async call (the future) has completed so you can assert the expected outcome.

5
  • I have done it,but test coverage is only till "notify" method and not for "CcNotification"method.how to call that method?for full test coverage Commented Dec 12, 2015 at 12:26
  • @Pri How can the test cover the notify method? The trigger only fires after insert. Nothing inside the if(Trigger.isUpdate){ block should ever execute.
    – martin
    Commented Dec 12, 2015 at 14:28
  • I have updated code with my test class.is it correct? Commented Dec 12, 2015 at 14:46
  • @Pri You should probably consider changing the first line of your trigger: trigger CaseTri on Case (after insert, after update) { . Then in the test class, make sure you are updating a case so that the update trigger will fire.
    – martin
    Commented Dec 12, 2015 at 15:32
  • yes @martin got it.. Commented Dec 12, 2015 at 17:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .