+ Start a Discussion
Chitral ChaddaChitral Chadda 

test method

@isTest

private class MyTestMethod {

   // Methods for testing


static testMethod void verifyAccountDescriptionsWhereOverwritten(){

    // Perform our data preparation.

    List<Account> accounts = new List<Account>{};

        

    for(Integer i = 0; i < 500; i++){

        Account a = new Account(Name = 'Test Account ' + i);

        accounts.add(a);

    }



    // Start the test, this changes governor limit context to

    // that of trigger rather than test.

    test.startTest();

        

    // Insert the Account records that cause the trigger to execute.

    insert accounts;

        

    // Stop the test, this changes limit context back to test from trigger.

    test.stopTest();

        

    // Query the database for the newly inserted records.

    List<Account> insertedAccounts = [SELECT Name, Description

                                      FROM Account

                                      WHERE Id IN :accounts];

        

    // Assert that the Description fields contains the proper value now.

    for(Account a : insertedAccounts){

      System.assertEquals(

        'This Account is probably left over from testing. It should probably be deleted.',

        a.Description);

    }

}
}




now since we are inserting 300 records
 before start test we can add max. 100 records(as it refreshes the limit) . //  insert accounts;
when start test runs we can add 100 new records 

but still 100 records are left .
wen i save this test class its not showing error.

m  not able to understand this point thru docs also
Best Answer chosen by Chitral Chadda
Vatsal KothariVatsal Kothari
For below code
for(Integer i = 0; i < 500; i++){
Account a = new Account(Name = 'Test Account ' + i,Description = 'This Account is probably left over from testing. It should probably be deleted.');
accounts.add(a);
}
you are not executing any DML operation, you are just creating objects of accounts and adding them to list, so accounts list will contains 500 accounts.

In above test class you are having only One DML statement i.e insert accounts.
insert accounts will insert above 500 accounts in single DML statement.

Governor limit is 150 DML Statement.

All Answers

Vatsal KothariVatsal Kothari
Hi Chitral,

Your test class is correct but the assert statement your checking is wrong, because you are not assigning any value to account description and in the last loop you are checking for account description.

Replace your below code:
for(Integer i = 0; i < 500; i++){
			Account a = new Account(Name = 'Test Account ' + i);
			accounts.add(a);
		}
with
for(Integer i = 0; i < 500; i++){
			Account a = new Account(Name = 'Test Account ' + i,Description = 'This Account is probably left over from testing. It should probably be deleted.');
			accounts.add(a);
		}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
Chitral ChaddaChitral Chadda
@Vatsal Kothari

my question was that
for(Integer i = 0; i < 500; i++)
we have  created 500 records


with test.starttest we add hv additinal 100 soql queries .


// Start the test, this changes governor limit context to
    // that of trigger rather than test.
test.startTest();

       

    // Insert the Account records that cause the trigger to execute.

    insert accounts;

       

    // Stop the test, this changes limit context back to test from trigger.

    test.stopTest();


after test.endtest

   how does it avoid governor limit i can understand anying from the documents available so i posted this qns here
how wil this happen?
Vatsal KothariVatsal Kothari
For below code
for(Integer i = 0; i < 500; i++){
Account a = new Account(Name = 'Test Account ' + i,Description = 'This Account is probably left over from testing. It should probably be deleted.');
accounts.add(a);
}
you are not executing any DML operation, you are just creating objects of accounts and adding them to list, so accounts list will contains 500 accounts.

In above test class you are having only One DML statement i.e insert accounts.
insert accounts will insert above 500 accounts in single DML statement.

Governor limit is 150 DML Statement.
This was selected as the best answer
Chitral ChaddaChitral Chadda

500 records are created and added to the list .
since governor limit is 150 for dml statemnt to advoid it we  use test.starttest() and stoptest method.


so m unable to understand at this point that 


 test.startTest();

    insert accounts;

  test.stopTest();


inside the test.start() it will insert all the 500 records ?? 
//cz i read somewhere in the docs that it refreshes the limit something something ...landing me confused  totally

does it mean that no matter how manny records we add in the list 

inside test.starttest()
insert accounts;

will insert all those 1000/500 records in one go there by avoiding governor limit of 150 stmnt

Vatsal KothariVatsal Kothari
yes it will insert all 500 records in one go.
Governor limit is on DML statement is 150 and records numbers are depends on heap size, for Synchronous its 6Mb and for Asynchronous its 12Mb. 
Chitral ChaddaChitral Chadda

okay   , thnkyou sir
i dnt find much use cz v cud hv directly inserted account rather than using test.starttest() and stoptest method.
if we can do like this than wats the use of these methods.

@isTest

private class MyTestMethod {

   // Methods for testing


static testMethod void verifyAccountDescriptionsWhereOverwritten(){

    // Perform our data preparation.

    List<Account> accounts = new List<Account>{};

       

    for(Integer i = 0; i < 500; i++){

        Account a = new Account(Name = 'Test Account ' + i,description='This Account is probably left over from testing. It should probably be deleted.');

        accounts.add(a);

    }



    
    insert accounts;

      
       

    // Query the database for the newly inserted records.

    List<Account> insertedAccounts = [SELECT Name, Description

                                      FROM Account

                                      WHERE Id IN :accounts];

       

    // Assert that the Description fields contains the proper value now.

    for(Account a : insertedAccounts){

      System.assertEquals(

        'This Account is probably left over from testing. It should probably be deleted.',

        a.Description);

    }

}
}

Vatsal KothariVatsal Kothari
Test.startTest() and Test.stopTest() static methods are used to get different set of governor limits for the enclosed code. Please go through the below article to have more clarity:-
http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
Chitral ChaddaChitral Chadda
i went thru this document bt still i thot if we cud do our work this way directly then y to use starttest and stoptest().  this is what i asked initallly
Vatsal KothariVatsal Kothari
The idea is that any queries or DML operations that you do to setup your test should be down before the Test.startTest() method. Then when testing your code such as executing a method your are testing do that between the start and stop method calls.
This gives the unit test context. Which will basically ignore any dml or queries done outside of your start and stop testing and only count what happens in between as part of your test. Otherwise all setup code and actual test code are all considered part of the same context and thus subject to be counted towards the limits.

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.
Chitral ChaddaChitral Chadda

thnkyu so much 

if 500 records are  added in the list then 

suppose 98 soql queries are made  before test.starttest() 

nw it will add 100 additionall soql queries

n after test.stoptest()

it will add 2 more before it reaches limit of 100...

 

 

this is wat i m hell confused ...so 200 records are added in total what about the remaining 300 records out if 500.

cz insided
test.starttest();
insert accounts;

we can add max of 100 records  (queries).  thn wat about remaining ones .

 

as u explained earlier that insert account will add all d 500 records at once

bt limit is that of 100 how it happens?