0

I'm literally pulling my hair out... I have this Apex Class:

public with sharing class ProviderAPI_Datatype {
    Public Class Customer {
        @AuraEnabled
        Public String CustomerName {get;set;}
        @AuraEnabled
        Public String SfID{get;set;}
        @AuraEnabled
        Public string TaxID{get;set;}
        @AuraEnabled
        Public string CustID{get;set;}
        @AuraEnabled
        Public Date LastPmtDate{get;set;}  

     }
}

And I have this test method

@isTest(seealldata=true)
public class ProviderAPI_Datatype_Test {

    static testMethod ProviderAPI_Datatype.Customer CustomerObj() {


        StaticResource sr = [SELECT body FROM StaticResource WHERE Name = 'Customer3281'];// - Need to get sample JSON from Orion system.
        String fullJson = sr.body.toString();


        ProviderAPI_Datatype.Customer CustomerResults = (ProviderAPI_Datatype.Customer)JSON.deserialize(fullJson, ProviderAPI_Datatype.Customer.class);        


        return CustomerResults;                     

    }
}

The static resource is a copy of the correct JSON for this, and I am getting 0% code coverage... What am I doing wrong? (besides seealldata true ;) )

  • 3
    note that StaticResource is available w/o needing seeAllData=true – cropredy Jan 20 at 23:24
3

You're overcomplicating this for yourself. Your JSON-deserialization approach writes directly to the object's instance variables and does not invoke the setter methods, which are the only executable lines of code in this class.

Also note that the testMethod annotation is deprecated (use @isTest), test methods don't return anything, and it sounds like you already know seeAllData=true is a bad idea!

In your test class, invoke the getter/setter methods by just setting the values directly:

@isTest
public class ProviderAPI_Datatype_Test {

    @isTest
    static void testCustomerObj() {
        ProviderAPI_Datatype.Customer customer = new ProviderAPI_Datatype.Customer();

        customer.CustomerName = 'Test';
        // if you're a completionist:
        System.assertEquals('Test', customer.CustomerName, 'set the CustomerName property');
        // and so on down the line
    }
}
| improve this answer | |
  • OMG! Thank you! This worked! What's funny is that in my journey through Salesforce, that has been my number one problem, over complicating it. I'm a 15 year .Net dev, that switched to Salesforce Apex a little over 2 years ago... Thanks! – Daniel Bauer Jan 21 at 4:12

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.