0

I have a test class that calls methods from a class and asserts that the work done by each method meets the expected result. For some reason, even though a message is displayed saying that all test have passed when the test class is run, the code coverage for the class is still at 0%. This can be see my looking in the top tab bar in the dev console as well as by running the following query:

SELECT ApexClassOrTrigger.Name, NumLinesCovered, NumLinesUncovered
FROM ApexCodeCoverageAggregate WHERE ApexClassOrTrigger.Name = 'TestCreateCampaign'

As was outlined in the answer of this post, I made sure to actually call the methods directly from the original class. I think that there possibly could be an issue with the fact that the original class is a static class with static methods, but I'm not sure if that would prevent the system from detecting the code coverage.

What could be the issue here?

The prototypes for the original class and its methods/attributes as well as the code for the test class are below:

original class:

public class CreateCampaign {
    class CampaignFrequency{}
    public class AvailableMailing{}
    class CampaignObj{}
    public static string reformatString(String oldString){}
    public static List<AvailableMailing> getAvailableMailings(){}
    public static List<Campaign> upsertCampaigns(){}
    public static List<CampaignMember> addCampaignMembers(){}
}

test class

@isTest
private class TestCreateCampaign {
   static testMethod void runPositiveTestCases() {

        //Initial test to verify that all three available mailings are selected to be used to create campaigns
        System.debug('Verifying available mailings  ');
        CreateCampaign camp = new CreateCampaign();
        camp.reformatString('test');
        List<CreateCampaign.AvailableMailing> availMailings =  CreateCampaign.getAvailableMailings();
        Integer origIdsCount = availMailings.size();
        List<Available_Mailings__c> testAvailMailings = [SELECT Id , Create_Campaign__c , Name, Mailing_Type__c, Frequency__c FROM Available_Mailings__c WHERE Create_Campaign__c = TRUE];
        Integer testIdsCount = testAvailMailings.size();
        for(Available_Mailings__c amc_t : testAvailMailings){
            for(CreateCampaign.AvailableMailing amc : availMailings){
                if(amc.MailingId == amc_t.Id){
                   --origIdsCount;
                   --testIdsCount;
                }
        }
    }

       System.assertEquals(origIdsCount, 0);
       System.assertEquals(testIdsCount, 0);


        //To test campaigns inserted, manually derive column (mailing_id+ _ + campaigin_key)
        //and check against the variable Campaign_Key__c in the campaign model
        System.debug('Verifying campaign creation');
       CreateCampaign.upsertCampaigns();
        List<Campaign> campaigns = [SELECT Id , Campaign_Key__c FROM Campaign WHERE Campaign_Key__c <> ''];
       origIdsCount = availMailings.size();
       for(CreateCampaign.AvailableMailing amc : availMailings){
           for(Campaign cmp : campaigns){
               if(amc.Campaign_Key == cmp.Campaign_Key__c){
                   --origIdsCount;
               }
           }
       }
       System.assertEquals(origIdsCount, 0);

        //Test if the campaign members were added to the appropriate campaigns
        System.debug('Verifying addition of campaign members to campaigns');
        List<CampaignMember> members = CreateCampaign.addCampaignMembers();
        Integer numMembers = members.size();
        for(CampaignMember cm : members){
            for(Campaign cmp : campaigns){
                if(cmp.Campaign_Key__c == cm.CampaignId){
                    numMembers--;
                }
            }
        }
        System.assertEquals(numMembers, 0);
    }

}
  • Shot in the dark, but I believe they changed test code syntax. Maybe change: "static testMethod void runPositiveTestCases() {" to "@isTest static void runPositiveTestCases() {" – Evan Feb 20 '18 at 21:35
  • @EvanRothstein It's still passing just the same as before. – loremIpsum1771 Feb 20 '18 at 21:48
1

Do you have your "Apex Test Execution Options" set so that "Store Only Aggregated Code Coverage" is turned off?

Go to https://test.salesforce.com/ui/setup/apex/ApexTestQueuePage?retURL=%2Fui%2Fsetup%2FSetup%3Fsetupid%3DDevToolsIntegrate&setupid=ApexTestQueue (replace "test." at the beginning with "login." for production) and click the "Options" button to be sure.

Note that if you leave it turned on in Production, you'll have to remember to turn it back off every time you refresh a sandbox.

| improve this answer | |
  • Yes, "Store Only Aggregated Code Coverage" is turned off. Actually, when I check the "Apex Test Execution" list, I see that the test class actually failed when I tried to assert 5==0 it was just that for some reason, in the dev console, a success message was being returned when I executed the class. I'm not sure why the code coverage was 0% when the test was passing though. – loremIpsum1771 Feb 20 '18 at 20:20
0

Maybe is a problem with the code coverage calculation but not with the unit test.

Please, double check this known issue:

https://success.salesforce.com/issues_view?id=a1p30000000T5k3AAC

Also please confirm that the class is being covered using the way "line-by-line code coverage" that you can find here:

https://help.salesforce.com/articleView?id=code_dev_console_tests_coverage.htm&type=5

| improve this answer | |
  • I'm just selecting the option "New Run: from the "Test" tab in the dev console. Is there another way to run the test class? – loremIpsum1771 Feb 20 '18 at 19:52
  • Yes you can do it under Apex Test Execution: help.salesforce.com/… – JaviCC82 Feb 20 '18 at 20:10
  • Right, I've done it there before. Is there a way to manually check for code coverage? – loremIpsum1771 Feb 20 '18 at 20:21
  • Yes, you can use the Overall Code Coverage panel: help.salesforce.com/…. Also, make sure that nobody is updating the class, when a class is updated, the coverage is removed for that class. – JaviCC82 Feb 20 '18 at 20:36
  • Idk, in the code coverage menu, there is only the option of "None" but the result of the test on the "Apex Test Execution" page is "(1/1) Test Methods Passed" so I'm not really sure what is wrong with test class. – loremIpsum1771 Feb 20 '18 at 20:51

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.