+ Start a Discussion

problem with making a test class

hi guys, i have difficulties on making a test class for the getContact() method.

can anyone help me or give me an example?

this is the class:



public class OpportunityQuotationPrintPdf_Extension {
private final Opportunity opportObj;

private string opportId = System.currentPageReference().getParameters().get('id');

private Id contactId;

private Contact theContact;

public OpportunityQuotationPrintPdf_Extension(ApexPages.StandardController stdController) {
this.opportObj = (Opportunity)stdController.getRecord();

public Contact getContact() {
OpportunityContactRole[] listOfOpport = [SELECT Id, ContactId FROM OpportunityContactRole
WHERE OpportunityId = :opportId
AND IsPrimary = true limit 1];
if (!listOfOpport.isEmpty()) {
contactId = listOfOpport[0].ContactId;
theContact = [SELECT Id, Name FROM Contact WHERE Id = :contactId];
return theContact;
else {
theContact = [SELECT Id FROM Contact WHERE FirstName like 'Epi'];
return theContact;


 and this is my attempt:



static TestMethod void testOpportunityQuotationPrintPdf_Extension() {

Opportunity testOpportObj = [SELECT Id FROM Opportunity WHERE Opportunity_Number__c = 'OPP-0098'];

PageReference pageR = new PageReference('/apex/OpportunityQuotationPrintPdf_Extension');
ApexPages.currentPage().getParameters().put('id', testOpportObj.Id);

ApexPages.StandardController standardC = new ApexPages.StandardController(testOpportObj);
OpportunityQuotationPrintPdf_Extension myOpportunity = new OpportunityQuotationPrintPdf_Extension(standardC);

Contact contactTest = [SELECT Id FROM Contact WHERE FirstName like 'Epi'];
Contact contactReal = myOpportunity.getContact();
System.assertEquals(contactReal, contactTest);


the getContact() method is running normally when i call it from the visualforce page ---> {!contact.Name}  if i want to get the Name of the contact.

but i am struggling to get a good 100% coverage test method.

it always go to the 'else' whenever i call it from my test method.

i have tried to make a new opportunity object, new contact object, and new opportunitycontactrole object and not using the current exist data like the code above, but still it did not work.


can anyone help me?

thanks in advance.




You using an existing Opportunity you have to create an opportunity .


Create an Opportunity without Opportunitcontactrolethen run your getcontact() method then add some Opportunitcontactrole


and rerun  getcontact();




In unit tests, you should always create your own dummy data at the start of the test case.  That way you can be sure of the values that returned from the code that you are testing

static TestMethod void testOpportunityQuotationPrintPdf_Extension() { Account acct = new Account(Name = 'Apex Test Account'); insert acct; Opportunity opp = new Opportunity(Name = ...... ); insert opp; //your test code goes here. //your system assertions go here. }


Records that you create in unit tests are not put into the production database.