12
@isTest
public class CaseUtilityTest {

    @isTest static void TestCreateCase()
    {
        Test.startTest();
        CaseUtility.CreateCase(100,false); // 1
        Test.stopTest();

        Test.startTest();
        CaseUtility.CreateCase(100,true); //2
        Test.stopTest();


            }

    @isTest static void TestDeleteAll()
    {
        Test.startTest();
        CaseUtility.DeleteAll();
        List<Case> c_list = [SELECT ID FROM Case];
        System.assertEquals(c_list.size(), 0);
        Test.stopTest();
    }


}

Class

public  class CaseUtility {

    @future
    public static void CreateCase(Integer num,Boolean sa_yes)
    {
         List<Case> case_list = new List<Case>();
        for(Integer cnt = 0; cnt < num; cnt++)
        {

            if(sa_yes)
            {
              case_list.add(new Case(AccountId = '00128000002iZHI',SpendAccount__c = 'a0028000005YEoO',Reason = 'SAMPLE CASE # '+cnt));  
            }
            else
            {
               case_list.add(new Case(AccountId = '00128000002iZHI',Reason = 'SAMPLE CASE # '+cnt)); 
            }
        }
        insert case_list;
    }

    @future
    public static void DeleteAll()
    {
        delete  ([SELECT ID FROM Case]);
    }
}

When I try to do a test run I am getting the following error in this test method (TestCreateCase()), I believe it is because I am calling .start/.stoptest two times within the same method.

System.FinalException: Testing already started (System Code) Class.CaseUtilityTest.TestCreateCase: line 10, column 1

But I am not able to understand the reasoning behind that error..

Can someone let me know why I am getting the error ?

1 Answer 1

14
 @isTest static void TestCreateCase()
{
    Test.startTest();
    CaseUtility.CreateCase(100,false); // 1
    Test.stopTest();


 }
 @isTest static void TestCreateCase2(){
 Test.startTest();
    CaseUtility.CreateCase(100,true); //2
    Test.stopTest();
}

Your first method has Test.startTest() twice .In a single test class you can have one test.starteTest and one Test.stopTest(). I have split into two diffferent methods .

You must log in to answer this question.

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