You need to sign in to do that
Don't have an account?
Naveen Kvp
Test class for costom controller??
I am try to write a test class for custom controller but i got some errors i am not that much familiar with writing test classes here i am post the controller code and test class can anybody find out the errors...help me to reach 100% code coverage
page:
<apex:page controller="Brandingcls" sidebar="true">
<apex:pageMessages />
<apex:form >
<table width="100%" align="center">
<tr>
<td style="font-size:15px" align="center">
<b>Branding Visit</b>
</td>
</tr>
</table>
<table width="100%" align="center">
<tr>
<td>
<apex:pageBlock >
<apex:pageBlockSection >
<apex:inputField value="{!brorder.Order_Date__c}" required="True"/>
<apex:inputField value="{!brorder.Account__c}" required="True"/>
</apex:pageBlockSection>
</apex:pageBlock>
</td>
</tr>
</table>
<apex:pageBlock >
<apex:pageBlockTable value="{!bimaster}" var="b">
<apex:column headerValue="Branding Items" value="{!b.name}"/>
<apex:column headerValue="Quantity">
<apex:inputField value="{!b.Quantity__c}"/>
</apex:column>
<apex:column headerValue="Remarks">
<apex:inputField value="{!b.Remarks__c}"/>
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Save" action="{!save}"/>
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
controller:
public class Brandingcls
{
public Branding_Visit__c brvisit{get;set;}
public Branding_Order__c brorder{get;set;}
public list<Branding_Item_Master__c> bimaster{get;set;}
public list<Branding_Line_Item__c> brlineitem{get;set;}
public Brandingcls()
{
bimaster=[select id,Name,Remarks__c,Quantity__c from Branding_Item_Master__c];
brorder=new Branding_Order__c(Order_Date__c=system.Today());
brvisit=new Branding_Visit__c();
}
public PageReference save()
{
list<Branding_Item_Master__c> lstbritem =new list<Branding_Item_Master__c>();
for(Branding_Item_Master__c bim :bimaster)
{
if(bim.Quantity__c>0)
{
lstbritem.add(bim);
}
}
if(lstbritem.Size()>0)
{
insert brorder;
brvisit.Account__c = brorder.Account__c;
brvisit.Visit_Date__c = brorder.Order_Date__c;
insert brvisit;
list<Branding_Line_Item__c> lstbli = new list<Branding_Line_Item__c>();
for(Branding_Item_Master__c bm:lstbritem)
{
Branding_Line_Item__c brandli= new Branding_Line_Item__c();
brandli.Branding_Order__c=brorder.id;
brandli.Branding_Type__c=bm.id;
brandli.Quantity__c=bm.Quantity__c;
brandli.Remarks__c=bm.Remarks__c;
lstbli.add( brandli);
}
if(lstbli.Size()>0)
{
insert lstbli;
}
}
else
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Enter atleast One Quantity to Create Branding Visit and Branding Order'));
return Null;
}
pagereference ref=new pagereference( '/' +brvisit.id);
ref.setredirect(true);
return ref;
}
public PageReference cancel()
{
return null;
}
}
testclass:
@istest
public class testBrandingcls{
static testmethod void testsave(){
Branding_Order__c br=new Branding_Order__c(Order_Date__c=system.today(),Account__c='testnaveen');
insert br;
Branding_Visit__c bv=new Branding_Visit__c(Visit_Date__c=system.today(),Account__c='testvisit');
insert bv;
Branding_Line_Item__c blm=new Branding_Line_Item__c(Branding_Type__c='Branding_Item_Master__c.id',Branding_Order__c='Branding_Order__c.id',Quantity__c=12);
insert blm;
Brandingcls obj= new Brandingcls();
obj.save();
obj.cancel();
PageReference p=Page.Brandingvfpage;
Test.setCurrentPage(p);
}
}
Thanks in advance...
page:
<apex:page controller="Brandingcls" sidebar="true">
<apex:pageMessages />
<apex:form >
<table width="100%" align="center">
<tr>
<td style="font-size:15px" align="center">
<b>Branding Visit</b>
</td>
</tr>
</table>
<table width="100%" align="center">
<tr>
<td>
<apex:pageBlock >
<apex:pageBlockSection >
<apex:inputField value="{!brorder.Order_Date__c}" required="True"/>
<apex:inputField value="{!brorder.Account__c}" required="True"/>
</apex:pageBlockSection>
</apex:pageBlock>
</td>
</tr>
</table>
<apex:pageBlock >
<apex:pageBlockTable value="{!bimaster}" var="b">
<apex:column headerValue="Branding Items" value="{!b.name}"/>
<apex:column headerValue="Quantity">
<apex:inputField value="{!b.Quantity__c}"/>
</apex:column>
<apex:column headerValue="Remarks">
<apex:inputField value="{!b.Remarks__c}"/>
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Save" action="{!save}"/>
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
controller:
public class Brandingcls
{
public Branding_Visit__c brvisit{get;set;}
public Branding_Order__c brorder{get;set;}
public list<Branding_Item_Master__c> bimaster{get;set;}
public list<Branding_Line_Item__c> brlineitem{get;set;}
public Brandingcls()
{
bimaster=[select id,Name,Remarks__c,Quantity__c from Branding_Item_Master__c];
brorder=new Branding_Order__c(Order_Date__c=system.Today());
brvisit=new Branding_Visit__c();
}
public PageReference save()
{
list<Branding_Item_Master__c> lstbritem =new list<Branding_Item_Master__c>();
for(Branding_Item_Master__c bim :bimaster)
{
if(bim.Quantity__c>0)
{
lstbritem.add(bim);
}
}
if(lstbritem.Size()>0)
{
insert brorder;
brvisit.Account__c = brorder.Account__c;
brvisit.Visit_Date__c = brorder.Order_Date__c;
insert brvisit;
list<Branding_Line_Item__c> lstbli = new list<Branding_Line_Item__c>();
for(Branding_Item_Master__c bm:lstbritem)
{
Branding_Line_Item__c brandli= new Branding_Line_Item__c();
brandli.Branding_Order__c=brorder.id;
brandli.Branding_Type__c=bm.id;
brandli.Quantity__c=bm.Quantity__c;
brandli.Remarks__c=bm.Remarks__c;
lstbli.add( brandli);
}
if(lstbli.Size()>0)
{
insert lstbli;
}
}
else
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Enter atleast One Quantity to Create Branding Visit and Branding Order'));
return Null;
}
pagereference ref=new pagereference( '/' +brvisit.id);
ref.setredirect(true);
return ref;
}
public PageReference cancel()
{
return null;
}
}
testclass:
@istest
public class testBrandingcls{
static testmethod void testsave(){
Branding_Order__c br=new Branding_Order__c(Order_Date__c=system.today(),Account__c='testnaveen');
insert br;
Branding_Visit__c bv=new Branding_Visit__c(Visit_Date__c=system.today(),Account__c='testvisit');
insert bv;
Branding_Line_Item__c blm=new Branding_Line_Item__c(Branding_Type__c='Branding_Item_Master__c.id',Branding_Order__c='Branding_Order__c.id',Quantity__c=12);
insert blm;
Brandingcls obj= new Brandingcls();
obj.save();
obj.cancel();
PageReference p=Page.Brandingvfpage;
Test.setCurrentPage(p);
}
}
Thanks in advance...
You can refer to the links which will help you write test classes for 100% coverage.
https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm
Thanks,
Pratik