You need to sign in to do that
Don't have an account?
KitagawaSan805
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'); } }
I was able to add
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!
I still haven't been able to get above 32% coverage on this...
Since there is no 'action' being taken (inserting a record etc).
Here is a cleaner example:
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);