function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Felix Jong Seok ChaeFelix Jong Seok Chae 

Can I insert a new record through a method in Apex Test Class?

I am trying to write a test class and its methods in Apex.
I first created a case record in one of test methods and insert that case, but after I did test->New run for that method, I didn't see the new record was created in the salesforce case object.
Here is the apex code:
@isTest
public class MyTestClass {
    /**
     * Test for inserting a single case record
     */
    @isTest static void singleCaseInsertion() {
        Case newCase = new Case(
    		subject = 'abc', 
                type = 'def', 
    		description = 'testing.'
        );
	insert newCase;
    }
}

 
Malni Chandrasekaran 2Malni Chandrasekaran 2
Felix,
The data changes whatever done from Test methods will not be committed in database.  The data will be used only with in the test class and any changes will not be committed in the database.
Please refer,
https://developer.salesforce.com/docs/atlas.en-us.208.0.apexcode.meta/apexcode/apex_testing_data.htm

Hence, you will not be able to see the data either with SOQL or page layout or anyways.

Hope this helps!

 
Felix Jong Seok ChaeFelix Jong Seok Chae
Thank you Malni.
My after-insert trigger set a field value of a new case, but when I do System.assert(newCase.field__c != null), I get a null pointer exception. Why is that?
Amit Singh 1Amit Singh 1
Felix, You need to assign the value for all field into Test class that are used into Case Trigger.
Felix Jong Seok ChaeFelix Jong Seok Chae
Thanks Amit.
I mean I'm testing if my after-insert trigger can set a right value for a specific field. So even if I do not explicitly set a value for that field in the test method, after-insert trigger is supposed to set the value after I insert some case in a method in the test class, and that's what I'm testing on.
Here is the pseudocode:
Trigger on Case(after insert) {
//set the value of the field3
}

// this method is defined in a testClass
@isTest testMethod() {
Case newCase = new Case(field1 = '', field2= '');
insert newCase;
System.assert(newCase.field3 != null);
}

 
Malni Chandrasekaran 2Malni Chandrasekaran 2
Felix,
I assume you have tested your trigger by inserting your data from the page layout and you got the expected result.
I have not seen your trigger. sometimes it fails if your trigger involves lot of dml operations.
You may try adding Test.StartTest() and Test.StopTest() methods (which helps to restart the governor limits) in your testmethod and let me know.
 
Felix Jong Seok ChaeFelix Jong Seok Chae
I'm sorry but where do I put Test.startTest() and Test.stopTest()? I put insert newCase between two methods and received a message that assertion was failed.
Felix Jong Seok ChaeFelix Jong Seok Chae
I also tried to put Test.startTest() before Case declartion statment but still get assertion fail message.
Malni Chandrasekaran 2Malni Chandrasekaran 2
Felix,

Couple of questions:
  • Have you tested your trigger by inserting your data from the page layout and you got the expected result?
  • Have you tried using any checkpoints or debug statements to locate where exactly it is failing? Eg: You may use database.update to update the case record, and log the saveresult using System.debug statement after the update statement in your trigger to see if the update was success or not?
    • Database.SaveResult SR =  Database.update(varCase);
Felix Jong Seok ChaeFelix Jong Seok Chae
Malni,

* Yes, I get expected result when I insert a case in anonymous window.
* It was assertion error saying that field that should be updated by trigger has null value after running test. 
Malni Chandrasekaran 2Malni Chandrasekaran 2
Felix,
Can you share your trigger code too? I can try and get back to you.
Felix Jong Seok ChaeFelix Jong Seok Chae
Thanks Malni.
I found the solution and it happened because test method was not accessible to sf database. I used "@isTest(SeeAllData=true)" above the test class, and it worked. 
Malni Chandrasekaran 2Malni Chandrasekaran 2
Felix,
(SeeAllData = True) annotation is used to access all the data in the organization which is not recommended practice. And we dont need this annotation to access data created in test class. You will learn it when you go further :) All the best.