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

Test class for custom controller of visualforce
Please help in writing test class.I have tried writing it,and has achieved 8% coverage.I m stusck how to cover if statement.Please guide me..
Visualforce page:
<apex:page controller="createJAfornewCand">
<apex:form >
<apex:pageblock title="New Candidate">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Information" collapsible="false" showHeader="true">
<apex:inputField value="{!c.First_Name__c}"/>
<apex:inputField value="{!c.Last_Name__c}"/>
<apex:inputField value="{!c.Years_of_Experience__c}"/>
<apex:inputField value="{!c.SSN__c}"/>
<apex:inputField value="{!c.Technology__c}"/>
<apex:inputField value="{!c.Phone__c}"/>
<apex:inputField value="{!c.Mobile__c}"/>
<apex:inputField value="{!c.Email__c}"/>
</apex:pageBlockSection>
</apex:pageblock>
</apex:form>
</apex:page>
Controller:
public class createJAfornewCand
{
Candidate__c c;
List<Position__c> p;
public Candidate__c getC()
{
if(c==null)
c=new Candidate__c();
return c;
}
public void setC(Candidate__c v)
{
v=c;
}
public PageReference save()
{
insert c;
Decimal d=c.Years_of_Experience__c;
if(c.Technology__c=='Java')
p =[select id from position__c where Experience_Required__c<= :d and java__c=true];
if(c.Technology__c=='Apex')
p =[select id from position__c where Experience_Required__c<= :d and apex__c=true];
if(c.Technology__c=='C#')
p =[select id from position__c where Experience_Required__c<= :d and c__c=true];
if(p.size()>0)
{
for(Position__c pos:p)
{
Job_application__c ja =new Job_application__c ();
ja.position__c=pos.id;
ja.candidate__c=c.id;
insert ja;
}
}
return null;
}
}
Test class:
@isTest
public class conrollerTest
{
static testmethod void createja()
{
Position__C p=new Position__C(Name='Tester',Experience_Required__c=9,java__c=true);
insert p;
Candidate__c c=new Candidate__C(First_Name__c='dd',Years_of_Experience__c=10,Technology__c='Java');
Test.StartTest();
PageReference pageRef = Page.createJAfornewCandidate;
Test.setCurrentPage(pageRef);
createJAfornewCand con= new createJAfornewCand();
insert c;
con.save();
Job_Application__c js=[select id from Job_Application__c where candidate__C= :c.id];
System.assertEquals(js.position__c,p.id);
Test.StopTest();
}
}
Visualforce page:
<apex:page controller="createJAfornewCand">
<apex:form >
<apex:pageblock title="New Candidate">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Information" collapsible="false" showHeader="true">
<apex:inputField value="{!c.First_Name__c}"/>
<apex:inputField value="{!c.Last_Name__c}"/>
<apex:inputField value="{!c.Years_of_Experience__c}"/>
<apex:inputField value="{!c.SSN__c}"/>
<apex:inputField value="{!c.Technology__c}"/>
<apex:inputField value="{!c.Phone__c}"/>
<apex:inputField value="{!c.Mobile__c}"/>
<apex:inputField value="{!c.Email__c}"/>
</apex:pageBlockSection>
</apex:pageblock>
</apex:form>
</apex:page>
Controller:
public class createJAfornewCand
{
Candidate__c c;
List<Position__c> p;
public Candidate__c getC()
{
if(c==null)
c=new Candidate__c();
return c;
}
public void setC(Candidate__c v)
{
v=c;
}
public PageReference save()
{
insert c;
Decimal d=c.Years_of_Experience__c;
if(c.Technology__c=='Java')
p =[select id from position__c where Experience_Required__c<= :d and java__c=true];
if(c.Technology__c=='Apex')
p =[select id from position__c where Experience_Required__c<= :d and apex__c=true];
if(c.Technology__c=='C#')
p =[select id from position__c where Experience_Required__c<= :d and c__c=true];
if(p.size()>0)
{
for(Position__c pos:p)
{
Job_application__c ja =new Job_application__c ();
ja.position__c=pos.id;
ja.candidate__c=c.id;
insert ja;
}
}
return null;
}
}
Test class:
@isTest
public class conrollerTest
{
static testmethod void createja()
{
Position__C p=new Position__C(Name='Tester',Experience_Required__c=9,java__c=true);
insert p;
Candidate__c c=new Candidate__C(First_Name__c='dd',Years_of_Experience__c=10,Technology__c='Java');
Test.StartTest();
PageReference pageRef = Page.createJAfornewCandidate;
Test.setCurrentPage(pageRef);
createJAfornewCand con= new createJAfornewCand();
insert c;
con.save();
Job_Application__c js=[select id from Job_Application__c where candidate__C= :c.id];
System.assertEquals(js.position__c,p.id);
Test.StopTest();
}
}
The code is written for three different skill set and you need to test it for all of them.
Hope it Helps!
It throwing error 'System.NullPointerException: Attempt to de-reference a null object' on the line con.save(); when test for one the scenario.
Hope this helps!
but if condition and for loop is not satitfying in test class...
please guide how to proceed