function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfuser12sfuser12 

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?
Best Answer chosen by sfuser12
sfuser12sfuser12
This is covered:

 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

Raj VakatiRaj Vakati
Try like this
 
@isTest static void testgetNameWhoId() {
      
        CCSelfFilingTrainingController getIds = new CCSelfFilingTrainingController();
        getIds.getNameWhoId();
        getIds.getNameWhatId();

 
sfuser12sfuser12
Hi Raj,

Thanks! But, its not increasing using this
sfuser12sfuser12
This is covered:

 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();
}
This was selected as the best answer