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 for delete

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



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

}
Best Answer chosen by Sony PSP
Nayana KNayana K
@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', AccountId = testAccount.Id);
        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;
        
		OpportunityContactRole ocr = new OpportunityContactRole();
		ocr.ContactId = testContact.Id;
		ocr.OpportunityId = testOpportunity.Id;
		ocr.IsPrimary = TRUE;
		ocr.Role = 'Decision Maker';
		insert ocr;
        
        OpportunityContactRoleController controller = new OpportunityContactRoleController(new ApexPages.StandardController(testOpportunity));
        
        controller.contRoleId = ocr.Id;
		system.assertEquals(1, [SELECT COUNT() FROM OpportunityContactRole]);
        controller.deleteOppContRole();
		system.assertEquals(0, [SELECT COUNT() FROM OpportunityContactRole]);
 
        controller.primaryContact();
        
        
    }

}

 

All Answers

Nayana KNayana K
@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', AccountId = testAccount.Id);
        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;
        
		OpportunityContactRole ocr = new OpportunityContactRole();
		ocr.ContactId = testContact.Id;
		ocr.OpportunityId = testOpportunity.Id;
		ocr.IsPrimary = TRUE;
		ocr.Role = 'Decision Maker';
		insert ocr;
        
        OpportunityContactRoleController controller = new OpportunityContactRoleController(new ApexPages.StandardController(testOpportunity));
        
        controller.contRoleId = ocr.Id;
		system.assertEquals(1, [SELECT COUNT() FROM OpportunityContactRole]);
        controller.deleteOppContRole();
		system.assertEquals(0, [SELECT COUNT() FROM OpportunityContactRole]);
 
        controller.primaryContact();
        
        
    }

}

 
This was selected as the best answer
Sony PSPSony PSP
great its working but receive this errror

System.NullPointerException: Attempt to de-reference a null object