1

Need some help. I know how to create test class for a apex class that expects inputs. How do you create a test class for a controller that is just performing a query and has no inputs?

Here is my controller:

public class CongaReportController {

    String APP_NAMESPACE_PREFIX = 'APXTConga4';

    public List<user> getCongaUsers() {

        List<User> results = Database.query(
            'SELECT Name FROM User ' + 
            'WHERE Id IN (SELECT UserId FROM UserPackageLicense WHERE (PackageLicense.NamespacePrefix = :APP_NAMESPACE_PREFIX)) ' +
            'ORDER BY Name ASC'
        );
        return results;

    }
}

The VF Page

<apex:page Controller="CongaReportController" showHeader="true" sidebar="true">

    <apex:pageBlock title="Conga Licensed Users">
        <apex:pageBlockTable value="{!CongaUsers}" var="LIC">
            <apex:column value="{!LIC.Name}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>

</apex:page>

As stated in title, this page is accessed via a Related list View button.

So how do I test this?

0

The only thing you can test (based on the code) is the number of users returned by the method getCongaUsers() (negative/positive). In general your test class could look like this (simplified example):

CongaReportController con = new CongaReportController();
System.assertEquals(XX, con.getCongaUsers());

BUT you might want to prepare a test data first. Based on the code provided it could be the following objects:

  • User
  • UserPackageLicense

So the complete test class looks then like this:

Create User
Insert User
Create UserPackageLicense and associate it with User-Id
Insert UserPackageLicense

CongaReportController con = new CongaReportController();
System.assertEquals(XX, con.getCongaUsers());
  • If I follow your second suggestion am I supposed to put in 1 for the XX value of your assert statement? I will have more licensed users than that so I am not sure I am interpreting your code correctly. – Matt M Dec 15 '15 at 17:50
  • 1
    mocked UserPackageLicense is going to have to be associated to PackageLicense with namespacePrefix = 'APXTConga4'. PackageLicense does not need to be mocked in the testmethod as they are visible even with SeeAllData=false. Then, XX = 1. Best practice would be to make APP_NAMESPACE_PREFIX @TestVisible so testmethod uses same constant to query for the Conga package as is used by prod code – cropredy Dec 15 '15 at 18:16
  • @MattM It is a best practice to NOT access a real data from a test class, that's why I've "created" a new user record. Since it is the only record available (assuming you are using SeeAllData=false) - you should set 1 to the assertion function (the method SHOULD return only one record, otherwise it will fail). – Sergej Utko Dec 15 '15 at 20:33

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.