function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lars SundbergLars Sundberg 

Testing inner classes

I have a trigger that invokes a callout on insert of a custom object.

 

In the test below, I only get 33 % code coverage, since the inner classes Response and Data isn't covered by the test. How can I test them?

 

My class:

 

public with sharing class OuterClass {
    @future (callout=true)
    public static void doCallout() {
        //Callout occurs here normally, but to simplify:
        String body = '{"status":1,"data":[{"id":"33e26b70-ec3e-11e1-bbc0-bfcb6088709b","name":"Testname","created":"2012-08-16T14:24:32"}]}';
        getData(body);
    }
    
    public static void getData(String body) {
        Response r = (Response) JSON.deserialize(body, Response.class);
        System.debug(r.status);
        for (Data d : r.data) {
            System.debug('d.id: ' + d.id);
        }
    }
    
    class Response {
        public Integer status;
        public Data[] data;
         
        public Response(Integer status) {
            this.status = status;
            this.data = new List<Data>();
        }        
    }
    
    class Data {
        public String id;
        public String name;
        public String created;
         
        public Data(String id, String name, String created) {
            this.id = id;
            this.name = name;
            this.created = created;
        }        
    }
}

 

My testclass:

 

@isTest 
private class TestOuterClass {
    static testMethod void testOuterClass() {
        //Creating test data...
        
        Test.startTest();
        insert testdata;
        Test.stopTest();
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
SamuelDeRyckeSamuelDeRycke

By doing the same thing pretty much.

 

OuterClass.Responce r= new Outerclass.Responce(5);
system.assertNotEquals(null, r.data);
system.assertNotEquals(5, r.status);


OuterClass.data d = new Outerclass.data('id','name','rightNow');
//assert stuff

 May contain typos.

 

 

 

 

 

 

.

All Answers

SamuelDeRyckeSamuelDeRycke

By doing the same thing pretty much.

 

OuterClass.Responce r= new Outerclass.Responce(5);
system.assertNotEquals(null, r.data);
system.assertNotEquals(5, r.status);


OuterClass.data d = new Outerclass.data('id','name','rightNow');
//assert stuff

 May contain typos.

 

 

 

 

 

 

.

This was selected as the best answer
Lars SundbergLars Sundberg

Thank you!

 

So, edited:

 

My class:

 

public with sharing class OuterClass {
    @future (callout=true)
    public static void doCallout() {
        //Callout occurs here normally, but to simplify:
        String body = '{"status":1,"data":[{"id":"33e26b70-ec3e-11e1-bbc0-bfcb6088709b","name":"Testname","created":"2012-08-16T14:24:32"}]}';
        getData(body);
    }
    
    public static void getData(String body) {
        Response r = (Response) JSON.deserialize(body, Response.class);
        System.debug(r.status);
        for (Data d : r.data) {
            System.debug('d.id: ' + d.id);
        }
    }
    
    public class Response {
        public Integer status;
        public Data[] data;
         
        public Response(Integer status) {
            this.status = status;
            this.data = new List<Data>();
        }        
    }
    
    public class Data {
        public String id;
        public String name;
        public String created;
         
        public Data(String id, String name, String created) {
            this.id = id;
            this.name = name;
            this.created = created;
        }        
    }
}

 

My testclass:

 

@isTest 
private class TestOuterClass {
    static testMethod void testInsertTrigger() {
        //Creating test data...
        
        Test.startTest();
        insert maUsers;
        Test.stopTest();
        
        //assert stuff
    }
    
    static testMethod void testInnerClasses() {
        OuterClass.Response r = new Outerclass.Response(1);
        system.assert(r.status == 1);
        
        OuterClass.Data d = new Outerclass.data('33e26b70-ec3e-11e1-bbc0-bfcb6088709b','Testname','2012-08-16T14:24:32');
        system.assert(d.id == '33e26b70-ec3e-11e1-bbc0-bfcb6088709b');
        system.assert(d.name == 'Testname');
        system.assert(d.created == '2012-08-16T14:24:32');
    }
}

 

This gives me 100 % coverage.

David Roberts 4David Roberts 4
Many thanks. A great help.