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

Test class for Sobject apex
Hi,
I am getting 41% code coverage in my apex controller.
public class ContactTaskOnApplication {
@AuraEnabled
public static List<Task> getContactTask(Id recordId,String contactAPIName){
if(recordId==null || contactAPIName==null){
return null;
}
String sObjName = recordId.getSObjectType().getDescribe().getName();
string query='SELECT Id, ' +contactAPIName+ ' FROM '+sObjName+ ' WHERE Id=:recordId';
List<sObject> sObjectList=database.query(query);
system.debug('sObjectList ' +sObjectList);
if (!sObjectList.isEmpty()){
id contactId=(Id)sObjectList[0].get(contactAPIName);
List<Task> tsk=[SELECT Id,Subject,Priority,ActivityDate,Due_Date__c,
whoId FROM task WHERE whoId=:contactId];
system.debug('tsk '+tsk);
return tsk;
}
return null;
}
}
Test class
====
@isTest
public class TestContactTaskOnApplication {
public static Account partnerAccount;
public static Contact studentContact;
public static Application__c application;
public static RFI__c rfi;
public static Task task;
static testmethod void testContactTaskOnApplicationMethod() {
try{
Id idRecordId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Educational_Institution').getRecordTypeId();
//Insert Account
partnerAccount = new Account(Name='Gwendwyr Test', RecordTypeId=idRecordId);
insert partnerAccount;
//Insert Contact
studentContact = new Contact(LastName='Someguy', firstname='Someguy' , email='test@test.com',hed__Citizenship__c='Canada');
insert studentContact;
//Insert Application
application=new Application__c(Contact__c=studentContact.id,Partner_Account_Application__c=partnerAccount.id);
insert application;
//Insert RFI
rfi= new RFI__c (Email__c='testrfi@somerfi.com',Last_Name__c='Test RFI',First_Name__c='Test RFI',Contact__c=studentContact.id );
insert rfi;
//Insert Task
task=new Task(Subject='Call',whoId=studentContact.id,ActivityDate=system.today(),Priority ='Normal',Due_Date__c=system.today()-1);
insert task;
ContactTaskOnApplication.getContactTask(application.id,application.Contact__c);
ContactTaskOnApplication.getContactTask(rfi.id,rfi.Contact__c);
//application.id, application.Contact__c
}
catch(exception e){
}
}
}
I am getting 41% code coverage in my apex controller.
public class ContactTaskOnApplication {
@AuraEnabled
public static List<Task> getContactTask(Id recordId,String contactAPIName){
if(recordId==null || contactAPIName==null){
return null;
}
String sObjName = recordId.getSObjectType().getDescribe().getName();
string query='SELECT Id, ' +contactAPIName+ ' FROM '+sObjName+ ' WHERE Id=:recordId';
List<sObject> sObjectList=database.query(query);
system.debug('sObjectList ' +sObjectList);
if (!sObjectList.isEmpty()){
id contactId=(Id)sObjectList[0].get(contactAPIName);
List<Task> tsk=[SELECT Id,Subject,Priority,ActivityDate,Due_Date__c,
whoId FROM task WHERE whoId=:contactId];
system.debug('tsk '+tsk);
return tsk;
}
return null;
}
}
Test class
====
@isTest
public class TestContactTaskOnApplication {
public static Account partnerAccount;
public static Contact studentContact;
public static Application__c application;
public static RFI__c rfi;
public static Task task;
static testmethod void testContactTaskOnApplicationMethod() {
try{
Id idRecordId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Educational_Institution').getRecordTypeId();
//Insert Account
partnerAccount = new Account(Name='Gwendwyr Test', RecordTypeId=idRecordId);
insert partnerAccount;
//Insert Contact
studentContact = new Contact(LastName='Someguy', firstname='Someguy' , email='test@test.com',hed__Citizenship__c='Canada');
insert studentContact;
//Insert Application
application=new Application__c(Contact__c=studentContact.id,Partner_Account_Application__c=partnerAccount.id);
insert application;
//Insert RFI
rfi= new RFI__c (Email__c='testrfi@somerfi.com',Last_Name__c='Test RFI',First_Name__c='Test RFI',Contact__c=studentContact.id );
insert rfi;
//Insert Task
task=new Task(Subject='Call',whoId=studentContact.id,ActivityDate=system.today(),Priority ='Normal',Due_Date__c=system.today()-1);
insert task;
ContactTaskOnApplication.getContactTask(application.id,application.Contact__c);
ContactTaskOnApplication.getContactTask(rfi.id,rfi.Contact__c);
//application.id, application.Contact__c
}
catch(exception e){
}
}
}
Greetings!
Please find the sample code in the below document which might help you in increasing the code coverage:
http://www.sfdcpoint.com/salesforce/test-class-with-example-salesforce/
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.
Warm Regards,
Shirisha Pathuri