0

I have created a LWC component, it has 1 Apex class that is test covered at 100%. However when I try to create my package, it fails saying "No test methods found in the Apex code included in the package. At least 75% test coverage is required." for my class.

When I run sf apex run test --wait 100 --code-coverage I get ComunikAppletApexClass 100%, ComunikAppletApexClass being the Apex class from my component.

Here's my test class, it is contained in a file located in the same folder as my Apex class.

@isTest
private class TestComunikAppletApexClass {
    @isTest static void testCreateCallLog() {
        Account acct = new Account(Name='Test Account');
        insert acct;
        
        Contact contact = new Contact(LastName='Test Contact', AccountId=acct.Id);
        insert contact;
        
        Task callLogReturned = ComunikAppletApexClass.createCallLog(contact, 'test', 'Outbound', 'http://test.url/');
        
        Task callLog = [
            SELECT 
                Id, WhoId, Description, Priority, Status, Subject, CallType, Type, TaskSubtype
            FROM Task 
            WHERE Id = :callLogReturned.Id
        ];
        System.assertEquals(callLog.WhoId, contact.Id);
        System.assertEquals(callLog.Description, 'http://test.url/');
        System.assertEquals(callLog.Priority, 'Normal');
        System.assertEquals(callLog.Status, 'Completed');
        System.assertEquals(callLog.Subject, 'test');
        System.assertEquals(callLog.CallType, 'Outbound');
        System.assertEquals(callLog.Type, 'Call');
        System.assertEquals(callLog.TaskSubtype, 'Call');
    }

    @isTest static void testGetContactByPhoneNumber() {
        Account acct = new Account(Name='Test Account');
        insert acct;
        
        Contact contact = new Contact(LastName='Test Contact', AccountId=acct.Id, Phone='1234567890');
        insert contact;
        
        Contact contactReturned = ComunikAppletApexClass.getContactByPhoneNumber('1234567890');
        
        System.assertEquals(contactReturned.Id, contact.Id);
    }

    private static final String FAKE_RESPONSE_JSON = '{"status": "ok"}';

    private class Mock implements HttpCalloutMock {
        public HTTPResponse respond(HTTPRequest req) {
            HTTPResponse res = new HTTPResponse();
            res.setBody(FAKE_RESPONSE_JSON);
            res.setStatusCode(200);
            return res;
        }
    }

    @isTest static void testLoginUser() {
        Test.setMock(HttpCalloutMock.class, new Mock());

        Test.startTest();
        String responseJSON = ComunikAppletApexClass.loginUser('[email protected]', 'test');
        Test.stopTest();

        System.assertEquals(FAKE_RESPONSE_JSON, responseJSON);
    }
}

I feel like I am doing something wrong but I don't understand what.

3
  • did you added test class also in package? Commented Jan 8 at 15:37
  • @ManjotSingh ok yeah, for some reason the only thing not automatically added to the package when I added my component was the test class. Is this normal?
    – SamPerron
    Commented Jan 8 at 15:50
  • 2
    If this is a 1GP package that is normal. For a 2GP, just being in the same packageDirectory means it is packaged (unless you have some .forceignore setup).
    – Phil W
    Commented Jan 8 at 16:41

1 Answer 1

1

For a 1GP package you must explicitly add unit test classes (and the Apex classes, unless they are a dependency for one or more class already in the package). You do this in the Package Manager in Setup on the packaging org.

For a 2GP, just being in the same packageDirectory means it is packaged (unless you have some .forceignore setup).

You must log in to answer this question.

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