Category Archives: Testing

Mockito: Effective Partial Mocking

Sometimes, we may only want to partially mock an object to unit test our code. There are several ways to skin this cat, but I’ll show two straightforward approaches that allow you to obtain the cat fur.

PROBLEM

Let’s assume we want to test this service class:-

public class CalculationService {

    public int getFirstValue() {
        return 1;
    }

    public int getSecondValue() {
        return 2;
    }

    public int getComputedValue() {
        return getFirstValue() + getSecondValue();
    }
}

Basically, getComputedValue() sums up getFirstValue() and getSecondValue() and returns the value.

PARTIAL MOCKING USING mock(...)

Here’s an example of using mock object:-

CalculationService calculationService = mock(CalculationService.class);
when(calculationService.getFirstValue()).thenReturn(2);
when(calculationService.getSecondValue()).thenReturn(5);

int val = calculationService.getComputedValue();

assertThat(val, is(???)); 

What do you think the value for ??? should be?

If you think the value is 7, you are not skinning the cat correctly.

The correct value is 0. In the above example, when we execute calculationService.getComputedValue(), we are actually executing a stub API from the mock object that doesn’t do anything but to return a default value, which is 0.

The good news is Mockito provides thenCallRealMethod() that allows us to execute the actual method instead. So, to fix this, we do something like this:-

CalculationService calculationService = mock(CalculationService.class);
when(calculationService.getFirstValue()).thenReturn(2);
when(calculationService.getSecondValue()).thenReturn(5);

when(calculationService.getComputedValue()).thenCallRealMethod();

int val = calculationService.getComputedValue();

assertThat(val, is(7));

PARTIAL MOCKING USING spy(...)

Another way to do partial mocking is to use a spy. Here’s an example:-

CalculationService calculationService = spy(new CalculationService());
when(calculationService.getFirstValue()).thenReturn(2);
when(calculationService.getSecondValue()).thenReturn(5);

int val = calculationService.getComputedValue();

assertThat(val, is(7));

WHICH IS A BETTER WAY?

It is difficult to say which approach is better because it all depends on the object we are testing. My rule of thumb is if we only want to modify the behavior of small chunk of API and then rely mostly on actual method calls, use spy(...). Otherwise, use mock(...).

In another word, when we start skinning a cat and it starts to bleed like crazy and scratch your face, sometimes, it’s wise to put down the knife and consider taking a different approach to solve the problem… metaphorically speaking.

How to Unit Test Spring MVC Controller

SCENARIO

Let’s assume we have the following controller that needs to be tested:-

@Controller
@RequestMapping(value = "/person")
public class PersonController {

    private PersonService personService;

    @Autowired
    public PersonController(PersonService personService) {
        this.personService = personService;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String getPerson(@PathVariable Long id, Model model) {
        model.addAttribute("personData", personService.getPerson(id));
        return "personPage";
    }
}

SOLUTION 1: “Works but It Won’t Get You the Promotion”

This working solution relies on:-

  • JUnit – General unit test framework.
  • Mockito – To mock PersonService.

public class PersonControllerTest {

    @Test
    public void testGetPerson() {

        PersonService personService = mock(PersonService.class);

        when(personService.getPerson(1L)).thenReturn(new Person(1L, "Chuck"));

        PersonController controller = new PersonController(personService);

        Model model = new ExtendedModelMap();

        String view = controller.getPerson(1L, model);

        assertEquals("View name", "personPage", view);

        Person actualPerson = (Person) model.asMap().get("personData");

        assertEquals("matching ID", Long.valueOf(1), actualPerson.getId());
        assertEquals("matching Name", "Chuck", actualPerson.getName());
    }
}

While this solution works, but it has a few problems. This test case strictly tests the actual controller API, but it completely disregards the URI and request method (GET, POST, PUT, DELETE, etc). For instance:-

  • What if the URI has a typo ( /persn/1 ) or it is not properly constructed ( /person/donkey-kong )?
  • What if the request method should be a POST but we accidentally used a GET?

SOLUTION 2: “A Mind Blowing Solution that Still Won’t Get You the Promotion but You Feel So Invincible That You Feel Compelled to Flip a Table Over”

This better solution relies on:-

  • JUnit – General unit test framework.
  • Mockito – To mock PersonService.
  • Spring MVC Test Framework – To properly test the controller.
  • Hamcrest – To clean way to assert the actual result is correct.

@RunWith(SpringJUnit4ClassRunner.class)
// This XML configuration basically enable component scanning.
// You could have used @Configuration and @ComponentScan to do the same thing.
@ContextConfiguration({"classpath*:spring-test.xml"})
public class PersonControllerTest {

    @Mock
    private PersonService personService;

    @InjectMocks
    private PersonController personController;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders.standaloneSetup(personController).build();
    }

    @Test
    public void testGetPerson() throws Exception {
        when(personService.getPerson(1L)).thenReturn(new Person(1L, "Chuck"));

        mockMvc.perform(get("/person/{id}", 1L))
                .andExpect(status().isOk())
                .andExpect(view().name("personPage"))
                .andExpect(model().attribute("personData",
                                             allOf(hasProperty("id", is(1L)),
                                                   hasProperty("name", is("Chuck")))));
    }
}

Basically object that is annotated with @Mock will get injected into object that is annotated with InjectMocks. Then, we rely on Spring MVC Test Framework’s MockMvc to test our controller in a very clean and detailed fashion.

Okay, you may flip a table over now…

By the way…

You need at least Spring 3.2 to use MockMvc.

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-test</artifactId>
	<version>3.2.4.RELEASE</version>
	<scope>test</scope>
</dependency>

If you are using an older Mockito version, you will get this error:-

org.mockito.exceptions.base.MockitoException: Field 'personController' annotated with @InjectMocks is null.
Please make sure the instance is created *before* MockitoAnnotations.initMocks();
Example of correct usage:
   class SomeTest {
      @InjectMocks private Foo foo = new Foo();
      
      @Before public void setUp() {
         MockitoAnnotations.initMock(this);

Upgrading Mockito to the latest version will fix this error:-

<dependency>
	<groupId>org.mockito</groupId>
	<artifactId>mockito-core</artifactId>
	<version>1.9.5</version>
	<scope>test</scope>
</dependency>