+ Start a Discussion
Alex SidlinskiyAlex Sidlinskiy 

Creating Test Data for Apex Tests

I have a small problem. I'm trying to run the code snipper provided in the TrailHead for apex testing on "Creating TEst Data for Apex Tests" and my test code fails two test cases out 4. Here what my test runs results.

User-added image

Here what the code looks like:
@isTest
private class TestAccountDeletion {

    @isTest static void TestDeleteAccountWithOneOpportunity() {
        // Test data setup
        // Create one account with one opportunity by calling a utility method
        Account[] accts = TestDataFactory.createAccountsWithOpps(1,1);
        
        // Perform test
        Test.startTest();
        Database.DeleteResult result = Database.delete(accts[0], false);
        Test.stopTest();

        // Verify that the deletion should have been stopped by the trigger,
        // so check that we got back an error.
        System.assert(!result.isSuccess());
        System.assert(result.getErrors().size() > 0);
        System.assertEquals('Cannot delete account with related opportunities.',
                             result.getErrors()[0].getMessage());
    }
    
    @isTest static void TestDeleteAccountWithNoOpportunities() {
        // Test data setup
        // Create one account with no opportunities by calling a utility method
        Account[] accts = TestDataFactory.createAccountsWithOpps(1,0);
        
        // Perform test
        Test.startTest();
        Database.DeleteResult result = Database.delete(accts[0], false);
        Test.stopTest();

        // Verify that the deletion was successful
        System.assert(result.isSuccess());
    }
    
    @isTest static void TestDeleteBulkAccountsWithOneOpportunity() {
        // Test data setup
        // Create accounts with one opportunity each by calling a utility method
        Account[] accts = TestDataFactory.createAccountsWithOpps(200,1);
        
        // Perform test
        Test.startTest();
        Database.DeleteResult[] results = Database.delete(accts, false);
        Test.stopTest();

        // Verify for each record.
        // In this case the deletion should have been stopped by the trigger,
        // so check that we got back an error.
        for(Database.DeleteResult dr : results) {
            System.assert(!dr.isSuccess());
            System.assert(dr.getErrors().size() > 0);
            System.assertEquals('Cannot delete account with related opportunities.',
                                 dr.getErrors()[0].getMessage());
        }
    }
    
    @isTest static void TestDeleteBulkAccountsWithNoOpportunities() {
        // Test data setup
        // Create accounts with no opportunities by calling a utility method
        Account[] accts = TestDataFactory.createAccountsWithOpps(200,0);
        
        // Perform test
        Test.startTest();
        Database.DeleteResult[] results = Database.delete(accts, false);
        Test.stopTest();

        // For each record, verify that the deletion was successful
        for(Database.DeleteResult dr : results) {
            System.assert(dr.isSuccess());
        }
    }
}

Any help much appreciated.
Best Answer chosen by Alex Sidlinskiy
Prasad Avala(SFDC)Prasad Avala(SFDC)
Hi Alex,
I guess the accounts with out opprotunities are not getting deleted per business logic

As Tejpal mentioned there is some other validation or other (trigger)logic which prevents deletion thought the account does not have any opportunities

Can you try the following:

 1. Manually create an account without opprotunity and try to delete it(enable debug logs to see if any other logic is firing)
    please note deletion is sucessful or not.
2. Try to negate the asserts at line number 33 and 69(example:System.assert(!result.isSuccess()); and see if the test class passes.
if test class passes,then deffinitely there is some logic which prevents deletion of accounts.

 

All Answers

Tejpal KumawatTejpal Kumawat
Hello Alex,

It's look like some validation stoping to delete. You need to check delete Account that not have Opportunity manually.

Regards
Tej Pal Kumawat
Skype : tejpalkumawat1991

If this answers your question mark Best Answer it as solution and then hit Like!

 
RadnipRadnip
First I would always add a comment on asserts so you can understand why the assert fails, helpfully for other developers. TBH I think it should be a required parameter. Could you copy out the error. The image is tiny and can't see what the error is. It looks like its failing on the asserts? Take a look at the debug log for the particular test and see what is going on.
Alex SidlinskiyAlex Sidlinskiy
Hey Radnip,

That is correct. The TestDeleteBulkAccountsWithNoOpportunities and TestDeleteAccountWithNoOpportunities are failing on the assertions on lines 69 and 33. 
Prasad Avala(SFDC)Prasad Avala(SFDC)

Hi Alex,
can you paste the exact error (from developer console or from debug logs), so that it would be easy to nail down the issue.

Note: only the errors where it says line 33 and 69 are failed whole debug log is not required
Alex SidlinskiyAlex Sidlinskiy
Errors:
TestDeleteBulkAccountsWithNoOpportunities: Class.TestAccountDeletion.TestDeleteBulkAccountsWithNoOpportunities: line 69, column 1

TestDeleteAccountWithNoOpportunities: Class.TestAccountDeletion.TestDeleteAccountWithNoOpportunities: line 33, column 1
 
Prasad Avala(SFDC)Prasad Avala(SFDC)
Hi Alex,
I guess the accounts with out opprotunities are not getting deleted per business logic

As Tejpal mentioned there is some other validation or other (trigger)logic which prevents deletion thought the account does not have any opportunities

Can you try the following:

 1. Manually create an account without opprotunity and try to delete it(enable debug logs to see if any other logic is firing)
    please note deletion is sucessful or not.
2. Try to negate the asserts at line number 33 and 69(example:System.assert(!result.isSuccess()); and see if the test class passes.
if test class passes,then deffinitely there is some logic which prevents deletion of accounts.

 
This was selected as the best answer
Alex SidlinskiyAlex Sidlinskiy
Thanks Prasad Avala 6, I'll take try that
Alex SidlinskiyAlex Sidlinskiy
It turns out I had AccountDeletion trigger enabled that wouldn't allow deletion of accounts with an oppen opportunity. As soon as I disabled that the apex test passed.
RonVelzeboerRonVelzeboer
For people doing the Trailhead and run into this problem like me too. Please also make sure the AddRelatedRecord trigger is disabled. This trigger creates and attaches an Opportunity as soon as an Account is being created. If this trigger is active then you may think you create an Account with no Opportunities, but this trigger does and causes the test to fail.
RonVelzeboerRonVelzeboer
In addition to my previous comment. Opposed to what Alex said, you should leave the AccountDeletion trigger enabled, because thats what the TestAccountDeletion class is testing. Sure, disabling the AccountDeletion trigger will make those two test pass, but it would fail the others.