1

a strange situation I can't find an example of elsewhere. I have a test class for a batchable apex that is showing in the developer console window as available for code coverage. Haven't seen a test class in there before so I'm wondering if there is an issue with the class. When I run the tests it works as expected and for the relevant classes provides 100% coverage. Unsure of the problem or what could happen when it comes time to deploy.

Thanks!

@isTest
public class UpdateOMSAccountBatchTest 
{
    private static void createTestData() 
    {
       String orgId = 'testorgid';
       String userId = 'testuserid';
       String userEmailValid = '[email protected]';
       String userEmailInvalid = '[email protected]';
    
    List<Account> account = new List<Account> 
    {        
        new Account
            (
            Name = orgId,
            Org_Id__c = orgId
            )
    };
    insert account;
    
    List<Contact> contact = new List<Contact> 
    {        
        new Contact
            (
            AccountId = account[0].Id,
            LastName = userId + '_1',
            User_Id__c = userId + '_1'
            )
    };
    insert contact;
    
    List<OMS_Organization__c> omsOrganization = new List<OMS_Organization__c> 
    {
        new OMS_Organization__c
            (
            Name = orgId,
            Org_Id__c = orgId,
            Organization_Id__c = orgId
            )
    };
    insert omsOrganization;

    List<OMS_User__c> omsUser = new List<OMS_User__c> 
    {
        new OMS_User__c
            (
            OMS_Organization__c = omsOrganization[0].Id,
            Name = userId + '_1',
            User_Id__c = userId + '_1'
            )
    };
    insert omsUser;
        
   List<OMS_Account__c> omsAccount = new List<OMS_Account__c> 
    {
        new OMS_Account__c
            (
            OMS_Organization__c = omsOrganization[0].Id,
            OMS_User__c = omsUser[0].Id,
            Name = userId + '_1',
            Account_id__c = '8373635',
            Status__c = 'Active'
            )
    };
    insert omsAccount;
}

@isTest static void testLinkAccountToOmsOrganizationByOrgId() 
{
    createTestData();
    UpdateOMSAccountStatusWeekly omsBatch = new UpdateOMSAccountStatusWeekly();
    omsBatch.query = 'SELECT Status__c FROM OMS_Account__c WHERE Account_id__c = \'8373635\'';
    Database.executebatch(omsBatch);
}

@isTest static void testschedule() 
{
    Test.StartTest();
    UpdateOMSAccountScheduler testsche = new UpdateOMSAccountScheduler();
    String sch = '0 0 23 * * ?';
    system.schedule('Test status Check', sch, testsche );
    Test.stopTest();
}

} enter image description here

2
  • 3
    I feel like you may have accidentally run some tests with it while it wasn't annotated with isTest. Have you tried clearing your test coverage in Test | Clear Test Data in the Developer Console?
    – sfdcfox
    Commented Jan 26, 2023 at 16:59
  • Just tried that and the class remained in there :(. However, I think you're right that I did try something before it was annotated, but not sure why it's still there after clearing the test data.
    – KB2021
    Commented Jan 27, 2023 at 22:20

1 Answer 1

0

@KB2021, this is due to the createTestData method in your test class. This should be annotated with @testSetup rather than just being defined as a private method within the test class.

Details about this are available here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_testsetup.htm

1
  • 1
    I removed the test class. Then in developer console Test->Clear Test Data. Then refreshed the Developer Console page. Then created the test class again. Now it is removed from Overall Code Coverage. Commented Aug 13 at 16:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .