1

When I run my test class from developer console, I get the error message:

Testing already stopped.

Can some one help me to fix this issue?

Here is my test class.

@isTest
public class BatchtoUpdateBusinessDaysTest {
    static testmethod void testBatchExecution (){
       List <Case> cse = new List<Case>();
        for(Integer i=0; i<200; i++) {

            Case c = new Case();
            c.Status = 'New';
            c.subject = 'Test case no:' + i;
            c.Division__c = 'Acura';
            c.subdivision__c = 'Campaign';
            c.Origin = 'ACS to Mediation';
            c.Type = 'Arbitration';
            c.Point_of_Origin__c = 'Attorney';
            cse.add(c);
        }

        Test.startTest();
        insert cse;
        String actionType = 'Email';
        integer j = 0;
        BatchtoUpdateBusinessDays b = new BatchtoUpdateBusinessDays ();
        ID batchprocessid = Database.executeBatch(b, 200);

        List<Case> inserted_cs = [select Id from case WHERE Id IN :cse];
        for(Case cs : inserted_cs ){
            j++;
            FeedItem post = new FeedItem();
            post.body = '[Feed item posted]';
            Post.parentid = cs.Id;        
            insert post;
            Test.stopTest();
        }
    }
}
0

3 Answers 3

5

You've put your "Test.stopTest()" inside a loop, so it's getting called more than once. Make sure that the method is only called once per unit test, or you'll get this error.

for(Case cs : inserted_cs ){
    j++;
    FeedItem post = new FeedItem();
    post.body = '[Feed item posted]';
    Post.parentid = cs.Id;        
    insert post;
}
Test.stopTest();

Also, generally speaking, you should not have DML inside a for loop, but instead create a list and insert all the records at once:

FeedItem[] postList = new FeedItem[0];
for(Case cs : inserted_cs ){
    j++;
    FeedItem post = new FeedItem();
    post.body = '[Feed item posted]';
    Post.parentid = cs.Id;
    postList.add(post);
}
insert postList;
Test.stopTest();
3

You can only call Test.stopTest() once. Since you have put that line in a loop, you are calling it many times. Consistent use of indentation can help you spot this sort of error:

static testMethod void myTest()
{
    // other logic
    for (Case record : someCollection)
    {
        // do stuff
        Test.stopTest();
    }
}

At the very least, move that stopTest call outside the loop.

static testMethod void myTest()
{
    // other logic
    for (Case record : someCollection)
    {
        // do stuff
    }
    Test.stopTest();
}

It's also worth having a read of Using Limits, startTest, and stopTest. The main uses of startTest and stopTest are to ensure asynchronous processing has completed and verify governor usage. You may want to wrap them around a more limited portion of your code.

In addition to the Limits methods, use the startTest and stopTest methods to validate how close the code is to reaching governor limits.

The startTest method marks the point in your test code when your test actually begins. Each test method is allowed to call this method only once. All of the code before this method should be used to initialize variables, populate data structures, and so on, allowing you to set up everything you need to run your test. Any code that executes after the call to startTest and before stopTest is assigned a new set of governor limits.

The startTest method does not refresh the context of the test: it adds a context to your test. For example, if your class makes 98 SOQL queries before it calls startTest, and the first significant statement after startTest is a DML statement, the program can now make an additional 100 queries. Once stopTest is called, however, the program goes back into the original context, and can only make 2 additional SOQL queries before reaching the limit of 100.

The stopTest method marks the point in your test code when your test ends. Use this method in conjunction with the startTest method. Each test method is allowed to call this method only once. Any code that executes after the stopTest method is assigned the original limits that were in effect before startTest was called. All asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously.

1

You have Test.stopTest() inside the loop, move it out.

You must log in to answer this question.

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