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
Matt Blatnik 8Matt 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;
}
}


 
Best Answer chosen by Matt Blatnik 8
James LoghryJames Loghry
Test methods only create methods for the lifecycle of the test.  Any records that are created or updated or deleted are rolled back before the test finishes.  That's part of the beauty of unit testing.  This stands true even when you use the SeeAllData=true annotation, which you should stay far away from by the way.

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

Naval Sharma4Naval Sharma4

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

James LoghryJames Loghry
Test methods only create methods for the lifecycle of the test.  Any records that are created or updated or deleted are rolled back before the test finishes.  That's part of the beauty of unit testing.  This stands true even when you use the SeeAllData=true annotation, which you should stay far away from by the way.

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
This was selected as the best answer
James LoghryJames Loghry
Sorry, that should have read "only create records for the lifecycle of the test"
Naval Sharma4Naval Sharma4
Hi Matt,

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.