You need to sign in to do that
Don't have an account?

Test Class Required
Test Class is needed for following code
---------------------------------------------------------------------------------------
Description: Account Record with Name =”Joe”. Create associated
contacts.
****************************************************************************************/
public class AssosciatedClass {
public static void addRecord(){
try {
Account accountRecord = new Account(Name='Joe');
insert accountRecord;
ID acctID = accountRecord.ID; //Get Id of the new Record
Contact contactRecord = new Contact( // Add a contact to this account.
FirstName='Joe',
LastName='Smith',
AccountId=acctID);
insert contactRecord;
}
catch(DmlException e) {
System.debug('An unexpected error has occurred: ' + e.getMessage());
}
}
}
---------------------------------------------------------------------------------------
Description: Account Record with Name =”Joe”. Create associated
contacts.
****************************************************************************************/
public class AssosciatedClass {
public static void addRecord(){
try {
Account accountRecord = new Account(Name='Joe');
insert accountRecord;
ID acctID = accountRecord.ID; //Get Id of the new Record
Contact contactRecord = new Contact( // Add a contact to this account.
FirstName='Joe',
LastName='Smith',
AccountId=acctID);
insert contactRecord;
}
catch(DmlException e) {
System.debug('An unexpected error has occurred: ' + e.getMessage());
}
}
}
Please check this code:-
@isTest
private class AssosciatedClassTest {
@isTest static void testAddRecordInsertContactAndAccount() {
Test.startTest();
AssosciatedClass.addRecord();
Test.stopTest();
Contact contact = [SELECT Id FROM Contact LIMIT 1];
System.assertNotEquals(null, contact, 'insert contact');
}
}
But it will not cover catch part because there is no way to get this exception from testClass So if you want to cover this part also then write your code something like this. Just pass argument lastName in your method definition
public class AssosciatedClass {
public static void addRecord(String lastName){
try {
Account accountRecord = new Account(Name='Joe');
insert accountRecord;
ID acctID = accountRecord.ID; //Get Id of the new Record
Contact contactRecord = new Contact( // Add a contact to this account.
FirstName='Joe',
LastName=lastName,
AccountId=acctID);
insert contactRecord;
}
catch(DmlException e) {
System.debug('An unexpected error has occurred: ' + e.getMessage());
}
}
}
Hi Supriya
Try for this code.
=================================== class ===============================
Thanks.
Akshay