Using apex-mocks-generator to create mock class definitions

Last time we saw how to define a component interface such that we could use a mock version of a component in our unit tests.

In order to either verify or stub any given component, we need a mock version of the component which knows how to communicate with the ApexMocks framework.

It would be pretty laborious (and boring!) as a developer if we had to hand-write the mock implementations of our components all the time, so the ApexMocks framework provides a command line tool which will create and add mock class definitions for you.

Having created the component interface definition, you can add a mock implementation of your interface to your unit test class using the apex-mocks-generator. You can either do this via Java or ant. The basic format is the same, apex-mocks-generator needs to be told about:

  1. sourcePath – where your apex source files reside
  2. testClassName – the class to which you want to add the mock
  3. mockClassName – the implementation class name of the component you want to mock
  4. interfaceName – the interface within the implementation class you want to mock

Generating a Mock Implementation Using Command Line Java

Simply download the apex-mocks-generator JAR from GitHub, then run the following from your terminal:

java -jar apex-mocks-generator-2.0.5.jar <sourcePath> <testClassName> <mockClassName> <interfaceName>

e.g. java -jar apex-mocks-generator-2.0.5.jar src/classes BillingServiceUnitTest CreditCardProcessor ICreditCardProcessor

Generating a Mock Implementation Using ant

To generate a mock implementation using ant, add the following target to your build.xml file, replacing the <path-to-apex-mocks-jar>:

<target name="generate.apex.mock">
    <java classname="com.financialforce.apexmocks.ApexMockGenerator">
            <pathelement location="<path-to-apex-mocks-jar>.jar"/>
        <arg value="${sourcePath}"/>
        <arg value="${testClassName}"/>
        <arg value="${mockClassName}"/>
        <arg value="${interfaceName}"/>

Then from your terminal run:

ant generate.apex.mock -DsourcePath="src/classes" -DtestClassName="BillingServiceUnitTest" -DmockClassName="CreditCardProcessor" -DinterfaceName="ICreditCardProcessor"

Using a top-level Mocks Class

If you want to keep all your mock class definitions in a single top-level class file (rather than as inner classes in your unit test), just create something like a Mocks class in your Salesforce project and pass Mocks as the testClassName when  you generate a mock. This could aid reuse if more than one test wants to use any given mock class.

In the next post we’ll see how to start writing unit tests using ApexMocks.


4 thoughts on “Using apex-mocks-generator to create mock class definitions

    • @AlanMorey possibly at some point. It’s not particularly a secret, but I’m investigating a couple of more options to make it even better.

      If you are having trouble using the generator please do send me the error you get either via here or the GitHub repo – just log an issue and I will respond ASAP.

  1. Pingback: Verify call’s order in Apex unit tests – XoNoX Force

  2. Pingback: Answering with Apex Mocks – XoNoX Force

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s