1

How can I test this Apex Controller Method? I just want to test that the values are in fact returned.

public String getPrimaryApplication() {
    return PrimeApp[0].PrimaryApplication__c + ' -- ' + PrimeApp[0].Customer_Profile_Text__c ;
} 

EDIT --- Added the below:

Full Controller:

public CustomerMartketProfileController(ApexPages.StandardController stdController)
{
    this.opp = (Opportunity)stdController.getRecord();  
 if (opp.Primary_Application__c != null) 
        {
            PrimeApp = [Select PrimaryApplication__c, Customer_Profile_Text__c FROM Market_Vertical_Config__c 
                                            WHERE RecordTypeID = :rectype 
                                            AND PrimaryApplication__c = :opp.Primary_Application__c];
        } 
}
public String getPrimaryApplication() {
    return PrimeApp[0].PrimaryApplication__c + ' -- ' + PrimeApp[0].Customer_Profile_Text__c ;
} 

Current Test Class:

public static testmethod void CustomerMartketProfile_Test() {  
    Account Acct = new Account(Name='AccountTest ConMrktProf', Institution_Type__c = 'Ac', Site = '123');
    insert Acct;

    Opportunity Opp1 = new Opportunity(AccountID = Acct.id,
                                      Name = 'TestOpp1',
                                      StageName = 'Recognition of Needs',
                                      CloseDate = System.today(),
                                      Market_Segment__c = 'Agri',
                                      Routine_Testing__c = 'Yes',
                                      Primary_Application__c = 'Amino');
    insert Opp1;

    ApexPages.currentPage().getParameters().put('opp',Opp1.id);
    ApexPages.StandardController Opport = new ApexPages.StandardController(Opp1);
    CustomerMartketProfileController controller1  = new CustomerMartketProfileController(Opport);
2
  • 2
    Could you elaborate a bit more on your ask? Also, perhaps provide more detail about your code? – Sebastian Kessel Jun 4 '18 at 17:44
  • @SebastianKessel Updated – dannycheeko Jun 4 '18 at 19:22
3

Your unit test should ensure that the instance variable PrimeApp is populated with at least one sObject value, which has the fields PrimaryApplication__c and Customer_Profile_Text__c populated.

This may entail creating and inserting one or more sObjects for a controller query to find, or it may be as simple as creating the sObject in memory and assigning it to the controller's instance variable - we can't tell that from the snippet of code you included. If you're creating and inserting one or more sObjects, you may wish to do that in an @testSetup method, so you can reuse the test data (after querying it) in each test case.

Your unit test case should then call getPrimaryApplication() and make an assertion, likely via System.assertEqual(), to show that the result is what you expect it to be.

You should also be writing tests for exceptional cases to ensure your code handles them gracefully. Here, you'd want at least two or three:

  • What happens when PrimeApp is null? Create the situation in your unit test (in a separate test method), and make assertions about what the result should be.
  • What happens when PrimeApp is not null, but contains no records? Again, create the situation and validate the behavior.
  • What happens when PrimeApp is not null or empty, but the first record has null values for the two fields being used?

So in the end, you'd have one test class with at least three unit test methods, depending on what other functionality you want to work on in this controller. Those tests would demonstrate and prove that your code works correctly in any situation presented to it. As a side effect, you'd achieve 100% test coverage and get the ability to deploy.

The Salesforce Developer blog has posted an excellent series on testing recently, that I'd recommend you read through. Trailhead also has a quality unit on unit testing.

1

Provided by salesforce support.

    controller1.getPrimaryApplication();
1
  • 1
    Calling the method does not constitute a test. You need to make an assertion to validate that the behavior is what is expected. – David Reed Jun 5 '18 at 14:59

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.