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
KitagawaSan805KitagawaSan805 

JSON Writer Class Test Method

Hello, 

 

I new still new to APEX and have a class that generates some JSON, but can't figure out how to write a test class for it... I have written test classes before, but not for a page controller that just serves up JSON... 

 

Thanks!

 

public class Json_Controller {
    
    public string json {get;set;}
        
    public Json_Controller() {      
        VQ_Reference_Sheet__c[] vqr = new VQ_Reference_Sheet__c[]{};
        integer start = 0;
        integer pageSize = 100;
        
        if (ApexPages.currentPage().getParameters().get('start') != null && ApexPages.currentPage().getParameters().get('limit') != null) {
            start = integer.valueof(ApexPages.currentPage().getParameters().get('start'));
            pageSize = integer.valueof(ApexPages.currentPage().getParameters().get('limit'));
        }
        
        string jsonRecordsString = '';
        
        integer i = 1;
        integer j = 0;
        
        for (VQ_Reference_Sheet__c v : [Select 
        
        id, 
        name, 
        Times_Referenced_Used_Total__c, 
        Hardware_Platform__c
              
        
        from VQ_Reference_Sheet__c order by name limit 1000]) {
            if (j >= start) {
                if (i <= pageSize) {
                    jsonRecordsString += '{';
                //record info      
                    jsonRecordsString += '"label":' + '"'+v.name+'",';  
                    jsonRecordsString += '"use":' + '["'+v.Times_Referenced_Used_Total__c+'"],';
                    jsonRecordsString += '"hrd":' + '["'+v.Hardware_Platform__c+'"],';
                    
                //record id    
                    jsonRecordsString += '"sfdcid":' + '"'+v.id+'",';
                    jsonRecordsString += '},';
                    i++;
                }
            }
            vqr.add(v);
            j++;
        }
        
      //lets do some clean-up   
            jsonRecordsString = jsonRecordsString.replaceAll(' 00:00:00','');
            jsonRecordsString = jsonRecordsString.replaceAll(';','","');
            jsonRecordsString = jsonRecordsString.replaceAll('null','');
            jsonRecordsString = jsonRecordsString.replaceAll('"latlng":","','"latlng":""');
               
        string jsonString = 'sfdccallback({ types: {"Reference" : {pluralLabel: "References"}},"items":[' + jsonRecordsString + '], "properties" : {"rev" : {"valueType" : "number"}, "emp" : {"valueType" : "number"}}   })';
        jsonString = jsonString.replaceAll(',]',']');
        this.json = jsonString;

    }
}

 

I have started something like this just to see if I can get it to  work, but still not success: 

@isTest
private class ControllerTest {
    static testMethod void Json_Controller() {
        Json_Controller controller = new Json_Controller();
        System.assertEquals(json.substring(0, 12),'sfdccallback');
    }
}

 

KitagawaSan805KitagawaSan805

I was able to add 

 static testMethod void Json_Controller() {
        Json_Controller controller = new Json_Controller();
        string json = controller.json;
        System.assertEquals(json.substring(0, 12),'sfdccallback');

  to get to 32% coverage... 

 

Is there a way to test the sections where I am just adding parts to the jsonRecordsString? I can't find the documentation to help... 

Any help would be greatly appreciated!

KitagawaSan805KitagawaSan805

I still haven't been able to get above 32% coverage on this... 

 

Since there is no 'action' being taken (inserting a record etc). 

KitagawaSan805KitagawaSan805

Here is a cleaner example: 

 

public class Json_Controller {
	
	public string json {get;set;}
		
	public Json_Controller() {		
        opportunity[] opps = new opportunity[]{};
		integer start = 0;
		integer pageSize = 10;
		
		if (ApexPages.currentPage().getParameters().get('start') != null && ApexPages.currentPage().getParameters().get('limit') != null) {
			start = integer.valueof(ApexPages.currentPage().getParameters().get('start'));
			pageSize = integer.valueof(ApexPages.currentPage().getParameters().get('limit'));
		}
        
        string jsonRecordsString = '';
        
        integer i = 1;
        integer j = 0;
        
        for (Opportunity o : [Select id, name, stagename, closedate from opportunity order by name limit 1000]) {
			if (j >= start) {
				if (i <= pageSize) {
					jsonRecordsString += '{';
					jsonRecordsString += '"id":' + '"'+o.id+'",';
					jsonRecordsString += '"name":' + '"'+o.name+'",';
					jsonRecordsString += '"stagename":' + '"'+o.stagename+'",';
					jsonRecordsString += '"closedate":' + '"'+o.closedate+'"';
					jsonRecordsString += '},';
					i++;
				}
			}
			opps.add(o);
			j++;
        }
               
		string jsonString = '({"total":"'+opps.size()+'", "results":[' + jsonRecordsString + ']})';
        jsonString = jsonString.replaceAll(',]',']');
        this.json = jsonString;
	}
}

 

knthornt1knthornt1

You will need to set up your page in your tests. You code is expecting there to be page params and you need to provide them.

 

PageReference pageRef = Page.<your VF page name>;

pageRef.getParameters().put('start','<your_start_param>');

pageRef.getParameters().put('start','<your_limit_param>');

Test.setCurrentPageReference(pageRef);