0

i have a test class for salesforce

   @IsTest
public class JsonGCloudApiApex_TEST {
    
   
    // This test method should give 100% coveragex
    static testMethod void testParse() {
        JsonGCloudApiApex objItems;
        List<JsonGCloudApiApex.Item> listItems;
        JsonGCloudApiApex objItems2;
        List<JsonGCloudApiApex.Item> listItems2;
                
        String json = '{"items":[{"coverage": "test","name":"test","id":"test","contentType":"test","fileName":"test","updated":"test","generation":"test","mediaLink":"test"},{"coverage": "test", "name":"test","id":"test","contentType":"test","fileName":"test","updated":"test","generation":"test","mediaLink":"test"}, {"name":"test","id":"test","contentType":"test","fileName":"test","updated":"test","generation":"test","mediaLink":"test"}]}';
        objItems = parse(json);
        
        listItems = new List<JsonGCloudApiApex.Item>();
        
        for(JsonGCloudApiApex.Item i : objItems.items){
            i.timestamp = 16002;
            System.assert(i.name != null);
            System.assert(i.id != null);
            System.assert(i.mediaLink != null);
            System.assert(i.contentType != null);
            System.assert(i.fileName != null);
            System.assert(i.updated != null);
            System.assert(i.timestamp != null);
        }
        
        listItems.sort();
        
        
        
    }
    
    
    static JsonGCloudApiApex parse(String json) {
        return (JsonGCloudApiApex) System.JSON.deserialize(json, JsonGCloudApiApex.class);
    }
}

and i have a my apex class

    public class JsonGCloudApiApex {
    public class Item implements Comparable{
        public string name {get; set;}
        public string id {get; set;}
        public string mediaLink {get; set;}
        public string contentType {get; set;}  
        public string fileName {get; set;}  
        public string updated {get; set;}
        public string generation {get; set;}
        public Integer timestamp {get; set;}
        
        
        public Integer compareTo(Object other) {
            return (Integer)(((Item)other).timestamp - this.timestamp);
        }
    }
    
    public List<Item> items {get; set;}
    
    public static JsonGCloudApiApex parse(String json) {
        return (JsonGCloudApiApex) System.JSON.deserialize(json, JsonGCloudApiApex.class);
    }
}

On my salesforce page i see 61% (8/13) code coverage, but i don't know what another extra assert add to my test class up to 75% coverage code any idea?

1
  • 1
    Please disregard my previous comment; I didn't see the scroll bar or the fact that you had a static method defined in your test class! However, you are certainly not calling JsonGCloudApiApex.parse(), so it won't be covered.
    – David Reed
    Commented Oct 2, 2020 at 19:15

1 Answer 1

1

You need to make sure you call JsonGCloudApiApex.parse(json) inside your testParse method. When you call parse in your unit test, that calls the method with this signature: JsonGCloudApiApex_TEST.parse, and not the one you need to get coverage for: JsonGCloudApiApex.parse. You can delete the parse method from the test class, I don't see any use for it.

 @IsTest
public class JsonGCloudApiApex_TEST {
    
    // This test method should give 100% coveragex
    static testMethod void testParse() {
        JsonGCloudApiApex objItems;
        List<JsonGCloudApiApex.Item> listItems;
        JsonGCloudApiApex objItems2;
        List<JsonGCloudApiApex.Item> listItems2;
                
        String json = '{"items":[{"coverage": "test","name":"test","id":"test","contentType":"test","fileName":"test","updated":"test","generation":"test","mediaLink":"test"},{"coverage": "test", "name":"test","id":"test","contentType":"test","fileName":"test","updated":"test","generation":"test","mediaLink":"test"}, {"name":"test","id":"test","contentType":"test","fileName":"test","updated":"test","generation":"test","mediaLink":"test"}]}';
        objItems = JsonGCloudApiApex.parse(json);
        
        listItems = new List<JsonGCloudApiApex.Item>();
        
        for(JsonGCloudApiApex.Item i : objItems.items){
            i.timestamp = 16002;
            System.assert(i.name != null);
            System.assert(i.id != null);
            System.assert(i.mediaLink != null);
            System.assert(i.contentType != null);
            System.assert(i.fileName != null);
            System.assert(i.updated != null);
            System.assert(i.timestamp != null);
        }
        
        listItems.sort();
    }

}

You must log in to answer this question.

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