You need to sign in to do that
Don't have an account?

Test Class not Covered
Hi Guys,
good day!
Just a newbie in here, can you assist me with below Apex Class I already made a Test Class and my Problem in the Test Class is the part of method deleteOppContRole() wherein it doesn't covered any help please ?
public class OpportunityContactRoleController {
public Id contRoleId {get; set;}
public Id oppId;
public Opportunity oppObj;
public ApexPages.StandardController controller {get; set;}
public List<OpportunityContactRole> oppContRoles {get; set;}
public OpportunityContactRoleController(ApexPages.StandardController controller){
this.oppObj = (Opportunity)controller.getRecord();
oppId = this.oppObj.Id;
oppContRoles = [SELECT Id,OpportunityId, Contact.Name, Contact.Email, Role, Contact.Account.Name, Contact.Phone, IsPrimary
FROM OpportunityContactRole WHERE OpportunityId = :((Opportunity)controller.getRecord()).Id];
}
public void deleteOppContRole(){
if(contRoleId != null){
OpportunityContactRole ocr = new OpportunityContactRole();
ocr.Id = contRoleId;
delete ocr;
oppContRoles = new List<OpportunityContactRole>();
oppContRoles = [SELECT Id,OpportunityId, Contact.Name, Contact.Email, Role, Contact.Account.Name, Contact.Phone, IsPrimary
FROM OpportunityContactRole WHERE OpportunityId = :((Opportunity)controller.getRecord()).Id];
}
}
public pageReference primaryContact() {
PageReference pageRef = Page.OpportunityPrimaryContact;
pageRef.getParameters().put('Id', oppId);
pageRef.setRedirect(true);
return pageRef;
}
}
good day!
Just a newbie in here, can you assist me with below Apex Class I already made a Test Class and my Problem in the Test Class is the part of method deleteOppContRole() wherein it doesn't covered any help please ?
public class OpportunityContactRoleController {
public Id contRoleId {get; set;}
public Id oppId;
public Opportunity oppObj;
public ApexPages.StandardController controller {get; set;}
public List<OpportunityContactRole> oppContRoles {get; set;}
public OpportunityContactRoleController(ApexPages.StandardController controller){
this.oppObj = (Opportunity)controller.getRecord();
oppId = this.oppObj.Id;
oppContRoles = [SELECT Id,OpportunityId, Contact.Name, Contact.Email, Role, Contact.Account.Name, Contact.Phone, IsPrimary
FROM OpportunityContactRole WHERE OpportunityId = :((Opportunity)controller.getRecord()).Id];
}
public void deleteOppContRole(){
if(contRoleId != null){
OpportunityContactRole ocr = new OpportunityContactRole();
ocr.Id = contRoleId;
delete ocr;
oppContRoles = new List<OpportunityContactRole>();
oppContRoles = [SELECT Id,OpportunityId, Contact.Name, Contact.Email, Role, Contact.Account.Name, Contact.Phone, IsPrimary
FROM OpportunityContactRole WHERE OpportunityId = :((Opportunity)controller.getRecord()).Id];
}
}
public pageReference primaryContact() {
PageReference pageRef = Page.OpportunityPrimaryContact;
pageRef.getParameters().put('Id', oppId);
pageRef.setRedirect(true);
return pageRef;
}
}
In the OpportunityContactRoleController you will also have a failure when the deleteOppContRole is actually executed. In the SOQL statement you are referencing the controller object on the class, but it was never actually set in the constructor so it will be null. Since you already have stored the oppId, you could go ahead and use that instead.
All Answers
@isTest
public class OpportunityContactRoleController_Test {
private static testMethod void testData(){
Account testAccount = new Account(Name = 'Test Account', Country__c = 'US');
insert testAccount;
Contact testContact = new Contact(LastName = 'Test LastName', FirstName = 'Test LastName');
insert testContact;
Opportunity testOpportunity = new Opportunity(AccountId = testAccount.Id, Name = 'Test Opportunity', Country__c = 'US',
CurrencyIsoCode = 'USD', StageName = 'Loan Current', CloseDate = date.today(), Days_In_Term__c = 2 );
insert testOpportunity;
OpportunityContactRoleController controller = new OpportunityContactRoleController(new ApexPages.StandardController(testOpportunity));
OpportunityContactRole ocr = new OpportunityContactRole();
controller.deleteOppContRole();
controller.primaryContact();
}
}
In the OpportunityContactRoleController you will also have a failure when the deleteOppContRole is actually executed. In the SOQL statement you are referencing the controller object on the class, but it was never actually set in the constructor so it will be null. Since you already have stored the oppId, you could go ahead and use that instead.
when I run the test class it fails and returns like this.
Class.OpportunityContactRoleController.deleteOppContRole: line 30, column 1
Class.OpportunityContactRoleController_Test.testData: line 33, column 1
System.NullPointerException: Attempt to de-reference a null object
Any Idea which returns a Null?
Apologies Still a newbiew in this