You need to sign in to do that
Don't have an account?
sfuser12
Cover test class for sobjecttype
Hi,
I am writing test class which is 57% covered. But not getting how to cover if (this.taskWhoId.getSobjectType() == Contact.SObjectType)
public String getNameWhoId() {
if (String.isBlank(this.taskWhoId))
return null;
else
if (this.taskWhoId.getSobjectType() == Contact.SObjectType)
return [select name from Contact where id = :taskWhoId limit 1].name;
else
return null;
}
Test Class:
@TestSetup static void setup()
{
Account account = new Account();
account.name='Test Account';
account.Phone = '9872345617';
account.Type = 'Prospect';
account.BillingCity='Testcity';
account.BillingStateCode='IL';
account.BillingCountryCode='US';
insert account;
Contact contact = new Contact();
contact.FirstName = 'firstName';
contact.LastName = 'lastName';
contact.Accountid= account.Id;
contact.Email__c = 'xyz@testorg12.com';
contact.MailingCity = 'Testcity';
contact.MailingStateCode='IL';
contact.MailingCountryCode='US';
insert contact;
Task task = new Task();
task.Type = 'Email';
task.Description = 'Test Description';
task.OwnerId = UserInfo.getUserId();
task.WhatId = account.Id;
task.WhoId = contact.Id;
task.Primary__c = 'Survey';
task.Call_Topic__c = 'ActiveDisclosure';
task.Status = 'New';
task.Subject = 'Test Subject';
task.Priority = 'Low';
insert task;
}
@isTest static void testgetNameWhoId() {
List<Task> tasks = [Select Id from task limit 1];
CCSelfFilingTrainingController getIds = new CCSelfFilingTrainingController();
getIds.getNameWhoId();
getIds.getNameWhatId();
Can anyone tell me how to cover if condition in this case?
I am writing test class which is 57% covered. But not getting how to cover if (this.taskWhoId.getSobjectType() == Contact.SObjectType)
public String getNameWhoId() {
if (String.isBlank(this.taskWhoId))
return null;
else
if (this.taskWhoId.getSobjectType() == Contact.SObjectType)
return [select name from Contact where id = :taskWhoId limit 1].name;
else
return null;
}
Test Class:
@TestSetup static void setup()
{
Account account = new Account();
account.name='Test Account';
account.Phone = '9872345617';
account.Type = 'Prospect';
account.BillingCity='Testcity';
account.BillingStateCode='IL';
account.BillingCountryCode='US';
insert account;
Contact contact = new Contact();
contact.FirstName = 'firstName';
contact.LastName = 'lastName';
contact.Accountid= account.Id;
contact.Email__c = 'xyz@testorg12.com';
contact.MailingCity = 'Testcity';
contact.MailingStateCode='IL';
contact.MailingCountryCode='US';
insert contact;
Task task = new Task();
task.Type = 'Email';
task.Description = 'Test Description';
task.OwnerId = UserInfo.getUserId();
task.WhatId = account.Id;
task.WhoId = contact.Id;
task.Primary__c = 'Survey';
task.Call_Topic__c = 'ActiveDisclosure';
task.Status = 'New';
task.Subject = 'Test Subject';
task.Priority = 'Low';
insert task;
}
@isTest static void testgetNameWhoId() {
List<Task> tasks = [Select Id from task limit 1];
CCSelfFilingTrainingController getIds = new CCSelfFilingTrainingController();
getIds.getNameWhoId();
getIds.getNameWhatId();
Can anyone tell me how to cover if condition in this case?
test.StartTest();
Task tasks = [Select Id, WhoId, WhatId from task Where Subject = 'Test Subject'];
CCSelfFilingTrainingController getIds = new CCSelfFilingTrainingController();
getIds.getNameWhoId();
getIds.getNameWhatId();
getIds.taskWhoId = tasks.WhoId;
getIds.taskWhatId = tasks.Whatid;
getIds.getNameWhoId();
getIds.getNameWhatId();
test.StopTest();
}
All Answers
Thanks! But, its not increasing using this
test.StartTest();
Task tasks = [Select Id, WhoId, WhatId from task Where Subject = 'Test Subject'];
CCSelfFilingTrainingController getIds = new CCSelfFilingTrainingController();
getIds.getNameWhoId();
getIds.getNameWhatId();
getIds.taskWhoId = tasks.WhoId;
getIds.taskWhatId = tasks.Whatid;
getIds.getNameWhoId();
getIds.getNameWhatId();
test.StopTest();
}