+ Start a Discussion
prasad1.3969574005596787E12prasad1.3969574005596787E12 

How to write a test class

Hi All,

I am trying to write a test class in the following code:

public class OpportunityContactRoleExtension{

public List<OpportunityContactRole> ContactRoles{get;set;}

public OpportunityContactRoleExtension(
ApexPages.StandardController controller
){
ContactRoles =
[
SELECT
Contact.Name,
Contact.Email,
Role
FROM
OpportunityContactRole
WHERE
OpportunityId =
:((Opportunity)controller.getRecord()).Id
];
}

Please Help me to solve
Pablo_RoldanPablo_Roldan
Hi,

Try this code:
@isTest
private class OpportunityContactRoleExtensionTest {
static testMethod void myUnitTest() {
Opportunity vOpp = new Opportunity();
vOpp.Name = 'OppTest';
vOpp.StageName = 'Prospecting';
vOpp.CloseDate = date.parse('04/16/2014');
insert vOpp;
OpportunityContactRoleExtension vOppCntRoleExt = new OpportunityContactRoleExtension(vOpp);
}
}

If this works, please don't forget mak it as Best answer to be sure that it's solved.
Thanks,
Pablo.
prasad1.3969574005596787E12prasad1.3969574005596787E12

Thankyou Roldan....!

THankyou for ur reply and answer..........

But., there is a error:

 

Compile Error: Invalid type: OpportunityContactRoleExtension at line 9 column 54

Pablo_RoldanPablo_Roldan
Could you show me your code indicating the lines, please?
prasad1.3969574005596787E12prasad1.3969574005596787E12
OpportunityContactRoleExtension vOppCntRoleExt = new OpportunityContactRoleExtension(vOpp);

Pablo_RoldanPablo_Roldan
Sorry, I did it, try this:
@isTest
private class OpportunityContactRoleExtensionTest {
static testMethod void myUnitTest() {
Opportunity vOpp = new Opportunity();
vOpp.Name = 'OppTest';
vOpp.StageName = 'Prospecting';
vOpp.CloseDate = date.parse('04/16/2014');
insert vOpp;
ApexPages.StandardController sc = new ApexPages.StandardController(testAccountPlanInsert);
OpportunityContactRoleExtension vOppCntRoleExt = new OpportunityContactRoleExtension(sc);
}
}
prasad1.3969574005596787E12prasad1.3969574005596787E12

Sorry...!

This is the line i getting error: line 9

ApexPages.StandardController sc = new ApexPages.StandardController(testAccountPlanInsert);

prasad1.3969574005596787E12prasad1.3969574005596787E12
 1:@isTest
 2:private class OpportunityContactRoleExtensionTest {
 3:static testMethod void myUnitTest() {
 4:Opportunity vOpp = new Opportunity();
 5:vOpp.Name = 'OppTest';
 6:vOpp.StageName = 'Prospecting';
 7:vOpp.CloseDate = date.parse('04/16/2014'); 
 8:insert vOpp;
 9:ApexPages.StandardController sc = new ApexPages.StandardController(testAccountPlanInsert);
10:OpportunityContactRoleExtension vOppCntRoleExt = new OpportunityContactRoleExtension(sc);
11:}
12:}

Compile Error: Invalid type: OpportunityContactRoleExtension at line 9 column 54
Pablo_RoldanPablo_Roldan
Yep.... sorry for that, but this code line was copied from one of my classes.
Try to change that line for the following:
ApexPages.StandardController sc = new ApexPages.StandardController(vOpp);
SFDC_DevloperSFDC_Devloper
Hi,
  
     try below code..

@isTest
private class OpportunityContactRoleExtensionTest {
static testMethod void myUnitTest() {
Opportunity vOpp = new Opportunity();
vOpp.Name = 'OppTest';
vOpp.StageName = 'Prospecting';
vOpp.CloseDate = date.parse('04/16/2014');
List<Opportunity> lstOpty=new List<Opportunity>();
lstOpty.add(vOpp);
insert lstOpty;

ApexPages.StandardController sc = new ApexPages.StandardController(lstOpty);
OpportunityContactRoleExtension vOppCntRoleExt = new OpportunityContactRoleExtension(sc);
}
}

Thanks,
Rockzz
Pablo_RoldanPablo_Roldan
Rockzz,

List are using on StandardSetController and he is trying to use StandardController.
So some changes in your code, and then works.
@isTest
private class OpportunityContactRoleExtensionTest {
static testMethod void myUnitTest() {
Opportunity vOpp = new Opportunity();
vOpp.Name = 'OppTest';
vOpp.StageName = 'Prospecting';
vOpp.CloseDate = date.parse('04/16/2014');
insert vOpp;

ApexPages.StandardController sc = new ApexPages.StandardController(vOpp);
OpportunityContactRoleExtension vOppCntRoleExt = new OpportunityContactRoleExtension(sc);
}
}

Thanks,
Pablo
SFDC_DevloperSFDC_Devloper
Yes pablo exactly...

@isTest
private class OpportunityContactRoleExtensionTest {
static testMethod void myUnitTest() {
Opportunity vOpp = new Opportunity();
vOpp.Name = 'OppTest';
vOpp.StageName = 'Prospecting';
vOpp.CloseDate = date.parse('04/16/2014');
insert vOpp;

ApexPages.StandardController sc = new ApexPages.StandardController(vOpp);
OpportunityContactRoleExtension vOppCntRoleExt = new OpportunityContactRoleExtension(sc);
}
}

Thanks,
Rockzz
Pablo_RoldanPablo_Roldan
Hi prasad,

Don't forget to click on 'Best answer' to know that this is solved if it works.

Thanks,
Pablo