function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sony PSPSony PSP 

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;
   }
    
}
Best Answer chosen by Sony PSP
_Zach Boyd_Zach Boyd
It looks like your deleteOppContRole method does not have complete coverage because you didn't set contRoleId on the OpportunityContactRoleController controller and you didn't actually insert the OpportunityContactRole that was instantiated. Here is a sample below that should meet code coverage requirements, but I would also recommend structuring the unit test to not just complete code coverage, but to also validate/assert the expected outcome.
 
OpportunityContactRoleController controller = new OpportunityContactRoleController(new ApexPages.StandardController(testOpportunity));
        
OpportunityContactRole ocr = new OpportunityContactRole( OpportunityId = testOpportunity.Id, ContactId = testContact.Id );
        
insert ocr;
        
controller.contRoleId = ocr.Id;
        
controller.deleteOppContRole();

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.
 
oppContRoles = [SELECT Id,OpportunityId, Contact.Name, Contact.Email, Role, Contact.Account.Name, Contact.Phone, IsPrimary
                            FROM OpportunityContactRole WHERE OpportunityId = :oppId];

 

All Answers

_Zach Boyd_Zach Boyd
Could you post your test class for review as well?
Sony PSPSony PSP
Hi Zach here is my test class

@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();
        
        
    }

}
Sony PSPSony PSP
I need to have do the delete but I do not have much idea on how to do it.

 
_Zach Boyd_Zach Boyd
It looks like your deleteOppContRole method does not have complete coverage because you didn't set contRoleId on the OpportunityContactRoleController controller and you didn't actually insert the OpportunityContactRole that was instantiated. Here is a sample below that should meet code coverage requirements, but I would also recommend structuring the unit test to not just complete code coverage, but to also validate/assert the expected outcome.
 
OpportunityContactRoleController controller = new OpportunityContactRoleController(new ApexPages.StandardController(testOpportunity));
        
OpportunityContactRole ocr = new OpportunityContactRole( OpportunityId = testOpportunity.Id, ContactId = testContact.Id );
        
insert ocr;
        
controller.contRoleId = ocr.Id;
        
controller.deleteOppContRole();

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.
 
oppContRoles = [SELECT Id,OpportunityId, Contact.Name, Contact.Email, Role, Contact.Account.Name, Contact.Phone, IsPrimary
                            FROM OpportunityContactRole WHERE OpportunityId = :oppId];

 
This was selected as the best answer
Sony PSPSony PSP
thank you! Very well explained! apologies just a beginner with salesforce :)
 
Sony PSPSony PSP
I still encounter this error

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
_Zach Boyd_Zach Boyd
This was actually answered in my original answer since I saw that it would be a problem. You reference the controller variable in the SOQL query which is null. You are already storing the oppId so my example above modified the query to use that.
Sony PSPSony PSP
thanks men! :D