New Improved apex-mocks-generator

An improved version of apex-mocks-generator will shortly be released to GitHub. This greatly simplifies the generation of mock classes for your interfaces.

Interfaces are now specified via a properties file using key-value type syntax <interface name>=<mock class name>

ICreditCardProcessor=CreditCardProcessor
TaxEngine=TaxEngineImpl

Define interface names as you like – use an ‘I’ prefix at your discretion. Choose whatever mock class name you like.

The new apex-mocks-generator will parse your interfaces file and generate a single ‘mocks’ class for you which will contain mock class definitions for all of the interfaces you specified.

As before you can use the generator 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, e.g. “src/classes”
  2. interfaces – the properties file containing the interfaces you want to mock, e.g. “interfaces.properties”
  3. mocksClassName – the class name for the class which will contain all your mock classes, e.g. “Mocks”
  4. targetPath – where you want the generated mocks class to reside

Generating a Mock Implementation Using Command Line Java

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

java -jar apex-mocks-generator-3.0.1.jar <sourcePath> <interfaces> <mocksClassName> <targetPath>

e.g. java -jar apex-mocks-generator-3.0.1.jar src/classes interfaces.properties Mocks src/classes

Here we are generating a Mocks class into the same folder as our other Apex classes, src/classes.

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">
        <classpath>
            <pathelement location="<path-to-apex-mocks-jar>.jar"/>
        </classpath>
        <arg value="${sourcePath}"/>
        <arg value="${interfaces}"/>
        <arg value="${mocksClassName}"/>
        <arg value="${targetPath}/>
    </java>
</target>

Then from your terminal run:

ant generate.apex.mock -DsourcePath="src/classes" -Dinterfaces="interfaces.properties" -DmocksClassName="Mocks" -DtargetPath="target/classes"

Here we are generating a Mocks class into a target folder which we are using for all our generated source files.

3 thoughts on “New Improved apex-mocks-generator

  1. Pingback: Counters in Apex Mocks verifications – XoNoX Force

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

  3. Pingback: Answering with Apex Mocks – XoNoX Force

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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