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
thinhtvuthinhtvu 

Can't get test class to run or cover any code

I'm trying to write a unit test class to cover the code in my controller extension, but I can't seem to get it to cover any of the code or even run properly. Could someone help me and let me know what's going on? I'm currently receiving this error message: 

 

System.DmlException : Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateAP: execution of AfterInsert

However, I'm not trying to work on the CreateAP triggers I have set up, just the drfExtension class I made for my VF pages.

 

Here's my class: 

public class drfExtension {
 
    // Declare the class variables maintaining the state of the wizard.
    // When users enter data into wizard, input is stored in variables.
   
    public ApexPages.StandardController opportunityController;
    public Opportunity opp {get; set;}
 
    public ApexPages.StandardController accountController {get; set;}
    public Account account {get; set;}
    
    public ApexPages.StandardSetController contactController {get; set;}
    public List<Contact> contact {get; set;}

    String warning = 'Your request has been denied because you either do not have a Vendor account ' +
        'associated with the opportunity, or your opportunity does not have any contact relationships. ' +
        'Please go back and edit your opportunity to include at least one contact and one ' +
        'vendor relationship before continuing.'; 

    // Creates the controller for the extension.
       
    public drfExtension(ApexPages.StandardController std)
    {
        opportunityController = std;      
        opp = [SELECT AccountId, Name, Lease_Number__c, OwnerId, Billing_Street__c, Billing_City__c,
            Billing_State__c, Billing_Zip_Code__c, Equipment_Street__c, Equipment_City__c, 
            Equipment_State__c, Equipment_Zip_Code__c, Equipment_Costs__c, Bumped_Rate__c, Buy_Rate__c,
            Monthly_Payment__c, Points_to_PCC__c, Term__c, Commission_to_PCC__c, End_of_Term_Options__c,
            Advanced_Payments__c, Funding_Source_Doc_Fee__c, PCC_Doc_Fee__c, Inspection_Fee__c, Point_s_to_Close__c, 
            Total_of_All_Fees__c, Amount, Prefund_Signing__c, Prefund_Delivery__c, Prefund_Complete__c,
            Customer_Payment_s__c, Reimburse_to_Customer__c, Proof_of_Payment__c, Pass_Through_Service_Type__c,
            Pass_Through_Amount__c, Notes__c, Vendor__c, Vendor_Contact__c, Invoice_Contact__c 
            from Opportunity WHERE Id = :std.getId()];
            
        try {    
            account = [SELECT Id, Name, Phone, Fax, BillingStreet, BillingCity, BillingState,
                BillingPostalCode FROM Account Where Id = :opp.Vendor__c];
            contact = [SELECT Id, FirstName, LastName, MailingStreet, MailingCity, MailingState,
                MailingPostalCode, HomePhone, MobilePhone, Title from Contact where AccountId = :opp.AccountId];
        } catch (QueryException e) {
            e.setMessage(warning);
            e.getMessage();
        } 
        
        try{             
            accountController = new ApexPages.StandardController(account);
            contactController = new ApexPages.StandardSetController(contact);
        } catch (NullPointerException n) {
            n.setMessage(warning);
            n.getMessage();
        }

    }
    
    // Methods to return one of the three class variables.
    
    public Account getAccount(){
        return account;
    }
    
    public List<Contact> getContact(){
        return contact;
    }
    
    // Methods that manage the navigation of the wizard.

    public PageReference goToStep1() {
        return Page.drfStep1;
    }

    public PageReference goToStep2() {
        return Page.drfStep2;
    }
    
    public PageReference goToStep3() {
        return Page.drfStep3;
    }
 
    // Methods to alter the state of each object's data.
    
    public PageReference save()
    {
        try {    
            update opp;

            opportunityController.save();
            accountController.save();
            contactController.save();
        } catch (QueryException e) {
            e.setMessage(warning);
            e.getMessage();
        } catch (NullPointerException n) {
            n.setMessage(warning);
            n.getMessage();
        }          
        return null; 
    }
    
        public PageReference cancel()
    {
        try {    
            opportunityController.cancel();
            accountController.cancel();
            contactController.cancel();
        } catch (QueryException e) {
            e.setMessage(warning);
            e.getMessage();
        } catch (NullPointerException n) {
            n.setMessage(warning);
            n.getMessage();
        }            
        return null;
    }
    
    public PageReference finish()
    {
        PageReference opptyPage = new PageReference('/' + opp.id);
        opptyPage.setRedirect(true);
        return opptyPage;
    }
    
}

Here's my extension:

/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class testDrfExtension {

    static testMethod void drfExtensionTest() {
    	
    	Account a = new Account();
    	a.Name = 'ABC Organization';
		a.Phone = '(404) 212-1241';
		a.Fax = '(404) 122-5512';
		a.BillingStreet = '125 Asgard St';
		a.BillingCity = 'Atlanta';
		a.BillingState = 'Georgia';
		a.BillingPostalCode = '30312';
		
		insert a;
		
		Opportunity o = new Opportunity();
		o.AccountId = a.Id; 
		o.Name = 'XYZ Organization';
		o.Lease_Number__c = '12345';
		o.OwnerId = '005d0000000ji9m';
		o.StageName = 'Closed Won';
		o.ForecastCategoryName = 'Pipeline';
		o.Billing_Street__c = a.BillingStreet;
		o.Billing_City__c = a.BillingCity;
        o.Billing_State__c = a.BillingState;
        o.Billing_Zip_Code__c = a.BillingPostalCode;
        o.Equipment_Street__c = a.BillingStreet;
        o.Equipment_City__c = a.BillingCity;
        o.Equipment_State__c = a.BillingState;
        o.Equipment_Zip_Code__c =  a.BillingPostalCode;
        o.Equipment_Costs__c = null;
        o.Buy_Rate__c = null;
        o.Monthly_Payment__c = null;
        o.Points_to_PCC__c = null;
        o.Term__c = null;
        o.Commission_to_PCC__c = null;
        o.End_of_Term_Options__c = null;
        o.Advanced_Payments__c = null;
        o.Funding_Source_Doc_Fee__c = null;
        o.PCC_Doc_Fee__c = null;
        o.Inspection_Fee__c = null;
        o.Point_s_to_Close__c = null;
        o.Total_of_All_Fees__c = null;
        o.Amount = null;
        o.Prefund_Signing__c = null;
        o.Prefund_Delivery__c = null;
        o.Prefund_Complete__c = null;
        o.Customer_Payment_s__c = null;
        o.Reimburse_to_Customer__c = true;
        o.Proof_of_Payment__c = true;
        o.Pass_Through_Service_Type__c = null;
        o.Pass_Through_Amount__c = null;
        o.Notes__c = null;
        o.Vendor__c = null;
        o.Vendor_Contact__c = null;
        o.Invoice_Contact__c = null;
        
        insert o;
		
    	Contact c = new Contact();
    	c.FirstName = 'Thinh';
    	c.LastName = 'Vu';
    	
    	insert c;
		
		PageReference pageRef = Page.drfStep1;
    	Test.setCurrentPage(pageRef);
    	
    	ApexPages.StandardController std = new ApexPages.StandardController(o);
    	drfExtension controller = new drfExtension(std);
    	
    	System.assertEquals(a, controller.getAccount());
    }
}

 

vriavmvriavm

Do you have After triggers . Please chek that error message looks like you are trying to update a locked record in trigger which will happen commonly when you try to edit trigger record after update...

thinhtvuthinhtvu

I do have a trigger after an update. How would I fix this problem then?

Naidu PothiniNaidu Pothini
@isTest
private class testDrfExtension {

    static testMethod void drfExtensionTest() {
    	
    	Account a = new Account();
    	a.Name = 'ABC Organization';
		a.Phone = '(404) 212-1241';
		a.Fax = '(404) 122-5512';
		a.BillingStreet = '125 Asgard St';
		a.BillingCity = 'Atlanta';
		a.BillingState = 'Georgia';
		a.BillingPostalCode = '30312';
		
		insert a;
		
		Opportunity o = new Opportunity();
		o.AccountId = a.Id; 
		o.Name = 'XYZ Organization';
		o.Lease_Number__c = '12345';
	//      o.OwnerId = '005d0000000ji9m';                 
		o.StageName = 'Closed Won';
		o.ForecastCategoryName = 'Pipeline';
		o.Billing_Street__c = a.BillingStreet;
		o.Billing_City__c = a.BillingCity;
        o.Billing_State__c = a.BillingState;
        o.Billing_Zip_Code__c = a.BillingPostalCode;
        o.Equipment_Street__c = a.BillingStreet;
        o.Equipment_City__c = a.BillingCity;
        o.Equipment_State__c = a.BillingState;
        o.Equipment_Zip_Code__c =  a.BillingPostalCode;
        o.Equipment_Costs__c = null;
        o.Buy_Rate__c = null;
        o.Monthly_Payment__c = null;
        o.Points_to_PCC__c = null;
        o.Term__c = null;
        o.Commission_to_PCC__c = null;
        o.End_of_Term_Options__c = null;
        o.Advanced_Payments__c = null;
        o.Funding_Source_Doc_Fee__c = null;
        o.PCC_Doc_Fee__c = null;
        o.Inspection_Fee__c = null;
        o.Point_s_to_Close__c = null;
        o.Total_of_All_Fees__c = null;
        o.Amount = null;
        o.Prefund_Signing__c = null;
        o.Prefund_Delivery__c = null;
        o.Prefund_Complete__c = null;
        o.Customer_Payment_s__c = null;
        o.Reimburse_to_Customer__c = true;
        o.Proof_of_Payment__c = true;
        o.Pass_Through_Service_Type__c = null;
        o.Pass_Through_Amount__c = null;
        o.Notes__c = null;
        o.Vendor__c = null;
        o.Vendor_Contact__c = null;
        o.Invoice_Contact__c = null;
        
        insert o;
		
    	Contact c = new Contact();
    	c.FirstName = 'Thinh';
    	c.LastName = 'Vu';
    	
    	insert c;
		
		PageReference pageRef = Page.drfStep1;
    	Test.setCurrentPage(pageRef);
    	
    	ApexPages.StandardController std = new ApexPages.StandardController(o);
    	drfExtension controller = new drfExtension(std);
    	
    	System.assertEquals(a, controller.getAccount());
    }
}

 Try with out specifying the ownerId for the opportunity.

 

 

thinhtvuthinhtvu

Thanks, that helped get rid of the first error, but now I'm running into the problem where I my System.assertEquals is saying the actual result is null. I'm thinking this is to do with how I used it... any suggestions?

 

This is my first unit test so I'm not quite sure how it works.