You need to sign in to do that
Don't have an account?
Mike Tol 1
Tests for add and delete contact
Hi!
Please help me to write negative and positive tests for two methods:
@AuraEnabled
public static void deleteContact(Id recordId) {
List<Contact> contact =
[SELECT Id FROM Contact WHERE Id = :recordId];
delete contact;
}
@AuraEnabled
public static void addContact() {
Contact contact = new Contact();
try {
insert contact;
} catch (exception ex) {
throw new auraHandledException(ex.getMessage());
}
}
I tried, but it turns out something terrible...
@isTest
public static void deleteContact_success_deletedContactById() {
Contact newContact = new contact(LastName = 'Bab');
insert newContact;
System.debug('newContact:' + newContact);
List<Contact> deletedContact =
[SELECT Id FROM Contact WHERE LastName = 'Bab'];
System.debug('deletedContact:' + deletedContact);
delete deletedContact;
Test.startTest();
List<Contact> Contact = ContactTableProjectSecond.deleteContact(recordId);
Test.stopTest();
System.assertEquals(deletedContact, contact, 'Contacts not searched');
}
@isTest
public static void addContact_success_addedContact() {
Contact newContact = new contact(LastName = 'Sam');
insert newContact;
List<Contact> addedContact = [SELECT Id, LastName FROM Contact WHERE LastName = 'Sam'];
System.debug('addedContact:' + addedContact);
Test.startTest();
List<Contact> contact = ContactTableProjectSecond.addContact();
Test.stopTest();
System.debug('contact:' + contact);
System.assertEquals(contact, addedContact, 'Contact not added');
}
Please help me to write negative and positive tests for two methods:
@AuraEnabled
public static void deleteContact(Id recordId) {
List<Contact> contact =
[SELECT Id FROM Contact WHERE Id = :recordId];
delete contact;
}
@AuraEnabled
public static void addContact() {
Contact contact = new Contact();
try {
insert contact;
} catch (exception ex) {
throw new auraHandledException(ex.getMessage());
}
}
I tried, but it turns out something terrible...
@isTest
public static void deleteContact_success_deletedContactById() {
Contact newContact = new contact(LastName = 'Bab');
insert newContact;
System.debug('newContact:' + newContact);
List<Contact> deletedContact =
[SELECT Id FROM Contact WHERE LastName = 'Bab'];
System.debug('deletedContact:' + deletedContact);
delete deletedContact;
Test.startTest();
List<Contact> Contact = ContactTableProjectSecond.deleteContact(recordId);
Test.stopTest();
System.assertEquals(deletedContact, contact, 'Contacts not searched');
}
@isTest
public static void addContact_success_addedContact() {
Contact newContact = new contact(LastName = 'Sam');
insert newContact;
List<Contact> addedContact = [SELECT Id, LastName FROM Contact WHERE LastName = 'Sam'];
System.debug('addedContact:' + addedContact);
Test.startTest();
List<Contact> contact = ContactTableProjectSecond.addContact();
Test.stopTest();
System.debug('contact:' + contact);
System.assertEquals(contact, addedContact, 'Contact not added');
}
Try below test Class Please Mark It As Best Answer If It Helps
Thank You!
What is the error you are getting?
Thanks!!
Thank youа CharuDutt! But the test does not cover the code.
Can you please share screen shot of class and with code coverage % in rightside of bottom in developer console.
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
It's quite simple test class should be execute, i have tested this code in my org it's working. Please follow below url
https://atrium.ai/resources/how-to-test-code-coverage-in-salesforce-useful-tips-and-techniques/
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh