You need to sign in to do that
Don't have an account?
Pallavi Ghole
Create a unit test for a simple Apex trigger
Hello,
In the exercise "Create a unit test for a simple Apex trigger" , here is the code I wrote and for some reason it keeps giving me an Assertion failed
@ line 24. Can you please help determine the reason?
@isTest
private class TestRestrictContactByName
{
@isTest static void TestMethodRestrictContact() {
// Test data setup
// Create an Account and its contact with Last Name = INVALIDNAME
Account acct = new Account(Name='Test Account');
insert acct;
List <Contact> contactlist = new List <Contact>();
Contact cont1 = new Contact(FirstName='Test1',LastName='INVALIDNAME',Email='test1@invalid.com',AccountId=acct.Id);
contactlist.add(cont1);
Contact cont2 = new Contact(FirstName='Test2',LastName='VALIDNAME',Email='test2@valid.com',AccountId=acct.Id);
contactlist.add(cont2);
Test.startTest();
try {
insert contactlist;
}
Catch (DMLException exp) {
for(integer i = 0; i < 2; i++) {
//System.assert(exp.getMessage().contains('The Last Name "'+Contactlist[i].LastName+'" is not allowed for DML'));
System.assert(exp.getMessage().contains('The Last Name INVALIDNAME is not allowed for DML'));
}
}
Test.stopTest();
}
}
In the exercise "Create a unit test for a simple Apex trigger" , here is the code I wrote and for some reason it keeps giving me an Assertion failed
@ line 24. Can you please help determine the reason?
@isTest
private class TestRestrictContactByName
{
@isTest static void TestMethodRestrictContact() {
// Test data setup
// Create an Account and its contact with Last Name = INVALIDNAME
Account acct = new Account(Name='Test Account');
insert acct;
List <Contact> contactlist = new List <Contact>();
Contact cont1 = new Contact(FirstName='Test1',LastName='INVALIDNAME',Email='test1@invalid.com',AccountId=acct.Id);
contactlist.add(cont1);
Contact cont2 = new Contact(FirstName='Test2',LastName='VALIDNAME',Email='test2@valid.com',AccountId=acct.Id);
contactlist.add(cont2);
Test.startTest();
try {
insert contactlist;
}
Catch (DMLException exp) {
for(integer i = 0; i < 2; i++) {
//System.assert(exp.getMessage().contains('The Last Name "'+Contactlist[i].LastName+'" is not allowed for DML'));
System.assert(exp.getMessage().contains('The Last Name INVALIDNAME is not allowed for DML'));
}
}
Test.stopTest();
}
}
I guess it is because your assert statement is missing double quotes.
System.assert(exp.getMessage().contains('The Last Name "INVALIDNAME" is not allowed for DML'));
All Answers
Greetings to you!
Please try below code:
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
Thank you so much for the response, the class you have provided above is the one I have seen in multiple forums. While I can always use that class as a backup I am also trying to understand what is going wrong with the code I have written. Any help would be appreciated!
Regards
Pallavi
I guess it is because your assert statement is missing double quotes.
System.assert(exp.getMessage().contains('The Last Name "INVALIDNAME" is not allowed for DML'));
Thanks for your help.
@isTest
public class TestRestrictContactByName {
@isTest static void TestRestrictContactByName()
{
Contact con = new Contact(LastName = 'INVALIDNAME');
test.startTest();
Database.SaveResult result= database.insert(con,false);
test.stopTest();
system.assertEquals('The Last Name INVALIDNAME is not allowed for DML',
result.getErrors()[0].getMessage());
}
}
public class TestRestrictContactByName {
@isTest static void TestUpdateContact(){
//create one contact records
Contact objContact = new Contact(LastName = 'INVALIDNAME');
insert objcontact;
//varify test
Test.startTest();
Database.saveResult result = Database.insert(objContact,false);
Test.stopTest();
//verify
//In this case deletion should have been stopped by the trigger.
System.assert(!result.isSuccess());
System.assert(result.getErrors().size() > 0);
System.assertEquals('Cannot update the contact.',result.getErrors()[0].getMessage());
}
}
Usually this problem occurs when the written test is not run. After writing the test, run it at least 1 time and try.
# SOLUTION
1. In the Developer Console, click **File** | **New** | **Apex Class**, and enter **VerifyDate** for the class name, and then click **OK**.
2. Replace the default class body with the following.
3. Press Ctrl+S to save your class.
4. In the Developer Console, click **File** | **New** | **Apex Class**, and enter **TestVerifyDate** for the class name, and then click **OK**.
5. Replace the default class body with the following.
6. Press Ctrl+S to save your class.
7. In the Developer Console, click **Test** | **New Run**.
8. Under **Test Classes**, click **TestVerifyDate**.
9. To add all the test methods in the **TestVerifyDate** class to the test run, click **Add Selected**.
10. Click **Run**.
11. In the Tests tab, you see the status of your tests as they’re running. Expand the test run, and expand again until you see the list of individual tests that were run. They all have green checkmarks.