0

I've a simple apex class as below.

public class ChildParentRecords {
    @AuraEnabled
    public static void getDta() {
        String createChild='SELECT Id, Name FROM Account LIMIT 1';
        
        List<Sobject> children = Database.query(createChild);
        System.debug(children.size());
    }
}

And I wrote a test class for the same as below.

@isTest
public with sharing class GetChildAndParentsTest {
    static testMethod void testgetRelDets(){
        String createChild='SELECT Id, Name FROM Account LIMIT 1';
       
        List<Sobject> children = Database.query(createChild);
        System.assertEquals(1, 1);
    }
}

when I am running this test class, it is passed(as I've given 1, 1 in assert Equals), when I go to the code coverage it shows none.

I'm very confused on where I'm going wrong. Please let me know how to fix this.

Thanks

enter image description here

1
  • 1
    The line 6 seems to be wrong because you are supposed to call your class and also need to insert test data. Once you create a test account then you will need to replace line 6 of test class by ChildParentRecords.getDta(); Please go through this trail to better understand this. trailhead.salesforce.com/en/content/learn/modules/apex_testing/…
    – manjit5190
    Jul 31, 2020 at 18:51

2 Answers 2

3

You're not supposed to duplicate the logic of the thing being tested in the test itself.

The test method is supposed to set up the environment to allow your SUT (system under test) to be run, and then gather results so you can make assertions to verify that the output is what you expect.

You only gain coverage for code that is executed as part of a test. Since your test method is not calling ChildParentRecords.getDta(), it doesn't get coverage.

How do I write an Apex unit test and How do I increase my code coverage, or why can't I cover these lines contain good information and resources, and you should go through them if you haven't already.

0

The test you wrote, does not invoke the code in the class.

You should write in the test:

ChildParentRecords.getDta();

You must log in to answer this question.

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