You need to sign in to do that
Don't have an account?
Mike Fitch
I need help getting code coverage for my apex class
I've only been able to get 72% code coverage for my apex class. I'm new to apex development. Can someone help me please?
Here is my class:
public class PcPContactInfo {
public Precall_Plan_2__c c{get; set;}
public PcPContactInfo(ApexPages.StandardController controller)
{
c = (Precall_Plan_2__c)controller.getRecord();
}
public pagereference logout()
{
pagereference p1=new pagereference('/apex/Precall_Plan_Detail_clone?id='+c.get('Id')+'&clone=1');
return p1;
}
public PageReference dosave()
{
try
{
if(ApexPages.currentPage().getParameters().get('clone')=='1'||ApexPages.currentPage().getParameters().get('save_new')=='1')
{
c.id=null;
insert c;
}
else
update c;
}
catch(exception e) { ApexPages.addMessages(e); return null; }
return new Pagereference('/' + c.get('Id'));
}
public void doSomething()
{
contact TEST = [SELECT account.name,MailingCountry,Mailingcity,Title,Phone,MailingStreet,MailingState,MailingPostalCode, accountid FROM contact where id = :c.Contact_name2__c];
// c.Company_Name__c= TEST.account.name;
c.Contact_mailing_city__c=TEST.Mailingcity;
c.Contact_s_Title__c=TEST.Title;
c.Contact_s_Telephone_Number__c=TEST.Phone;
c.Contact_Street__c=TEST.MailingStreet;
c.Contact_State__c=TEST.MailingState;
c.Contact_Zip__c=TEST.MailingPostalCode;
}
}
And here is my Test Class:
@isTest
private class test_PcPContactInfo
{
static testMethod void go_PcPContactInfo()
{
account a = new account(name='ESAB');
insert a;
contact c = new contact (lastname= 'test2',accountid=a.id,email='abc@gmail.com');
insert c;
Precall_Plan_2__c cw=new Precall_Plan_2__c(name='test',Contact_name2__c=c.id,Company_Name__c=a.id);
insert cw;
ApexPages.StandardController controller = new ApexPages.StandardController(cw);
PcPContactInfo stdController = new PcPContactInfo(controller);
//account a = [SELECT name,id FROM account limit 1];
//contact TEST = [SELECT id, accountid,email FROM contact where id = :cw.contact__c];
//cw.Email__c = TEST.email;
//update cw;
stdController.doSomething();
stdController.dosave();
}
}
Here is my class:
public class PcPContactInfo {
public Precall_Plan_2__c c{get; set;}
public PcPContactInfo(ApexPages.StandardController controller)
{
c = (Precall_Plan_2__c)controller.getRecord();
}
public pagereference logout()
{
pagereference p1=new pagereference('/apex/Precall_Plan_Detail_clone?id='+c.get('Id')+'&clone=1');
return p1;
}
public PageReference dosave()
{
try
{
if(ApexPages.currentPage().getParameters().get('clone')=='1'||ApexPages.currentPage().getParameters().get('save_new')=='1')
{
c.id=null;
insert c;
}
else
update c;
}
catch(exception e) { ApexPages.addMessages(e); return null; }
return new Pagereference('/' + c.get('Id'));
}
public void doSomething()
{
contact TEST = [SELECT account.name,MailingCountry,Mailingcity,Title,Phone,MailingStreet,MailingState,MailingPostalCode, accountid FROM contact where id = :c.Contact_name2__c];
// c.Company_Name__c= TEST.account.name;
c.Contact_mailing_city__c=TEST.Mailingcity;
c.Contact_s_Title__c=TEST.Title;
c.Contact_s_Telephone_Number__c=TEST.Phone;
c.Contact_Street__c=TEST.MailingStreet;
c.Contact_State__c=TEST.MailingState;
c.Contact_Zip__c=TEST.MailingPostalCode;
}
}
And here is my Test Class:
@isTest
private class test_PcPContactInfo
{
static testMethod void go_PcPContactInfo()
{
account a = new account(name='ESAB');
insert a;
contact c = new contact (lastname= 'test2',accountid=a.id,email='abc@gmail.com');
insert c;
Precall_Plan_2__c cw=new Precall_Plan_2__c(name='test',Contact_name2__c=c.id,Company_Name__c=a.id);
insert cw;
ApexPages.StandardController controller = new ApexPages.StandardController(cw);
PcPContactInfo stdController = new PcPContactInfo(controller);
//account a = [SELECT name,id FROM account limit 1];
//contact TEST = [SELECT id, accountid,email FROM contact where id = :cw.contact__c];
//cw.Email__c = TEST.email;
//update cw;
stdController.doSomething();
stdController.dosave();
}
}
Check out I have added some code to your existing test class
@isTest
private class test_PcPContactInfo
{
static testMethod void go_PcPContactInfo()
{
account a = new account(name='ESAB');
insert a;
contact c = new contact (lastname= 'test2',accountid=a.id,email='abc@gmail.com');
insert c;
Precall_Plan_2__c cw=new Precall_Plan_2__c(name='test',Contact_name2__c=c.id,Company_Name__c=a.id);
insert cw;
ApexPages.StandardController controller = new ApexPages.StandardController(cw);
PcPContactInfo stdController = new PcPContactInfo(controller);
//account a = [SELECT name,id FROM account limit 1];
//contact TEST = [SELECT id, accountid,email FROM contact where id = :cw.contact__c];
//cw.Email__c = TEST.email;
//update cw;
stdController.doSomething();
ApexPages.currentPage().getParameters().put('clone','1');
stdController.dosave();
stdController.logout();
}
}
Now I think it will cover the most of the part except the catch part.
Let me know after running the test.
Cheers.
All Answers
It woul be easy to help if you highlight the lines which are not covered.
public class PcPContactInfo {
public Precall_Plan_2__c c{get; set;}
public PcPContactInfo(ApexPages.StandardController controller)
{
c = (Precall_Plan_2__c)controller.getRecord();
}
public pagereference logout()
{
pagereference p1=new pagereference('/apex/Precall_Plan_Detail_clone?id='+c.get('Id')+'&clone=1');
return p1;
}
public PageReference dosave()
{
try
{
if(ApexPages.currentPage().getParameters().get('clone')=='1'||ApexPages.currentPage().getParameters().get('save_new')=='1')
{
c.id=null;
insert c;
}
else
update c;
}
catch(exception e) { ApexPages.addMessages(e); return null; }
return new Pagereference('/' + c.get('Id'));
}
public void doSomething()
{
contact TEST = [SELECT account.name,MailingCountry,Mailingcity,Title,Phone,MailingStreet,MailingState,MailingPostalCode, accountid FROM contact where id = :c.Contact_name2__c];
// c.Company_Name__c= TEST.account.name;
c.Contact_mailing_city__c=TEST.Mailingcity;
c.Contact_s_Title__c=TEST.Title;
c.Contact_s_Telephone_Number__c=TEST.Phone;
c.Contact_Street__c=TEST.MailingStreet;
c.Contact_State__c=TEST.MailingState;
c.Contact_Zip__c=TEST.MailingPostalCode;
}
}
Check out I have added some code to your existing test class
@isTest
private class test_PcPContactInfo
{
static testMethod void go_PcPContactInfo()
{
account a = new account(name='ESAB');
insert a;
contact c = new contact (lastname= 'test2',accountid=a.id,email='abc@gmail.com');
insert c;
Precall_Plan_2__c cw=new Precall_Plan_2__c(name='test',Contact_name2__c=c.id,Company_Name__c=a.id);
insert cw;
ApexPages.StandardController controller = new ApexPages.StandardController(cw);
PcPContactInfo stdController = new PcPContactInfo(controller);
//account a = [SELECT name,id FROM account limit 1];
//contact TEST = [SELECT id, accountid,email FROM contact where id = :cw.contact__c];
//cw.Email__c = TEST.email;
//update cw;
stdController.doSomething();
ApexPages.currentPage().getParameters().put('clone','1');
stdController.dosave();
stdController.logout();
}
}
Now I think it will cover the most of the part except the catch part.
Let me know after running the test.
Cheers.