1

I have a custom object which is a child to the Opportunity object. The custom object is viewed via visualforce page. Sometimes Users need to create payment records for the child object. This is achieved by calling a method on a controller extension. When pressed, the method will automatically create detail records using information in the custom object record.

I am trying to write a test class that will check the controller is working correctly and that the values copied over from the custom object are correct. I can't figure out how to call the method to create the payments. Can anyone help? Here is the test class:

@isTest                 
public class sipExt_Test {
    public static testMethod void testSIPExtController() {

    // Create Opportunity from test utilities class
    List<Opportunity> OppList = testUtilities.createTestOpps(1); 

    // Create the SIP
    bonus_calculator__c bc1 = new bonus_calculator__c(opportunity__c = OppList[0].Id);
    insert bc1;    

    PageReference ref = new PageReference('/apex/SIP2?id=' + bc1.Id);
    Test.setCurrentPage(ref);

    // Create SIP standard controller, pass it the SIP Record
    ApexPages.StandardController controller = new ApexPages.StandardController(bc1);

    // Pass the Controller to the Extension
    sipExt stdController = new sipExt(controller); 

    stdController.createPayments();           

    // Query for Payments fields
    List <Bonus_Payments__c> bPayList = [SELECT Id FROM Bonus_Payments__c WHERE Bonus_Record__c = :bc1.Id];    

        system.debug(bPayList[0].id);                

    }
}
  • If createPayments is the method you are talking about then you are calling it correctly. What is not working? – Keith C Dec 12 '14 at 10:43
  • When I query for the bonus payments at the bottom, I get an out of bounds error message which suggests that no records are being created. – Jay Dec 12 '14 at 11:15
  • OK, then there is a bug in either sipExt or the test code. I suggest you do some debugging yourself - see How do I start to debug my own Apex code? - and if you can't figure out what is wrong also post the sipExt code. – Keith C Dec 12 '14 at 12:01
  • I agree - I spent most of yesterday afternoon doing this. Thanks for taking a look all the same. I will post up the solution when I get there. – Jay Dec 12 '14 at 12:04
  • With some help, I found that the sales_associate values were null in the extension despite being populated when executed via the UI. – Jay Dec 12 '14 at 17:03
1

The call to createPayments is being correctly made.

So there is a bug or bugs in either sipExt or the test code. I suggest you do some debugging yourself - How do I start to debug my own Apex code? - and if you can't figure out what is wrong also post the sipExt code.

  • 1
    Some notes on how I debugged this: To debug this, I ran the process via the UI as an end user would and then monitored the results in the Dev Console. I then executed the apex test which simulates this process and monitored the results in the Dev Console. By comparing the results I was able to see that some key fields were not being updated when run through the Apex test. I was then able to update the test class as required. – Jay Dec 15 '14 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.