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
SalesforceUser11SalesforceUser11 

How to increase test coverage

Hi,

My test class is passed but test coverage is 0%. Can any one help to increase tets coverage:

APex Class: 

public class TopicOfInterestUserAsgmtDataAccessor implements TopicOfInterestUserAssignmentDAI {
    public List<Topic_of_Interest_User_Assignment__c> getSubscribedTopicsOfInterestByUserId(Id userId) {
        return [
            SELECT Id, 
                User__c, 
                TopicofInterest__c 
            FROM Topic_of_Interest_User_Assignment__c 
            WHERE User__c = :userId];
    }

    public List<Topic_of_Interest_User_Assignment__c> getSubscribedTopicsOfInterestLabelsByUserId(Id userId) {
        return [
            SELECT Id, 
                User__c, 
                toLabel(TopicofInterest__c) 
            FROM Topic_of_Interest_User_Assignment__c 
            WHERE User__c = :userId];
    }

    public void upsertUserTopicSelections(List<Topic_of_Interest_User_Assignment__c> newOrExistingTopics) {
        upsert newOrExistingTopics;    
    }

    public void deleteUserTopicSelections(List<Topic_of_Interest_User_Assignment__c> existingTopics) {
        delete existingTopics;
    }

}

Test Class:
@IsTest
private class TopicOfInterestUserAsgmtDataAccessorTest {

    @testSetup
    private static void testSetup() {
    }

    @isTest
   Private static void testgetSubscribedTopicsOfInterestByUserId() {

        Profile p = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
        User u = new User(Alias = 'standt', Email = 'standarduser@testorg.com',
                EmailEncodingKey = 'UTF-8', LastName = 'Testing', LanguageLocaleKey = 'en_US',
                LocaleSidKey = 'en_US', ProfileId = p.Id,
                TimeZoneSidKey = 'America/New_York', UserName = 'standarduser1@testorg.com');

        System.runAs(u) {

            Topic_of_Interest_User_Assignment__c tci = new Topic_of_Interest_User_Assignment__c();
            tci.User__c = u.Id;
            tci.TopicofInterest__c = 'Administrative';
            insert  tci;
            system.debug('**************tci'+tci);
        }
    }
}
Best Answer chosen by SalesforceUser11
Ravi Dutt SharmaRavi Dutt Sharma
Hi,

Below class should give you 100% coverage. Dont forget to add appropriate assert statments.
 
Private static void testgetSubscribedTopicsOfInterestByUserId() {

        Profile p = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
        User u = new User(Alias = 'standt', Email = 'standarduser@testorg.com',
                EmailEncodingKey = 'UTF-8', LastName = 'Testing', LanguageLocaleKey = 'en_US',
                LocaleSidKey = 'en_US', ProfileId = p.Id,
                TimeZoneSidKey = 'America/New_York', UserName = 'standarduser1@testorg.com');

        System.runAs(u) {

            Topic_of_Interest_User_Assignment__c tci = new Topic_of_Interest_User_Assignment__c();
            tci.User__c = u.Id;
            tci.TopicofInterest__c = 'Administrative';
            insert  tci;
            system.debug('**************tci'+tci);
			
			List<Topic_of_Interest_User_Assignment__c> tciList = new List<Topic_of_Interest_User_Assignment__c>();
			tciList.add(tci);
			
			TopicOfInterestUserAsgmtDataAccessor obj = new TopicOfInterestUserAsgmtDataAccessor ();
			obj.getSubscribedTopicsOfInterestByUserId(UserInfo.getUserId());
			obj.getSubscribedTopicsOfInterestLabelsByUserId(UserInfo.getUserId());
			obj.upsertUserTopicSelections(tciList);
			obj.deleteUserTopicSelections(tciList);
        }
    }
}

Please mark this as the best answer if this resolves your issue. Thanks.

All Answers

Ravi Dutt SharmaRavi Dutt Sharma
Hi,

Below class should give you 100% coverage. Dont forget to add appropriate assert statments.
 
Private static void testgetSubscribedTopicsOfInterestByUserId() {

        Profile p = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
        User u = new User(Alias = 'standt', Email = 'standarduser@testorg.com',
                EmailEncodingKey = 'UTF-8', LastName = 'Testing', LanguageLocaleKey = 'en_US',
                LocaleSidKey = 'en_US', ProfileId = p.Id,
                TimeZoneSidKey = 'America/New_York', UserName = 'standarduser1@testorg.com');

        System.runAs(u) {

            Topic_of_Interest_User_Assignment__c tci = new Topic_of_Interest_User_Assignment__c();
            tci.User__c = u.Id;
            tci.TopicofInterest__c = 'Administrative';
            insert  tci;
            system.debug('**************tci'+tci);
			
			List<Topic_of_Interest_User_Assignment__c> tciList = new List<Topic_of_Interest_User_Assignment__c>();
			tciList.add(tci);
			
			TopicOfInterestUserAsgmtDataAccessor obj = new TopicOfInterestUserAsgmtDataAccessor ();
			obj.getSubscribedTopicsOfInterestByUserId(UserInfo.getUserId());
			obj.getSubscribedTopicsOfInterestLabelsByUserId(UserInfo.getUserId());
			obj.upsertUserTopicSelections(tciList);
			obj.deleteUserTopicSelections(tciList);
        }
    }
}

Please mark this as the best answer if this resolves your issue. Thanks.
This was selected as the best answer
SalesforceUser11SalesforceUser11
Hi Ravi,

Thanks! Code covered by 100%. Really thanks for help.