You need to sign in to do that
Don't have an account?
Felix 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:
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; } }
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!
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?
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:
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.
Couple of questions:
* 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.
Can you share your trigger code too? I can try and get back to you.
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.
(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.