0

I'm trying to get a test class to cover a few wrapper classes I've created in Apex, but for some reason one of the wrapper classes doesn't gain coverage (0%) like the other one does (70%). I read in a similar question that its not possible to cover variables in wrapper classes, but I'm able to do just that on my second wrapper (WebhookNewCustomerWrapper) which is at 70% test coverage.

My main question is am I going about testing these classes the correct way or do I need to be looking in another direction?

Please find below my Test Class as well as 2 of my wrapper classes.

Test Class

@isTest
public class TestWebhookListenerAndWrapper {

@isTest static void test_method_1(){
    
    // Test WebhookWrapper
    WebhookWrapper whw = new WebhookWrapper();
    whw.id = 1111;
    whw.title = 'Test Product';
    whw.product_type = 'Donation';
    whw.variants = new list<webhookwrapper.variants>();
    
    webhookwrapper.Variants v = new webhookwrapper.variants();
    v.id = 1111;
    v.product_id = 1111;
    v.title = 'Test Product Variant';
    v.price = '20.00';
    v.sku = '1111';
    v.position = 1;
    v.compare_at_price = 15.00;
    v.option1 = 'small';
    v.inventory_item_id = 1;
    v.inventory_quantity = 2;
    v.old_inventory_quantity = 3;
    
    whw.variants.add(v);
    
    System.debug( whw);
    
    // Test WebhookNewCustomerWrapper
    WebhookNewCustomerWrapper whncw = new WebhookNewCustomerWrapper();
    
    whncw.id = '1111';
    whncw.email = '[email protected]';
    whncw.first_name = 'test';
    whncw.last_name = 'name';
    whncw.state = 'virginia';
    whncw.note = 'test note';
    whncw.phone= '5555555555';
    
    System.debug(whncw);

Wrapper 1: WebhookWrapper (0% Test Coverage)

public class WebhookWrapper {

public Long id;
public String title;
public String product_type;
public List<Variants> variants;
public list<metafields> metafields;
public String status;

public class Variants {
    public Long id;
    public Long product_id;
    public String title;
    public String price;
    public String sku;
    public Integer position;
    public Double compare_at_price;
    public String option1;
    public Long inventory_item_id;
    public Integer inventory_quantity;
    public Integer old_inventory_quantity; 
}

public class Metafields {
    public String key;
    public String value;
    
}

public static WebhookWrapper parse(String json) {
    return (WebhookWrapper) System.JSON.deserialize(json, WebhookWrapper.class);
}
}

Wrapper 2: WebhookNewCustomerWrapper (70% test coverage)

public class WebhookNewCustomerWrapper {
public String id {get;set;} 
public String email {get;set;} 
public String first_name {get;set;} 
public String last_name {get;set;} 
public String state {get;set;} 
public String note {get;set;} 
public String phone {get;set;} 
public List<Address> addresses {get;set;} 

public static WebhookNewCustomerWrapper parse(String json) {
    return (WebhookNewCustomerWrapper) System.JSON.deserialize(json, WebhookNewCustomerWrapper.class);
}
}

1 Answer 1

1

You are missing testing the actual code that does work --

public static WebhookWrapper parse(String json) {
  return (WebhookWrapper) System.JSON.deserialize(json, WebhookWrapper.class);
}

You need in your testmethod...

WebhookNewCustomerWrapper.parse('some json here'); 

You do not need to test coverage on variables unless they have {get; set;} or {get {..some method.. } or set {..some method..}

2
  • Thanks, this solved my issue! Jun 8, 2022 at 16:27
  • note that your test method doesn't need to set the variables at all. You can delete all those lines
    – cropredy
    Jun 8, 2022 at 16:35

You must log in to answer this question.

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