Is a test double programmed to throw exception still a "stub"?

93 Views Asked by At

Arguing with my colleague on terminology.

Imagine the following jUnit test:

class PlanetDensityCalculatorTest {
    PlanetService planetServiceTestDouble = Mockito.mock(PlanetService.class);

    PlanetDensityCalculator calculator = new PlanetDensityCalculator(planetServiceTestDouble);

    @Test
    void shouldReturnDensityForCorrectPlanet() throws InvalidPlanetException {
        Mockito.when(planetServiceTestDouble.findPlanet("Some Planet")).thenReturn(
                new SomePlanet("Some Planet", 639, 3389, 226)
        );
        // ...
    }

    @Test
    void shouldReturnNegativeDensityForIncorrectPlanet() throws InvalidPlanetException {
        Mockito.when(planetServiceTestDouble.findPlanet("incorrect planet")).thenThrow(new InvalidPlanetException(""));
        // ...
    }
}

I call planetServiceTestDouble a "stub". It just return some hard-coded result (either return-value or throws hard-coded exception).

My colleague says:

It's mock, because you specify not only the returned value, but it also has some programmed behaviour, like throwing an exception under certain conditions.

I do not agree with him, because:

  1. even though planetServiceTestDouble can behave differently on different input, it's still a stub, because it returns hard-coded values with no logic (besides the input value).
  2. It can't be called a mock, because we do not verify interaction with this test double (which is IMO an essential thing in test double to be called a mock)

PS: looks like there can be a mismatch in test doubles terminology, but I am looking for answer in the context of Martin Fowler/xUnit test patterns definitions.

0

There are 0 best solutions below