0

Hopefully this will be an easy one and I am just missing something small.

I have a method queries for a custom object. My test class isn't seeing the custom object I have created in the test class. Am I out of order? Why would my test class not be able to query for the test record I inserted? There are only two filters in the where clause:

List<Credit_Review__c> creditReviewList = [select Opportunity__c, Broker_Preferred_Email__c, ...., Status__c from Credit_Review__c WHERE Id =: controllerValue AND Status__c = 'Approved' ORDER BY LastModifiedDate DESC LIMIT 1];

public static testmethod void test2(){
    Id recordType = schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Broker WC Oppty').getRecordTypeId();

    RecordType rt = [SELECT Id, DeveloperName FROM RecordType WHERE Id =:recordType];

    Contact c = new Contact(LastName = 'TestLn');
    insert c;

    Account a = new Account(Name='Testing Account', Legal_Name__c = 'Test');
    insert a;

    Opportunity oppty = new Opportunity(
            name='wendy test',
            StageName = 'Identified',
            CloseDate=System.today(),
            Type = 'NEW',
            RecordType = rt,
            AccountId=a.id);
    insert oppty;
    Credit_Review__c cr = new Credit_Review__c(Status__c = 'In Progress', Opportunity__c = oppty.Id, Broker_Preferred_Email__c = 'otest@test.com');
    insert cr;

    BrokerApprovalEmailTemplate controller = new BrokerApprovalEmailTemplate();
    Test.StartTest();
    controller.controllerValue = oppty.Id;
    controller.setControllerValue(oppty.Id);
    controller.getControllerValue();
    system.debug('test controller value:: ' + controller.getControllerValue());
    Credit_Review__c cc = new Credit_Review__c(Id=cr.Id, Status__c = 'Approved');
    update cc;
    controller.getLoadData();
    System.assertEquals(controller.getControllerValue(), oppty.Id);
}

enter image description here

  • 1
    Where is controller.init() called? You don't update the Credit_Review__c to match the query filters until the end of your test. – David Reed Aug 29 '18 at 19:13
  • @DavidReed sorry the picture got cut off. I upploaded a new one. init() is called in getLoadData(). And it has the same result if I mark the Credit_Review__c object as "Approved" upon insert or update – Olivia Aug 29 '18 at 19:18
  • Well, if you know that controllerValue is correct at that stage (I see you have a debug statement to validate that), I would be looking at other automation on Credit_Review__c that might change its status. I don't see any obvious mistakes in your code. – David Reed Aug 29 '18 at 19:19
  • thank you for verifying, I will continue to dig into the automation side of things. – Olivia Aug 29 '18 at 19:48
1

As @DavidReed states - avoid seeAlldata=true - it can only lead to deployment issues sometime later in life; perhaps in some other sandbox

I highly recommend following the testmethod pattern popularized by Martin Fowler (SpecificationByExample)

@IsTest
private static void givenXXXWhenYYYThenZZZ {
  // Given data setup for testcase
  ... mock data
  // Given Controller constructed
  .. construct controller
  // Given inputs to controller from VF page
  .. set controller properties
  // When action method AAA invoked
  ... invoke action method
  // Then verify ZZZ
  ... verify database updated and/or 
      action method updates public properties B,C, ... and/or
      VF page messages as expected
}

then repeat pattern for next test case

I find this pattern extremely useful to write by starting with the comments, then filling in the details. It also helps clarify where you can write common methods that multiple testmethods can leverage for repeatable setup, execution, or verify fragments.

0

I was able to get the test to pass by using (SeeAllData=True) and query for the appropriate Credit_Review__c record which I typically don't like to use but in this case it was unable to pass because automation and my mock test data were not inserting in the proper order.

  • 2
    Don't do that! Break up your unit test into smaller pieces first and get each one reproducible - you're probably making your life more difficult by testing too many things in one test method. – David Reed Aug 29 '18 at 20:06

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.