You need to sign in to do that
Don't have an account?
need help understanding code so i can write unit test
I am not a developer but I was trying to create a simple vf page for a report I need that I could not create using standard reports. Using a Group By sql query I am able to get the results I am looking for but to display it in a vf page I had to use some apex code I found on a blog that was linked to on the boards. It works great but first I don't fully understand the code and because I am not creating or inserting any records I am not sure how to write unit test to get the required 75% code coverage. I have searched the boards and have read the links that people point everyone to when they ask test questions and I understand the concepts but I have yet to find a simple example of how to test code that just query's the db. I am sure it can't be hard but with no development back ground I haven't got a clue.
Thanks for any help you can provide.
Eric
public class casemorethan10 { public list<AggregateResult> lstAR = new list<AggregateResult>(); /* Note that any query that includes an aggregate function returns its results in an array of AggregateResult objects. AggregateResult is a read-only sObject and is only used for query results. Aggregate functions become a more powerful tool to generate reports when you use them with a GROUP BY clause. For example, you could find the count of all opportunities for a CloseDate. */ public casemorethan10() { lstAR = [Select c.account.name name, count(c.casenumber) total from Case c where status = 'Open' and c.recordtype.name <> 'Opperations Case' and c.recordtype.name <> 'Maintenance Notifications' group by c.account.name Having count(c.casenumber) > 15]; } public list<OppClass> getResults() { list<OppClass> lstResult = new list<OppClass>(); for (AggregateResult ar: lstAR) { oppClass objOppClass = new oppClass(ar); lstResult.add(objOppClass); } return lstResult; } class oppClass { public Integer total { get;set; } public String name { get;set; } public oppClass(AggregateResult ar) { //Note that ar returns objects as results, so you need type conversion here total = (Integer)ar.get('total'); name = (String)ar.get('name'); } } }
<apex:page controller="casemorethan10"> <apex:outputPanel layout="block" style="overflow:auto;width:380px;height:600px" > <apex:dataTable value="{!Results}" var="r" border="2" cellpadding="1" > <apex:column headerValue="Name" value="{!r.name}"/> <apex:column headerValue="# cases" value="{!r.total}"/> </apex:dataTable> </apex:outputPanel> </apex:page>