ApexMocks Improvements – exception stubbing, inner interfaces and mock base classes

A new version of ApexMocks and the associated mock class generator will be released to GitHub later today. This will bring various improvements to your life as an Apex mockist!

Support for Exception Stubbing

Sometimes when writing a unit test it is helpful to be able to stub an exception to be thrown. You can now do this using the new thenThrow() and doThrowWhen() methods:

mocks.when(mockList.get(0)).thenThrow(new MyException('Stubbed exception.'));

((fflib_MyList.IList) mocks.doThrowWhen(new MyException('Stubbed exception.'), mockList)).clear();

The doThrowWhen syntax is necessary when you want to stub a void method to thrown an exception. Please see the fflib_ApexMocksTest class for examples of unit tests using these features.

Inner Interfaces

The apex-mocks-generator now supports inner interfaces, for example:

public class fflib_MyList implements IList
{
    public interface IList
}

Simply specify the inner interface in your interfaces properties file:

fflib_MyList.IList=Mockfflib_MyList

Base class support for Mocks

If you would like a generated mock class to extend a base class, you can use the following notation in your interfaces properties file:

IOpportunities=Opportunities:BaseOpportunities

Here we are saying that the IOpportunities interface should create a mock class named Opportunities which will extend the BaseOpportunities class:

public class Opportunities extends BaseOpportunities

Improved Code Generation

The apex-mocks-generator has been enhanced to create less generated code resulting in slimmer, more efficient mock classes. Please use version 3.1.0 to take advantage of this and the above features.

Advertisement

5 thoughts on “ApexMocks Improvements – exception stubbing, inner interfaces and mock base classes

  1. Great tool; is there a ref/ md/ guide doc available for the accumulated Mock tooling? On the blog one has to start at the bottom, read upwards. and accumulate the changes.
    thanks!

    • Hi Bernie,

      that’s a really good point, there isn’t a quick ref available, but I will try and find some time to put one together.

      Thanks for the suggestion.

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

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

  4. 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 )

Facebook photo

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

Connecting to %s