You need to sign in to do that
Don't have an account?
Thakkar Parth
Help creating test class
I'm new to using APEX and would really appreciate your help. I found this bit of code online I need to write a test class to ensure it has coverage for deployment. Can someone please help me construct a test class, or point me in the right direction? It describes charts with a combination of Bar and Line charts and with pie chart in one grid.
Thanks, Marc
=== Class ====
==== Page ====
Thanks, Marc
=== Class ====
public class HighchartsController { public String str { get { String res = ''; Integer i; //SOQL res = '['; for(i=1;i<13;i++) res += '' + (i+1) + ','; res += 12; res += ']'; return res; } } public String bar { get { String str = ''; Integer i; //SOQL str = '['; for(i=1;i<13;i++) str += '' + i + ','; str += 12; str += ']'; return str; } } public List<Integer> X { get { //X= '10'; X=new List<Integer>(); String q = 'Select count(Id),StageName from opportunity where StageName IN(\'Closed Won\',\'Closed Lost\') group by StageName'; AggregateResult[] agr =Database.query(q); // X = '\''+ String.valueof(agr[0].get('expr0')) + ',' +String.valueof(agr[1].get('expr0')) + '\''; X.add(Integer.valueof(agr[0].get('expr0'))); X.add(Integer.valueof(agr[1].get('expr0'))); return X; } set {} } }
==== Page ====
<apex:page controller="HighchartsController"> <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="https://code.highcharts.com/highcharts.js"></script> <script src="https://code.highcharts.com/modules/exporting.js"></script> <div id="container" style="min-width: 310px; height: 400px; margin:0 auto"></div> <script> pieOrdinate = {!X}; // pieOrdinate = ServerStr.split(','); $(function () { $('#container').highcharts({ title: { text: 'Chart showing opportunities' }, xAxis:{ categories: ['Jan','Feb','Mar','Apr','May','Jun','July','Aug','Sept','Oct','Nov','Dec'] }, labels: { items: [{ html: 'Opportunities', style: { left: '50px', top: '18px', color: (Highcharts.theme && Highcharts.theme.textColor) || 'black' } }] }, series: [ { type: 'column', name: 'Indian Railways', data: {!str} //data:[2,3,4,5,6] }, { type: 'spline', name: 'Monthly Sales', // Average // data: [3, 2.67, 3, 6.33, 3.33], data :{!bar}, marker: { lineWidth: 2, lineColor: Highcharts.getOptions().colors[3], fillColor: 'white' } }, { type: 'pie', name: 'Total consumption', data: [ { name: 'Lost', //y:23, y :parseInt(pieOrdinate[0]), sliced:true, selected:true, color: Highcharts.getOptions().colors[1] // Opp's Lost color }, { name: 'Won', y:parseInt(pieOrdinate[1]), color: Highcharts.getOptions().colors[2] // Opp's won color }], center: [100, 80], size: 100, showInLegend: false, dataLabels: { enabled:true } }] }); }); </script> </apex:page>
Your class may look something like the following:
Note, that I prototyped you two method for testing "X". A good unit test will test all the conditions. Positive, Negative, boundary conditions, et al.
To run the unit test to verify everything is working smoothly, you can open up Developer Console (Setup->Developer Console) and run the unit test there.
static testmethod void testStrGetter(){
//construct HighchartsController
//Call ctrl.str;
//System.assert that str is as expected.
}
I've written out the test class for "testXWithValidOpportunities", which is probably the most complicated test class you'll write with that controller. That should give you a good starting point. Note, that you may have other required fields on the Opportunity you'll need to fill in.