1

For instance:

Country__c newCountry = new Country\__c();
newCountry.CountryName__c = 'GlobalLogic';
newCountry.IsActive__c = true;        
insert newCountry; // why we are using insert statement 

Is this to insert this object into database, if so then why we are inserting this temporary object into database because we are testing the code

If not so then we can simply do my testing without typing this insert statement at each of my object its my big confusion.

What will happen if i write this code without insert statement:

Country_c newCountry = new Country_c();
newCountry.CountryName__c = 'GlobalLogic';
newCountry.IsActive__c = true;
 //insert newCountry; // without this statement

What happens in this scenario ?

2 Answers 2

2

If you are testing code that does SOQL calls, then the testmethod should insert the test records - otherwise, there is nothing for the SOQL call to retrieve (assuming you are following best practices and not using @isTest(SeeAllData=true)

If you are testing code that just manipulates objects and doesn't do SOQL calls, then your testmethods don't need to insert anything into the database (unless the code being tested needs to have ID fields instantiated, or relies on formula fields / rollupsummaries / lookup relationships - in these use cases you need to insert SObjects into the database).

All test data inserted during a testmethod is automatically rolled back by SFDC at the completion of the testmethod - very convenient.

0

@Amit. Insert Newcountry will insert a new record under Country object. I don't think that country object is a temporary. Once the object exists then only u can insert a record. where as during test case we insert a dummy record inorder to make sure that our code works well.

2
  • You mean that it will get reflected in database. I am not able to understand , what does actually this insert command function ? Jan 9, 2014 at 9:15
  • Insert will stored the record in the database but will not commit. Once the trigger events are executed then it will commit into the database.See the "order of execution of trigger" for more info
    – user4902
    Jan 9, 2014 at 9:22

You must log in to answer this question.

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