ApexMocks: Setup

In my last two posts, I covered why using ApexMocks is beneficial and how ApexMocks works. In this post, I will cover how to deploy ApexMocks into one of your orgs. In order to deploy ApexMocks, we will utilize the Force.com Migration Tool. While it isn’t necessary to know this tool in depth, you should understand the general concept of the tool and you should already have it set up. Here are detailed instructions on installing the Force.com Migration Tool if you need some help.

Pull Down Code from GitHub

ApexMocks is open source and provides all of the code you will need to use it on GitHub. There are several ways to get the code off of GitHub, you can:

Once you have the code on your machine, we can go ahead and deploy it to your developer or sandbox org.

Note: Do not deploy directly to a production instance. This should be used in development before attempting to go to production.

Deploying ApexMocks

Once we have the Force.com Migration Tool ready to use, we can go ahead and deploy ApexMocks to our org. In order to get the code to our org, we will use the sf:deploy Ant target. In order to do the deploy, we will need 2 files, build.properties and build.xml. Let’s take a look:

build.properties

# Specify the login credentials for the desired Salesforce organization
sf.username = <insert username here>
sf.password = <insert password and security token>

# Use 'https://login.salesforce.com' for production or developer edition
# Use 'https://test.salesforce.com for sandbox.
sf.serverurl = https://login.salesforce.com

sf.maxPoll = 20

This is the file that will store all of the properties we will use as part of the deploy. Assuming you are familiar with Ant, the general idea here is that this file will hold the dynamic properties that will be different for each build/user of the deploy file. Make sure you replace the sf.username, sf.password, and sf.serverurl with the values for your org. I’ll explain how each of these are used in the build.xml file.

build.xml

<project name="ApexMocks" default="test" basedir="." xmlns:sf="antlib:com.salesforce">
    <property file="build.properties"/>
    <property environment="env"/>

    <target name="deployApexMocks">
        <sf:deploy username="${sf.username}" password="${sf.password}" serverurl="${sf.serverurl}" maxPoll="${sf.maxPoll}" deployRoot="src"/>
    </target>
</project>

The build.xml file is what holds the Ant target that will be used to actually deploy the code for ApexMocks. For a detailed description on each of the variables on the sf:deploy task, you can read the documentation.

Add Files to ApexMocks Directory

Note: This should be resolved in a future version so that these files are included in the repo. At the time this article was written, these files are not in the repo.

Now that we have the build.properties and build.xml, we just need to add these files to the base ApexMocks directory where you pulled down the code. Your project should look something like:

Run the Deploy

At this point, all you need to do is run the Ant target we have set up. Open up your Terminal (or Cygwin), and navigate to the home directory of your project. In my scenario, that is in the Documents/workspace/apexmocks/fflib-apex-mocks directory. Now run your ant task, ant deployApexMocks. The result should look like:

Jesses-MacBook-Air:fflib-apex-mocks jaltman$ ant deployApexMocks
Buildfile: /Users/jaltman/Documents/workspaces/apexmocks/fflib-apex-mocks/build.xml

deployApexMocks:
[sf:deploy] Request for a deploy submitted successfully.
[sf:deploy] Request ID for the current deploy task: 09Si0000003EevFEAS
[sf:deploy] Waiting for server to finish processing the request...
[sf:deploy] Request Status: InProgress
[sf:deploy] Request Status: Succeeded
[sf:deploy] *********** DEPLOYMENT SUCCEEDED ***********
[sf:deploy] Finished request 09Si0000003EevFEAS successfully.

BUILD SUCCESSFUL
Total time: 14 seconds

Conclusion

ApexMocks should now be available in your org. Feel free to start mocking! Make sure to check some of the reference material myself and Paul Hardaker have created to help you along the way. Enjoy!

Note: All the code in this article is available as a Gist on GitHub

No comments yet.

Leave a Comment