You need to sign in to do that
Don't have an account?

Help in writing Apex class.
Hey,
I have written an Apex class and Visual Force page which searches the records from custom object. There is a field which is a multi pick list and it is a fielter field. User seletcs the value in that field and all the records meeting the critiriea appear in the 'Results' section. This page has 3 buttons "Search", "Cancel" and "Export to CSV". I am not good in writing a test class but I tried to write some and it is not giving my desired code coverage. Can someone pleae help me out ? Any help will be appreciated
Apex class:
public class Fetchsiteplacement{
String devicetype;
public Site_Placements__c sp{get;set;}
public List<Site_Placements__c > spRec{get;set;}
public List<Site_Placements__c > spRec1{get;set;}
public List<siteplacementwrapper> spwrapper{get;set;}
public List<siteplacementwrapper> spwrapperexcel{get;set;}
public class siteplacementwrapper{
public String Name {get;set;}
}
public Fetchsiteplacement(){
sp=new Site_Placements__c ();
spRec = new List<Site_Placements__c>();
}
public void FillAggregates(){
String devicetype= '';
string[] lststr1=sp.Device_Type__c.split(';');
for (String s1: lststr1) {
devicetype+= '\'' + s1 + '\',';
}
devicetype= devicetype.substring (0,devicetype.length() -1);
spwrapper=new List<siteplacementwrapper>();
spwrapperexcel=new List<siteplacementwrapper>();
String sitePlacementQuery ='SELECT Id,Name from Site_Placements__c WHERE Device_Type__c INCLUDES (' + devicetype + ') ';
spRec=database.query(sitePlacementQuery);
String sitePlacementQuery1 ='SELECT Id,Name from Site_Placements__c WHERE Device_Type__c INCLUDES (' + devicetype + ') ';
spRec1=database.query(sitePlacementQuery1);
if(spRec.size() == 0)
{
Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No records match your criteria'));
}
else {
for(Site_Placements__c sitePlacement:spRec){
siteplacementwrapper spr=new siteplacementwrapper();
spr.Name=sitePlacement.Name;
spwrapper.add(spr);
}
for(Site_Placements__c sitePlacement:spRec1){
siteplacementwrapper spr=new siteplacementwrapper();
spr.Name=sitePlacement.Name;
spwrapperexcel.add(spr);
}
}
}
public PageReference FetchExcelReport() {
PageReference nextpage = new PageReference('/apex/RFP_Excel_Page');
return nextpage;
}
public pagereference CancelSPRec(){
PageReference page = new PageReference('https://c.cs30.visual.force.com/apex/SiteListPage');
page.SetRedirect(true);
return page;
}}
Visual Force Page:
<apex:page controller="Fetchsiteplacement" tabStyle="Site_Placements__c" sidebar="false">
<apex:form >
<apex:pageBlock >
<apex:messages layout="table" styleClass="exceptionText"/>
<apex:pageBlockButtons location="Bottom">
<apex:commandButton value="Search" action="{!FillAggregates}"/>
<apex:commandButton value="Reset" action="{!CancelSPRec}"/>
<apex:commandButton value="Export to CSV" action="{!FetchExcelReport}" id="theButton" />
</apex:pageBlockButtons>
<apex:pageBlockSection title="Select your filter criteria" collapsible="False">
<apex:pageBlockSectionItem >
<apex:outputLabel value="Player - Device Type" for="Devicetype"/>
<apex:inputField value="{!sp.Device_Type__c}" id="Devicetype"/>
</apex:pageBlockSectionItem>
<apex:pageBlock >
<apex:pageBlockSection title="Site List Results" collapsible="False">
<apex:pageBlockSectionItem >
<apex:pageblockTable value="{!spwrapper}" var="site" style="width:150%">
<apex:column style="width:650px"> <apex:facet name="header">Name</apex:facet>{!site.Name}</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageblock>
</apex:form>
</apex:page>
Test Class:
@isTest(seeAllData=true)
private class TestFetchsiteplacement {
static testMethod void myUnitTest() {
// TO DO: implement unit test
Account a = new Account(Name='Test Account');
insert a;
Publisher_Deal_Types__c pubdeal = new Publisher_Deal_Types__c ( Name = 'Test Publisher deal type', Account_Name__c = a.Id);
insert pubdeal;
Site_Placements__c sp = new Site_Placements__c ();
sp.Name = 'Test site placement';
insert sp;
PageReference ref = new PageReference('/apex/SiteListPage');
Test.setCurrentPage(ref);
Test.startTest();
Fetchsiteplacement myController = new Fetchsiteplacement();
Test.stopTest();
}
}
I have written an Apex class and Visual Force page which searches the records from custom object. There is a field which is a multi pick list and it is a fielter field. User seletcs the value in that field and all the records meeting the critiriea appear in the 'Results' section. This page has 3 buttons "Search", "Cancel" and "Export to CSV". I am not good in writing a test class but I tried to write some and it is not giving my desired code coverage. Can someone pleae help me out ? Any help will be appreciated
Apex class:
public class Fetchsiteplacement{
String devicetype;
public Site_Placements__c sp{get;set;}
public List<Site_Placements__c > spRec{get;set;}
public List<Site_Placements__c > spRec1{get;set;}
public List<siteplacementwrapper> spwrapper{get;set;}
public List<siteplacementwrapper> spwrapperexcel{get;set;}
public class siteplacementwrapper{
public String Name {get;set;}
}
public Fetchsiteplacement(){
sp=new Site_Placements__c ();
spRec = new List<Site_Placements__c>();
}
public void FillAggregates(){
String devicetype= '';
string[] lststr1=sp.Device_Type__c.split(';');
for (String s1: lststr1) {
devicetype+= '\'' + s1 + '\',';
}
devicetype= devicetype.substring (0,devicetype.length() -1);
spwrapper=new List<siteplacementwrapper>();
spwrapperexcel=new List<siteplacementwrapper>();
String sitePlacementQuery ='SELECT Id,Name from Site_Placements__c WHERE Device_Type__c INCLUDES (' + devicetype + ') ';
spRec=database.query(sitePlacementQuery);
String sitePlacementQuery1 ='SELECT Id,Name from Site_Placements__c WHERE Device_Type__c INCLUDES (' + devicetype + ') ';
spRec1=database.query(sitePlacementQuery1);
if(spRec.size() == 0)
{
Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No records match your criteria'));
}
else {
for(Site_Placements__c sitePlacement:spRec){
siteplacementwrapper spr=new siteplacementwrapper();
spr.Name=sitePlacement.Name;
spwrapper.add(spr);
}
for(Site_Placements__c sitePlacement:spRec1){
siteplacementwrapper spr=new siteplacementwrapper();
spr.Name=sitePlacement.Name;
spwrapperexcel.add(spr);
}
}
}
public PageReference FetchExcelReport() {
PageReference nextpage = new PageReference('/apex/RFP_Excel_Page');
return nextpage;
}
public pagereference CancelSPRec(){
PageReference page = new PageReference('https://c.cs30.visual.force.com/apex/SiteListPage');
page.SetRedirect(true);
return page;
}}
Visual Force Page:
<apex:page controller="Fetchsiteplacement" tabStyle="Site_Placements__c" sidebar="false">
<apex:form >
<apex:pageBlock >
<apex:messages layout="table" styleClass="exceptionText"/>
<apex:pageBlockButtons location="Bottom">
<apex:commandButton value="Search" action="{!FillAggregates}"/>
<apex:commandButton value="Reset" action="{!CancelSPRec}"/>
<apex:commandButton value="Export to CSV" action="{!FetchExcelReport}" id="theButton" />
</apex:pageBlockButtons>
<apex:pageBlockSection title="Select your filter criteria" collapsible="False">
<apex:pageBlockSectionItem >
<apex:outputLabel value="Player - Device Type" for="Devicetype"/>
<apex:inputField value="{!sp.Device_Type__c}" id="Devicetype"/>
</apex:pageBlockSectionItem>
<apex:pageBlock >
<apex:pageBlockSection title="Site List Results" collapsible="False">
<apex:pageBlockSectionItem >
<apex:pageblockTable value="{!spwrapper}" var="site" style="width:150%">
<apex:column style="width:650px"> <apex:facet name="header">Name</apex:facet>{!site.Name}</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageblock>
</apex:form>
</apex:page>
Test Class:
@isTest(seeAllData=true)
private class TestFetchsiteplacement {
static testMethod void myUnitTest() {
// TO DO: implement unit test
Account a = new Account(Name='Test Account');
insert a;
Publisher_Deal_Types__c pubdeal = new Publisher_Deal_Types__c ( Name = 'Test Publisher deal type', Account_Name__c = a.Id);
insert pubdeal;
Site_Placements__c sp = new Site_Placements__c ();
sp.Name = 'Test site placement';
insert sp;
PageReference ref = new PageReference('/apex/SiteListPage');
Test.setCurrentPage(ref);
Test.startTest();
Fetchsiteplacement myController = new Fetchsiteplacement();
Test.stopTest();
}
}
myController .FillAggregates();
myController .FetchExcelReport();
myController .CancelSPRec();
Also you have to set "Device_Type__c" something like
sp.Device_Type__c=a;b;c;d; //What ever picklist value you have for Device_Type__c.
Can you alos be specific to What are the lines which are not covered by your Test class .
Thanks for your reply. Device__type__c can have values as PC, Tablet, Mobile. Below is the highlighted Code which is not covered. Please Advice.
public class Fetchsiteplacement{
String devicetype;
public Site_Placements__c sp{get;set;}
public List<Site_Placements__c > spRec{get;set;}
public List<Site_Placements__c > spRec1{get;set;}
public List<siteplacementwrapper> spwrapper{get;set;}
public List<siteplacementwrapper> spwrapperexcel{get;set;}
public class siteplacementwrapper{
public String Name {get;set;}
}
public Fetchsiteplacement(){
sp=new Site_Placements__c ();
spRec = new List<Site_Placements__c>();
}
public void FillAggregates(){
String devicetype= '';
string[] lststr1=sp.Device_Type__c.split(';');
for (String s1: lststr1) {
devicetype+= '\'' + s1 + '\',';
}
devicetype= devicetype.substring (0,devicetype.length() -1);
spwrapper=new List<siteplacementwrapper>();
spwrapperexcel=new List<siteplacementwrapper>();
String sitePlacementQuery ='SELECT Id,Name from Site_Placements__c WHERE Device_Type__c INCLUDES (' + devicetype + ') ';
spRec=database.query(sitePlacementQuery);
String sitePlacementQuery1 ='SELECT Id,Name from Site_Placements__c WHERE Device_Type__c INCLUDES (' + devicetype + ') ';
spRec1=database.query(sitePlacementQuery1);
if(spRec.size() == 0)
{
Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No records match your criteria'));
}
else {
for(Site_Placements__c sitePlacement:spRec){
siteplacementwrapper spr=new siteplacementwrapper();
spr.Name=sitePlacement.Name;
spwrapper.add(spr);
}
for(Site_Placements__c sitePlacement:spRec1){
siteplacementwrapper spr=new siteplacementwrapper();
spr.Name=sitePlacement.Name;
spwrapperexcel.add(spr);
}
}
}
public PageReference FetchExcelReport() {
PageReference nextpage = new PageReference('/apex/RFP_Excel_Page');
return nextpage;
}
public pagereference CancelSPRec(){
PageReference page = new PageReference('https://c.cs30.visual.force.com/apex/SiteListPage');
page.SetRedirect(true);
return page;
}}
Try this updated code, and also try to send Site_Placements__c sp details to class.
Best Regards,
Mithun.
Thanks for your reply. I am gettig this below error.
System.NullPointerException: Attempt to de-reference a null object
Class.TestFetchsiteplacement.myUnitTest: line 48, column 1
error is in this line : myController.FillAggregates();
How do I pass parameters in myController.FillAggregates(); ? And how do we address Multi pick lists as a fielter field ? Please advice.
Sorry to bother you. Can anyone of you please advice on this ? I will really appreciate your help.