-2

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);
        }
    }
}
  • 4
    You're not actually calling the class you want to test, just inserting a record. Is that class called from a trigger? Try directly calling the class instead of inserting records. – Folkert Aug 24 '18 at 10:33
1

I would make all your utility methods static:

public class TopicOfInterestUserAsgmtDataAccessor implements TopicOfInterestUserAssignmentDAI {
    public static 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 static 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 static void upsertUserTopicSelections(List<Topic_of_Interest_User_Assignment__c> newOrExistingTopics) {
        upsert newOrExistingTopics; 
    }

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

}

Your test class never calls anything from the class you're trying to test.

I also like to use static initialization code in my tests to organize the repetitive parts of the tests:

@IsTest
private class TopicOfInterestUserAsgmtDataAccessorTest {
    public static User testUser;
    public static List<Topic_of_Interest_User_Assignment__c> toiuas;

    static {
        // This pattern prevents list has no rows errors when trying to
        // query for data created in the test, If using a pre-existing user
        // you don't need this pattern
        for (User u : [
            SELECT Id
            FROM User
            WHERE Alias = 'standt'
        ]) {
            testUser = u;
            break;
        }

        if (u != null) {
            toiuas = TopicOfInterestUserAsgmtDataAccessor.getSubscribedTopicsOfInterestByUserId(u.Id);
        }
    }

    @testSetup
    private static void testSetup() {
        Profile testUserProfile = [
            SELECT Id 
            FROM Profile 
            WHERE Name = 'System Administrator'
        ];

        testUser = new User(
            Alias = 'standt', 
            Email = 'standarduser@testorg.com', 
            EmailEncodingKey = 'UTF-8',
            LastName = 'Testing',
            LanguageLocaleKey = 'en_US',
            LocaleSidKey = 'en_US', 
            ProfileId = testUserProfile.Id,
            TimeZoneSidKey = 'America/New_York', 
            UserName = 'standarduser1@testorg.com'
        );

        insert testUser;

        toiuas = new List<Topic_of_Interest_User_Assignment__c>();

        for (Integer i = 0; i < 3; i++) {
            toiuas.add(new Topic_of_Interest_User_Assignment__c(
                // Populate required fields
                User__c = TestUser.id
            ));
        }

        insert toiuas;
    }

    @isTest
    Private static void deleteTest() {
        System.runAs(testUser) {
            TopicOfInterestUserAsgmtDataAccessor.deleteUserTopicSelections(toiuas);
        }
    }

    @isTest
    Private static void upsertTest() {
        System.runAs(testUser) {
            TopicOfInterestUserAsgmtDataAccessor.upsertUserTopicSelections(toiuas);
        }
    }

    @isTest
    Private static void get1Test() {
        System.runAs(testUser) {
            toiuas = TopicOfInterestUserAsgmtDataAccessor.getSubscribedTopicsOfInterestByUserId(UserInfo.getUserId());
            system.assert(!toiuas.isEmpty());
        }
    }

    @isTest
    Private static void get2Test() {
        System.runAs(testUser) {
            toiuas = TopicOfInterestUserAsgmtDataAccessor.getSubscribedTopicsOfInterestLabelsByUserId(UserInfo.getUserId());
            system.assert(!toiuas.isEmpty());
        }
    }
}

Don't forget to add assert statements to check that your code actually works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.