2

I'm trying to get code coverage on my class to no avail. Any assistance would be greatly appreciated. I'm sure there is a better way to write my class but just trying to get the end user his request at this point. I've spent a couple of days fiddling with this and tried Googling many resources.

Here is my Class

global with sharing class BusinessIntelligenceFeedback {

  public String BusIntId{get;set;}

  public Business_Intelligence_Survey__c bif {
    get {
      if (bif == null)
        bif = new Business_Intelligence_Survey__c(
        Business_Intelligence__c = BusIntId,
        Survey_Results__c = 'Yes'
        );
      return bif;
    }
    set;
  }

  // save button is clicked
 public PageReference save() {

    try {
      insert bif; // inserts the new record into the database
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new bif.'));
      return null;
    }
        // if successfully inserted new BISurvey, then displays the thank you page.
    return Page.BusinessIntelligence_Create_Thankyou;
  }

}

Here is my Test Class

@isTest(SeeAllData=true)

private class TestBusinessIntelligenceFeedback {
    static testMethod void testMe(){

  Business_Intelligence_Survey__c bif = new Business_Intelligence_Survey__c();
        bif.Business_Intelligence__c = 'a24U00000005kkL';
        bif.Survey_Results__c = 'Yes';
        insert bif;

    Business_Intelligence_Survey__c testbif = [select Id, Survey_Results__c, name from Business_Intelligence_Survey__c where id =:bif.Id]; 

     System.debug('Status on ticket after trigger fired: ' + bif.Survey_Results__c);

       // Test that the trigger correctly updated the survey

       System.assertEquals('Yes', bif.Survey_Results__c);

    }

}
  • 1
    You biffed the naming a bit, perhaps. On a serious note, never use SeeAllData=true unless you absolutely have to, and never use a hard-coded Id for any reason. – Adrian Larson Aug 2 '16 at 18:06
  • Hi @AdrianLarson, I backed that out and still have 0% coverage and the Test still passed. Appreciate you taking a look dude. – Louis HM Aug 2 '16 at 18:07
  • 2
    @sfdcfox - Thanks for editing this, it's my first post ever and I couldn't get it to properly format. – Louis HM Aug 2 '16 at 18:08
  • 2
    @LouisHM It's not a problem. Just use the "code format" button when posting, so that your code appears in the grey, preformatted box, or you can use Ctrl-K (or Cmd-K on Mac, I believe). – sfdcfox Aug 2 '16 at 18:11
  • 1
    Yeah ctrl/command + k is magic. – Adrian Larson Aug 2 '16 at 18:17
3

Your unit test appears to be the standard form for testing a trigger. You're not trying to test a trigger, so this type of pattern is inappropriate. Instead, you want to use a "test a class" form. Here's the minimum changes you'd want to make:

@isTest(SeeAllData=true)

private class TestBusinessIntelligenceFeedback {
    static testMethod void testMe(){
        BusinessIntelligenceFeedback controller = new BusinessIntelligenceFeedback();
        controller.BusIntId = 'a24U00000005kkL';
        controller.save();
        System.assertNotEquals(null, controller.bif.Id);
    }
}

As others have commented, you should be creating all records you need within the unit test, instead of hard-coding an Id in your code. This can cause unexpected changes in behavior when migrating between environments or if someone modifies/deletes the record referenced by the hard-coded Id.

That might look like:

@isTest

private class TestBusinessIntelligenceFeedback {
    static testMethod void testMe(){
        Business_Intelligence__c bi = new Business_Intelligence__c();
        // populate required fields here, then insert
        insert bi;
        BusinessIntelligenceFeedback controller = new BusinessIntelligenceFeedback();
        controller.BusIntId = bi.Id;
        controller.save();
        System.assertNotEquals(null, controller.bif.Id);
    }
}
| improve this answer | |
  • AHHHHH, @sfdcfox!!! You're a ninja!!!! That worked. I'm going to review this line by line and see where I went wrong. Many thanks, many many many thanks!!!! – Louis HM Aug 2 '16 at 18:13
  • @LouisHM Don't forget that you can accept any of these answers as the most helpful, likely this one. – Adrian Larson Aug 2 '16 at 18:31
  • @sfdcfox, yes, you're absolutely right. One of the other dev guys has advised me about this in the past. Thanks again for your time and assistance! – Louis HM Aug 2 '16 at 19:00
4

Just a note on how to cover the catch block, my standard approach is to use a lookup Id for the incorrect sObjectType. For example, if you set busIntId to a User Id, then you should get a DmlException.

static testMethod void testSave()
{
    MyController controller = new MyController();
    controller.busIntId = UserInfo.getUserId();

    Test.startTest();
        controller.save();
    Test.stopTest();

    system.assert(ApexPages.hasMessages(), '<assertion message here>');
}
| improve this answer | |
  • 2
    Thanks for reminding me about this. I've got a unit test to go fix... – sfdcfox Aug 2 '16 at 18:18
0

I did some modification to your code, check if this helps you to with your coverage. I pefer not to set see all data true.

   @isTest(SeeAllData=true)
    private class TestBusinessIntelligenceFeedback {
        static testMethod void testMe(){

            BusinessIntelligenceFeedback bIfController = new BusinessIntelligenceFeedback();

            Test.startTest();
            bIfController.BusIntId = 'a24U00000005kkL';
            bIfController.bif = BusinessIntelligenceFeedback.bif;
            bIfController.save();
            Test.stopTest();

            Business_Intelligence_Survey__c testbif = [select Id, Survey_Results__c, name from Business_Intelligence_Survey__c where id =:bif.Id]; 

            System.debug('Status on ticket after trigger fired: ' + bIfController.Survey_Results__c);

            // Test that the trigger correctly updated the survey

            System.assertEquals('Yes', bif.Survey_Results__c);

        }

    }
| improve this answer | |
  • 2
    Please don't just do a code dump. If code is worth the time to post, it should be worth the time to explain as well. – Adrian Larson Aug 2 '16 at 18:14
  • If you change SeeAllData to false, instead of hardcoding the BusIntId, you have to create the record and assign the id of the record created. – D. Cruz Aug 2 '16 at 18:15
  • 1
    Thanks @D. Cruz! I used SFDCFox's code and I got 78% code coverage. I'm all set here but I will def take a stab at yours too. You guys are awesome. I didn't think I would get an answer so fast from anyone. I appreciate you guys a ton. – Louis HM Aug 2 '16 at 18:15
  • You where not getting coverage because you where manually creating the Business_Intelligence_Survey__c instead of using the controller methods and variables. – D. Cruz Aug 2 '16 at 18:19
  • Will do @AdrianLarson. I will explain that moving forward from now on. What I was trying to send a user to a VFpage after they click on an email with a thumbs up or thumbs down. This is basically my Thumbs up Class and it will set the Saleforce record to 'Yes'. Basically my job is to record wether or not the end user is happy with our method of business workflow. So this class creates a Yes record and associates the original parent record. I then send them to another landing VFpage that says Thank You. – Louis HM Aug 2 '16 at 18:19

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.