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.

Advertisements