0

I am trying to write a test class for a apex class that contains multiple external objects. I`ve tried the answer in this link. Although test is passed, the code coverage is only 37%. How can I improve code coverage?

SimpleExternal.cls

public class SimpleExternal {
     public class WrapperClass {
        @AuraEnabled
        public String ExternalId { get; set; }
        @AuraEnabled
        public Decimal Quantity { get; set; }

        public WrapperClass(String ExternalId, Decimal Quantity) {
            this.externalId = ExternalId;
            this.Quantity = Quantity;
        }
    }

    public static List<WrapperClass> getOrders(Id recordId){
        List<WrapperClass> records = new List<WrapperClass>();
        List<Order__x> OrderRecords = Query.records([SELECT ExternalId, Id FROM Order__x LIMIT 5]);
        System.debug('Order__x ' + OrderRecords);
        for (Order__x orderRecord : OrderRecords) {
            List<OrderDetail__x> Orderdetailrecords = Query.records([SELECT ExternalId, Id, quantity__c FROM OrderDetail__x WHERE ExternalId = '1005-2']);
            System.debug('OrderDetail__x ' + Orderdetailrecords);
            WrapperClass wrapper = new WrapperClass(
                    record.ExternalId,
                    Orderdetailrecords[0].quantity__c
                );
                records.add(wrapper);
            }
                return records;
        }
}

Query.cls(Same with the link)

public virtual inherited sharing class Query {
    public static List<SObject> records(List<SObject> records) {
        return instance.passThrough(records);
    }

    static Query instance = new Query();
    @TestVisible static void setMock(Query mock) {
         instance = mock;
         }

    protected virtual List<SObject> passThrough(List<SObject> records)
    {
        return records;
    }
}

SimpleExternalTest.cls

@IsTest
class SimpleExternalTest {
    class Mock extends Query{
        final Map<SObjectType, List<SObject>> cache;
    public Mock()
    {
        cache = new Map<SObjectType, List<SObject>>();
    }
    public Mock setDataStore(List<SObject> records)
    {
        cache.put(records.getSObjectType(), records);
        return this;
    }
}
    @IsTest static Void testStuff() {
        List<Order__x> mockRecords = new List<Order__x>();
        mockRecords.add(new Order__x(
            externalId = '123'
        ));
        List<OrderDetail__x> mockDetail = new List<OrderDetail__x>();
        mockDetail.add(new OrderDetail__x(
            externalId = '123',
            quantity__c = 5
        ));
        Mock mock = new Mock();
    mock.setDataStore(mockRecords);
    mock.setDataStore(mockDetail);

        Account acc = new account(name = 'Test Account123');
        insert acc;
        Test.startTest();
            SimpleExternal.getOrders(acc.Id);
        Test.stopTest();

        System.assertEquals(1, mockRecords.size());
    }
}

1 Answer 1

0

After you've defined the mock instance, you need to actually set the mock for use by your code:

Query.setMock(mock);

At the moment you've constructed an instance of this, but haven't actually used it. This is mentioned in the answer to the question you have linked. I'm assuming that this means that your test coverages ends at the for loop in your ExternalSimple.getOrders method.

Another thing that's wrong with the test class is the assertion as well. You're asserted that the list of mock records you've created in your test is of size 1, when in fact you should be testing that the getOrders method returned a non-zero length list, for example:

Test.startTest();
    List<WrapperClass> returnedRecords = SimpleExternal.getOrders(acc.Id);
Test.stopTest();

System.assertEquals(1, returnedRecords.size());

You must log in to answer this question.

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