I'm a Java software developer from Buenos Aires, Argentina. I also enjoy working with pure object-oriented and functional languages like Smalltalk and Haskell. Lucas has posted 1 posts at DZone. You can read more from them at their website. View Full User Profile

Using JUnit Theories with Spring and Mockito

  • submit to reddit

What is a Theory?

Functionally, a theory is an alternative to JUnit's parameterized tests. Semantically, a theory encapsulates the tester's understanding of an object's universal behavior. That is, whatever it is that a theory asserts, it is expected to be true for all data. Theories should be especially useful for finding bugs in edge cases.

Contrast this with a typical unit test, which asserts that a specific data point will have a specific outcome, and only asserts that. (For this reason, typical unit tests are sometimes called example-based tests to contrast them with theories.)

This is very nice in theory, but...

A @Theory needs a special JUnit runner (Theories.class). So if you want to use Spring and/or Mockito together with theories, you have a problem. All of these features need a different runner and you can only use one on each test class.

The solution

For Mockito is easy. Instead of using the @Mock annotiation, you can use the static createMock method. One problem solved.

For Spring is a little bit trickier. First of all, you have to use @ContextConfiguration to declare the XML with the bean definitions that you need. But the trickiest part is that you have to tell Spring how to do the autowiring without using its own runner. This can be accomplish adding this line to the @Before method:

new TestContextManager(getClass()).prepareTestInstance(this);

Basic Usage Example

package org.mackenzine.theories;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;

import java.util.Date;

import org.junit.Before;
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestContextManager;

public class QuoteTheoriesTest {

   private static String deleteMessage = "deleteMessage";
   private static String updateMessage = "updateMessage";

   private QuoteFactory factory;

   private final Event event = Mockito.mock(Event.class);
   private final Contract contract = Mockito.mock(Contract.class);
   private final Commodity commodity = Mockito.mock(Commodity.class);

   private Parser parser;

   public void setUp() throws Exception {
     factory = new QuoteFactory();
     new TestContextManager(getClass()).prepareTestInstance(this);

   public static String[] getEventTypes() {
     return new String[] { updateMessage, deleteMessage };

   public void shouldCreateQuote(final String message) throws Exception {
     Date now = new Date();


     Quote quote = factory.createQuote(event);


     assertEquals("code", quote.getCommodityCode());
     assertEquals(now, quote.getTradeDate());

Definition of Theories:

Original Idea for Parameterized Tests:

Thread on SpringSource:

Open Issue in SpringSource for Parameterized Tests (not for Theories):

Published at DZone with permission of its author, Lucas Godoy.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Rodrigo Fernandez replied on Fri, 2012/12/14 - 3:01pm

Actually, you can use @Mock without using the Mockito runner, by using:

In your @Before method.That way you can have all three at the same time.Great info, btw!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.