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
Tina Chang 6Tina Chang 6 

"System.AssertException: Assertion Failed" error in my test class

Hello, there! I've checked all similar posts but still wasn't able to solve the problem. This is a pretty simple trigger from Trailhead:
trigger RestrictContactByName on Contact (before insert, before update) {
	
	//check contacts prior to insert or update for invalid data
	For (Contact c : Trigger.New) {
		if(c.LastName == 'INVALIDNAME') {	//invalidname is invalid
			c.AddError('The Last Name "'+c.LastName+'" is not allowed for DML.');
		}

	}
}
This is my test class. The test class can get me 100% code coverage and the UI operation all works beautifully. The issue is the test result returned "System.AssertException: Assertion Failed" error at Line 15. Can anyone please explain to me why this is happening? Is it about Database opertion or DML statements? Thank you so much!
@isTest
private class TestRestrictContactByName {
    
     @isTest static void TestLastNameIsvalid() {	
        // Test data setup
        // Create a contact with a valid Last Name, and then try to insert/update it.
 		Contact c = new Contact (FirstName='Kathy',LastName='Smith',Department='Technology');
        insert c;
        // Perform test
        Test.startTest();
        Database.SaveResult result = Database.insert(c, false);
        Test.stopTest();
        // Verify 
        // In this case the insert/update should have been processed.
        System.assert(result.isSuccess());
    }          
    
    @isTest static void TestLastNameIsNotInvalid() {
        // Test data setup
        // Create a contact with a Last Name of 'INVALIDNAME', and then try to insert/update it.
        Contact c2 = new Contact (FirstName='Joe',LastName='INVALIDNAME',Department='Finance');
        insert c2;
        // Perform test
        Test.startTest();
        Database.SaveResult result = Database.insert(c2, false);
        Test.stopTest();
        // Verify 
        // In this case the deletion should have been stopped by the trigger,
        // so verify that we got back an error.
        System.assert(!result.isSuccess());
        System.assert(result.getErrors().size() > 0);
        System.assertEquals('The Last Name "INVALIDNAME" is not allowed for DML.',
                             result.getErrors()[0].getMessage());
    }   
}





 
Best Answer chosen by Tina Chang 6
Alain CabonAlain Cabon
Hi,

Just remove the two extra "insert"

insert c;
insert c2;

There are inserts twice each time and the second ones have an Id and failed .

When you want to know the reasons, just add the code below:
if (!result.isSuccess()) {
        // Operation failed, so get all errors                
        for(Database.Error err : result.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Fields that affected this error: ' + err.getFields());
        }
}

All Answers

Sampath SuranjiSampath Suranji
Hi, 
You have used two insert options(insert  and Database.insert)for the  same object(c), please comment the line no 08

Regards
Sampath
Alain CabonAlain Cabon
Hi,

Just remove the two extra "insert"

insert c;
insert c2;

There are inserts twice each time and the second ones have an Id and failed .

When you want to know the reasons, just add the code below:
if (!result.isSuccess()) {
        // Operation failed, so get all errors                
        for(Database.Error err : result.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Fields that affected this error: ' + err.getFields());
        }
}
This was selected as the best answer
Narender Singh(Nads)Narender Singh(Nads)

Hi,
You are trying to insert same record twice, line 8 and line 11 . That is the reason assertion fails because how can you insert a record which is already inserted in the database. Comment out your line 8 and try testing it again. It should work.

Btw, same is the case with your second function named 'TestLastNameIsNotInvalid'. Do the same for this as well.

Thanks.

Amit Chaudhary 8Amit Chaudhary 8

Try to update your code like below
@isTest
private class TestRestrictContactByName {
    
     @isTest static void TestLastNameIsvalid() {	
 		Contact c = new Contact (FirstName='Kathy',LastName='Smith',Department='Technology');
        //insert c;
        Test.startTest();
		
			Database.SaveResult result = Database.insert(c, false);
			
        Test.stopTest();
        System.assert(result.isSuccess());
    }          
    
    @isTest static void TestLastNameIsNotInvalid() {
        Contact c2 = new Contact (FirstName='Joe',LastName='INVALIDNAME',Department='Finance');
        //insert c2;
        
		Test.startTest();
		
			Database.SaveResult result = Database.insert(c2, false);
			
        Test.stopTest();
		
        System.assert(!result.isSuccess());
        System.assert(result.getErrors().size() > 0);
        System.assertEquals('The Last Name "INVALIDNAME" is not allowed for DML.',
                             result.getErrors()[0].getMessage());
    }   
}

Let us know if this will help u
 
Tina Chang 6Tina Chang 6
Guys! Thank you so so much for your assistance. I've just started the developer portion of my Salesforce knowledge base and all your responses helped me a lot. It's a shame that I could only choose one Best Answer. I gave it to @Alain Cabon for the additional piece of information. You guys are amazing. Thank you.