You need to sign in to do that
Don't have an account?
Matt Blatnik 8
Test Class Does not Create a record in Salesforce
Hi,
I am trying to learn Apex.
I have created a test class in Developer org to create an account and opportunity. When I Run Test it displays success, but does not create records in Sandbox.
What could be the reason?
@IsTest(SeeAllData=true)
public class CreateTestAccOpp {
static TestMethod void Test1(){
Account Acc = new Account(Name = 'ABC');
insert Acc;
Opportunity Opp = new Opportunity (Name = 'TestDrewberry', CloseDate=system.today(),StageName = 'Qualified', AccountId = Acc.id);
insert Opp;
}
}
I am trying to learn Apex.
I have created a test class in Developer org to create an account and opportunity. When I Run Test it displays success, but does not create records in Sandbox.
What could be the reason?
@IsTest(SeeAllData=true)
public class CreateTestAccOpp {
static TestMethod void Test1(){
Account Acc = new Account(Name = 'ABC');
insert Acc;
Opportunity Opp = new Opportunity (Name = 'TestDrewberry', CloseDate=system.today(),StageName = 'Qualified', AccountId = Acc.id);
insert Opp;
}
}
For more on unit testing, I suggest you go to trailhead and go through the apex unit testing module, for one. https://developer.salesforce.com/trailhead/en/apex_testing/apex_testing_intro
All Answers
Hi Matt,
Records created in test method doesn't get saved in database. All the records exist only in the scope of test methods.
You can use @testSetup annotation to get all the records in your test methods.
Thanks,
Naval
For more on unit testing, I suggest you go to trailhead and go through the apex unit testing module, for one. https://developer.salesforce.com/trailhead/en/apex_testing/apex_testing_intro
See what salesforce documented in the docs.
Apex test data is transient and isn’t committed to the database.
This means that after a test method finishes execution, the data inserted by the test doesn’t persist in the database. As a result, there is no need to delete any test data at the conclusion of a test. Likewise, all the changes to existing records, such as updates or deletions, don’t persist. This transient behavior of test data makes the management of data easier as you don’t have to perform any test data cleanup. At the same time, if your tests access organization data, this prevents accidental deletions or modifications to existing records.
By default, existing organization data isn’t visible to test methods, with the exception of certain setup objects. You should create test data for your test methods whenever possible. However, test code saved against Salesforce API version 23.0 or earlier has access to all data in the organization. Data visibility for tests is covered in more detail in the next section.