+ Start a Discussion
Sony PSPSony PSP 

Test Class Run Error

HI,

I have this class

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;
   }
    
}



Test Class :


@isTest
public class OpportunityContactRoleController_Test {
    
    private static testMethod void testData(){
        
 
        Account testAccount = new Account(Name = 'Test Account', Country__c = 'Denmark');
        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 = 'Denmark', 
                                                      
                                                      CurrencyIsoCode = 'USD', StageName = 'Loan Current', CloseDate = date.today(), Days_In_Term__c = 2 );
        insert testOpportunity;
        

        OpportunityContactRoleController controller = new OpportunityContactRoleController(new ApexPages.StandardController(testOpportunity));
  
        controller.primaryContact();

        OpportunityContactRole ocr = new OpportunityContactRole(OpportunityId = testOpportunity.Id, ContactId = testContact.Id );
        insert ocr;
        
       
        controller.contRoleId = ocr.Id;
        controller.deleteOppContRole();
        
}
}



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
Best Answer chosen by Sony PSP
ra1ra1
Hi Sony,

I believe, you are getting NPE on "((Opportunity)controller.getRecord()).Id" in deleteOppContRole method, alternatively you can replace it with oppId (as you are already storing Opportunity Id in this variable).

The update method should look like:
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 = :oppId];
        }
    }


Hope this helps !!

Thanks


 

All Answers

ra1ra1
Hi Sony,

I believe, you are getting NPE on "((Opportunity)controller.getRecord()).Id" in deleteOppContRole method, alternatively you can replace it with oppId (as you are already storing Opportunity Id in this variable).

The update method should look like:
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 = :oppId];
        }
    }


Hope this helps !!

Thanks


 
This was selected as the best answer
Sony PSPSony PSP
Thank you!Great! :D