You need to sign in to do that
Don't have an account?
Alex 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.
Here what the code looks like:
Any help much appreciated.
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.
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
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!
That is correct. The TestDeleteBulkAccountsWithNoOpportunities and TestDeleteAccountWithNoOpportunities are failing on the assertions on lines 69 and 33.
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
TestDeleteBulkAccountsWithNoOpportunities: Class.TestAccountDeletion.TestDeleteBulkAccountsWithNoOpportunities: line 69, column 1
TestDeleteAccountWithNoOpportunities: Class.TestAccountDeletion.TestDeleteAccountWithNoOpportunities: line 33, column 1
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.