You need to sign in to do that
Don't have an account?
cFang
Need Help Testing APEX Code
I am having difficulties getting >=75% code coverage on the following APEX code.
public class sampleDetailPageCon {
private List<Opportunity> oppz;
private Contact cntact;
public sampleDetailPageCon(ApexPages.StandardController controller) {
this.cntact= (Contact)controller.getRecord(); }
public List<Opportunity> getOppz() {
Contact con = [Select id, Account.id FROM Contact where id = :cntact.id];
if (con.Account == null)
return null;
oppz = [Select id, Name, Account.Name, CloseDate, Amount, Type from Opportunity where Account.id = :con.Account.id];
return oppz;
}
}
The bold section is the section that I cannot get code coverage on. Does anyone have any suggestions on how to test it?
I appreciate any assistance.
The bold section is the section that I cannot get code coverage on. Does anyone have any suggestions on how to test it?
I appreciate any assistance.
You haven't invoked the getOppz method from the controller as far as I can see.
All Answers
This sounds like you need to invoke that method and assert against the response. Can you post your test method code?
This is one of the first APEX codes if written and tested. I borrowed the original code from a blog. Here is what I've put together based off of my own research. I don't think it is correct.
@isTest
public class sampletest {
public static testmethod void testsampledetailpagecon(){
account acc = new account(Name='acme');
contact cntact = new contact(lastName='test contact', AccountID=acc.ID);
opportunity oppz = new opportunity(Name='Behind the Cloud', AccountID=acc.ID, stagename='Prospecting',closedate=Date.valueof('2011-12-14'));
//Insert Account, opportunity, and contact
insert acc;
insert oppz;
insert cntact;
Contact con=[select id, Account.id FROM Contact where id= :cntact.id];
if(con.account== null)
return;
oppz=[select id, Name, Account.name, CloseDate from Opportunity where Account.id = :con.Account.id];
return;
//Check for large quantity of records
List<Opportunity> oppzList = new List<Opportunity>();
for(integer x=0;x<200;x++){
oppzList.add(new opportunity(AccountID=acc.id,StageName='Prospecting',closedate=Date.valueof('2011-12-14')));
}
insert oppzList;
List<Contact> cntactList = new List<Contact>();
for(integer i=0;i<200;i++){
cntactList.add(new contact(LastName='test',AccountID=acc.id));
}
insert cntactList;
Contact conlist=[select id, Account.id FROM Contact where id= :cntact.id];
if(conlist.account== null)
return;
oppzlist=[select id, Name, Account.name, CloseDate from Opportunity where Account.id = :acc.id];
return;
}
}
This looks to me like you are setting up lots of data, but not actually instantiating the controller. Once all the data is in place, you need to create an instance of the controller, something like:
then you can execute the method.
This is my test code now. The bolded text is new. I still only get 40% coverage.
@isTest
public class sampletest {
public static testmethod void testsampledetailpagecon(){
account acc = new account(Name='Acme');
contact cntact = new contact(lastName='test contact', AccountID=acc.ID);
opportunity oppz = new opportunity(Name='Behind the Cloud', AccountID=acc.ID, stagename='Prospecting',closedate=Date.valueof('2011-12-14'));
//Insert Account, opportunity, and contact
insert acc;
insert oppz;
insert cntact;
//Test converge for the visual force page
PageReference pageRef=Page.sampledetailpage;
test.setcurrentpagereference(pageref);
//create first contact
ApexPages.StandardController sc=new
ApexPages.standardController(cntact);
//create an instance of the controller
sampledetailpagecon myPageCon=new sampledetailpagecon(sc);
//calling methods/properties in all possible scenarios
Contact con=[select id, Account.id FROM Contact where id= :cntact.id];
if(con.account== null)
return;
oppz=[select id, Name, Account.name, CloseDate from Opportunity where Account.id = :con.Account.id];
return;
system.assertequals(cntact.AccountID,oppz.AccountID);
//Check for large quantity of records
List<Opportunity> oppzList = new List<Opportunity>();
for(integer x=0;x<200;x++){
oppzList.add(new opportunity(AccountID=acc.id,StageName='Prospecting',closedate=Date.valueof('2011-12-14')));
}
insert oppzList;
List<Contact> cntactList = new List<Contact>();
for(integer i=0;i<200;i++){
cntactList.add(new contact(LastName='test',AccountID=acc.id));
}
insert cntactList;
Contact conlist=[select id, Account.id FROM Contact where id= :cntact.id];
if(conlist.account== null)
return;
oppzlist=[select id, Name, Account.name, CloseDate from Opportunity where Account.id = :acc.id];
return;
system.assertequals(contact.accountID,opportunity.AccountID);
}
}
You haven't invoked the getOppz method from the controller as far as I can see.
After you instantiate the controller, you can call the 'getOppz()' method.
e.g.
You can also assert on the list of opportunities returned.
Much appreciated, calling the getOppz() method pushed me to 80%. Thank you very much.