This page details the use of the SevenMock Class Extension.

Although SevenMock can create a mock for any specified class or interface, the resulting mock object must normally be cast to an interface before it can be used.

If a class is supplied when calling getMock(Class aClass), then the resulting mock object will support all the interfaces implemented by that class - but the object must still be cast to one of the interfaces implemented by that class before it is used. If you try to cast it to the class itself, java will throw a ClassCastException.

Normally this is not a problem because if a unit of code is worth mocking, it is worth defining an interface to reduce dependencies from other code. If your style of coding or testing makes the declaration of interfaces inconvenient, however, it is possible to mock classes directly without the need to declare interfaces. This can be achieved by using the SevenMock class extension library: sevenmock-classext.jar. This in turn uses the cglib library to allow the mock objects to be cast to a class.


mockControl = new MockController();
AccountManager acntMgrMock = mockControl.getMock(AccountManagerImpl.class);
mockControl = new MockController();
AccountManagerImpl acntMgrMock = mockControl.getMock(AccountManagerImpl.class);

In order to make this work, you will need the following cglib and related library jars in your classpath:

  • cglib-2.2_beta1.jar
  • asm.jar
  • asm-util.jar
  • asm-commons.jar

  • SevenMock Home Logo