You need to sign in to do that
Don't have an account?
soorya r
My Test is not run as expected
Hi all,
Here is my controller:
public with sharing class positionfetch {
public candidate__c candidate {get;set;} //Used For get position id
public candidate__c delrecord {get;set;}
public list<candidate__c> cand {get;set;} //Used For query candidates
public list<candidatewrapper> selectedcandidates1 {get;set;}
public list<candidate__c> selcand {get;set;}
public string detail {get;set;}
public boolean add1 {get;set;}
public boolean addsec {get;set;}
public boolean sec {get;set;}
public string delid {get;set;}
public string canid;
public string gender;
public integer count1;
public positionfetch() {
candidate = new candidate__c(); // initialize the object which is used to empty it
cand = new list<candidate__c>();
selcand = new list<candidate__c>();
delrecord = new candidate__c();
addsec = false;
sec = false;
}
public void fetch() { // fetch the list of record for particular position & gender
selectedcandidates1 = new list<candidatewrapper>();
system.debug('^^^^^selectedcandidates1' +selectedcandidates1);
system.debug('candidate -->>>'+candidate.position_apply_to__c);
system.debug('candidate -->>>'+candidate.Gender__c);
canid = candidate.position_apply_to__c; //Used for assign positionid
gender = candidate.Gender__c; //Used for assign gender**/
String query = 'SELECT Id, Name, Education_qualification__c,address__c FROM candidate__c WHERE Id != Null '; // Dynamic SoQL Query
if(gender == null) {
query += 'AND position_apply_to__c =: canid';
sec = true;
}
else {
query += 'AND position_apply__to_c =: canid AND gender__c =: gender';
sec = true;
}
cand = Database.query(query);
system.debug('--->'+cand.size());
for(candidate__c can : cand) { // add records to wrapperlist
candidatewrapper candInst = new candidatewrapper();
candInst.candidate1 = can;
selectedcandidates1.add(candInst);
}
system.debug('^^^^^selectedcandidates1' +selectedcandidates1);
if(cand.size() == 0) {
sec = false;
system.debug('*****sec' +sec);
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'There is no child records to display'));
}
add1 = true;
addsec = false;
}
public pagereference detailpage() { // while click link leads to detailpage of record
system.debug('---->>detail' +detail);
pagereference page;
page = new pagereference('/'+detail);
page.setredirect(true);
return page;
}
public void Deleterecord() { // Delete records from object
system.debug('$$$$$$$$$$$$$' +delid);
try {
count1 = [select count() from candidate__c where id = : delid];
delrecord = [select id, name, address__c, Education_qualification__c from candidate__c where id=: delid];
if(count1 > 0) {
system.debug('----->>>' +delrecord);
delete delrecord;
system.debug('lllll' +delrecord);
fetch();
}
}
catch(Exception e) {
system.debug('--->>>e' +e);
}
}
public void selectedcandidates() { //display selected records
addsec =false;
system.debug('selectedcandidates1 --------------->>>'+selectedcandidates1);
selcand = new list<candidate__c>();
for(candidatewrapper candwrap : selectedcandidates1) {
if(candwrap.isselected == true) {
addsec = true;
selcand.add(candwrap.candidate1);
system.debug('----->>>>>>selcand' +selcand.size());
}
}
if(selcand.size() == 0){
addsec =false;
system.debug('======' +selcand.size());
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.error,'Please select the candidates'));
}
}
public class candidatewrapper{ //wrapper class for candidate
public candidate__c candidate1 {get;set;}
public boolean isselected {get;set;}
public candidatewrapper() {
isselected = false;
}
}
}
Here is my Test class for above controller:
@isTest
public class Testpositionfetch {
public static testmethod void method1() {
position__c pos = new position__c();
pos.Name = 'sss';
pos.Skills_Requried__c = 'JAVA';
pos.close_date__c = Date.parse('12/9/2015');
insert pos;
candidate__c candidate = new candidate__c();
candidate.Name = 'ss1';
candidate.position_apply_to__c = pos.id;
candidate.Education_Qualification__c ='BE';
candidate.Gender__c ='male';
insert candidate;
Test.StartTest();
PageReference pageref = Page.positionfetch;
pageRef.getParameters().put('id', String.valueOf(candidate.Id));
Test.setCurrentPage(pageref);
positionfetch position = new positionfetch();
positionfetch.candidatewrapper CandInst = new positionfetch.candidatewrapper();
list<positionfetch.candidatewrapper> CandWrapList = new list<positionfetch.candidatewrapper>();
CandInst.candidate1 = candidate;
CandInst.isselected = true;
CandWrapList.add(CandInst);
position.selectedcandidates1 = CandWrapList;
position.fetch();
position.detailpage();
position.delid = candidate.id;
position.Deleterecord();
position.selectedcandidates();
Test.StopTest();
}
public static testmethod void method2() {
position__c pos = new position__c();
pos.Name = 'sss';
pos.Skills_Requried__c = 'JAVA';
pos.close_date__c = Date.parse('12/9/2015');
insert pos;
candidate__c candidate = new candidate__c();
candidate.Name = 'ss1';
candidate.position_apply_to__c = pos.id;
candidate.Education_Qualification__c ='BE';
insert candidate;
Test.StartTest();
PageReference pageref = Page.positionfetch;
pageRef.getParameters().put('id', String.valueOf(candidate.Id));
Test.setCurrentPage(pageref);
positionfetch position = new positionfetch();
positionfetch.candidatewrapper CandInst = new positionfetch.candidatewrapper();
list<positionfetch.candidatewrapper> CandWrapList = new list<positionfetch.candidatewrapper>();
CandInst.candidate1 = candidate;
CandInst.isselected = false;
CandWrapList.add(CandInst);
position.selectedcandidates1 = CandWrapList;
position.fetch();
position.detailpage();
//position.delid = candidate.id;
position.Deleterecord();
position.selectedcandidates();
Test.StopTest();
}
}
I am having 88% code coverage only i want to acheive 100% coverage is there any wrong plz help me!. Thanks in Advance
Here is my controller:
public with sharing class positionfetch {
public candidate__c candidate {get;set;} //Used For get position id
public candidate__c delrecord {get;set;}
public list<candidate__c> cand {get;set;} //Used For query candidates
public list<candidatewrapper> selectedcandidates1 {get;set;}
public list<candidate__c> selcand {get;set;}
public string detail {get;set;}
public boolean add1 {get;set;}
public boolean addsec {get;set;}
public boolean sec {get;set;}
public string delid {get;set;}
public string canid;
public string gender;
public integer count1;
public positionfetch() {
candidate = new candidate__c(); // initialize the object which is used to empty it
cand = new list<candidate__c>();
selcand = new list<candidate__c>();
delrecord = new candidate__c();
addsec = false;
sec = false;
}
public void fetch() { // fetch the list of record for particular position & gender
selectedcandidates1 = new list<candidatewrapper>();
system.debug('^^^^^selectedcandidates1' +selectedcandidates1);
system.debug('candidate -->>>'+candidate.position_apply_to__c);
system.debug('candidate -->>>'+candidate.Gender__c);
canid = candidate.position_apply_to__c; //Used for assign positionid
gender = candidate.Gender__c; //Used for assign gender**/
String query = 'SELECT Id, Name, Education_qualification__c,address__c FROM candidate__c WHERE Id != Null '; // Dynamic SoQL Query
if(gender == null) {
query += 'AND position_apply_to__c =: canid';
sec = true;
}
else {
query += 'AND position_apply__to_c =: canid AND gender__c =: gender';
sec = true;
}
cand = Database.query(query);
system.debug('--->'+cand.size());
for(candidate__c can : cand) { // add records to wrapperlist
candidatewrapper candInst = new candidatewrapper();
candInst.candidate1 = can;
selectedcandidates1.add(candInst);
}
system.debug('^^^^^selectedcandidates1' +selectedcandidates1);
if(cand.size() == 0) {
sec = false;
system.debug('*****sec' +sec);
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'There is no child records to display'));
}
add1 = true;
addsec = false;
}
public pagereference detailpage() { // while click link leads to detailpage of record
system.debug('---->>detail' +detail);
pagereference page;
page = new pagereference('/'+detail);
page.setredirect(true);
return page;
}
public void Deleterecord() { // Delete records from object
system.debug('$$$$$$$$$$$$$' +delid);
try {
count1 = [select count() from candidate__c where id = : delid];
delrecord = [select id, name, address__c, Education_qualification__c from candidate__c where id=: delid];
if(count1 > 0) {
system.debug('----->>>' +delrecord);
delete delrecord;
system.debug('lllll' +delrecord);
fetch();
}
}
catch(Exception e) {
system.debug('--->>>e' +e);
}
}
public void selectedcandidates() { //display selected records
addsec =false;
system.debug('selectedcandidates1 --------------->>>'+selectedcandidates1);
selcand = new list<candidate__c>();
for(candidatewrapper candwrap : selectedcandidates1) {
if(candwrap.isselected == true) {
addsec = true;
selcand.add(candwrap.candidate1);
system.debug('----->>>>>>selcand' +selcand.size());
}
}
if(selcand.size() == 0){
addsec =false;
system.debug('======' +selcand.size());
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.error,'Please select the candidates'));
}
}
public class candidatewrapper{ //wrapper class for candidate
public candidate__c candidate1 {get;set;}
public boolean isselected {get;set;}
public candidatewrapper() {
isselected = false;
}
}
}
Here is my Test class for above controller:
@isTest
public class Testpositionfetch {
public static testmethod void method1() {
position__c pos = new position__c();
pos.Name = 'sss';
pos.Skills_Requried__c = 'JAVA';
pos.close_date__c = Date.parse('12/9/2015');
insert pos;
candidate__c candidate = new candidate__c();
candidate.Name = 'ss1';
candidate.position_apply_to__c = pos.id;
candidate.Education_Qualification__c ='BE';
candidate.Gender__c ='male';
insert candidate;
Test.StartTest();
PageReference pageref = Page.positionfetch;
pageRef.getParameters().put('id', String.valueOf(candidate.Id));
Test.setCurrentPage(pageref);
positionfetch position = new positionfetch();
positionfetch.candidatewrapper CandInst = new positionfetch.candidatewrapper();
list<positionfetch.candidatewrapper> CandWrapList = new list<positionfetch.candidatewrapper>();
CandInst.candidate1 = candidate;
CandInst.isselected = true;
CandWrapList.add(CandInst);
position.selectedcandidates1 = CandWrapList;
position.fetch();
position.detailpage();
position.delid = candidate.id;
position.Deleterecord();
position.selectedcandidates();
Test.StopTest();
}
public static testmethod void method2() {
position__c pos = new position__c();
pos.Name = 'sss';
pos.Skills_Requried__c = 'JAVA';
pos.close_date__c = Date.parse('12/9/2015');
insert pos;
candidate__c candidate = new candidate__c();
candidate.Name = 'ss1';
candidate.position_apply_to__c = pos.id;
candidate.Education_Qualification__c ='BE';
insert candidate;
Test.StartTest();
PageReference pageref = Page.positionfetch;
pageRef.getParameters().put('id', String.valueOf(candidate.Id));
Test.setCurrentPage(pageref);
positionfetch position = new positionfetch();
positionfetch.candidatewrapper CandInst = new positionfetch.candidatewrapper();
list<positionfetch.candidatewrapper> CandWrapList = new list<positionfetch.candidatewrapper>();
CandInst.candidate1 = candidate;
CandInst.isselected = false;
CandWrapList.add(CandInst);
position.selectedcandidates1 = CandWrapList;
position.fetch();
position.detailpage();
//position.delid = candidate.id;
position.Deleterecord();
position.selectedcandidates();
Test.StopTest();
}
}
I am having 88% code coverage only i want to acheive 100% coverage is there any wrong plz help me!. Thanks in Advance
If not, please do it and it should show you the lines which remain uncovered in the test class.