Creating mocks with annotations

In the previous recipe, we saw how to create a mock by means of the Mockito.mock static method. It's much better, however, to use Mockito's annotations to make your tests look even nicer. Before going into the details of how to do it, let's take a closer look at the system under test (it's the same as in the previous recipe, but in order for you not to jump around pages, let's take a look at it here).

Getting ready

In this recipe, our system under test is a class that calculates a mean value of tax factors retrieved through a web service, as shown in the following code:

public class MeanTaxFactorCalculator {

    private final TaxService taxService;

    public MeanTaxFactorCalculator(TaxService taxService) {
        this.taxService = taxService;
    }

    public double calculateMeanTaxFactorFor(Person person) {
        double currentTaxFactor = taxService.getCurrentTaxFactorFor(person);
        double anotherTaxFactor = taxService.getCurrentTaxFactorFor(person);
        return (currentTaxFactor + anotherTaxFactor) / 2;
    }

}

Let's now write a test for the system that will check whether it can properly calculate the mean value of the tax factor. We have to create a stub of TaxService and stub its behavior (we don't want it to send any real requests).

How to do it...

Since Mockito integrates very nicely with JUnit (refer to Chapter 1, Getting Started with Mockito, for more details regarding both JUnit and TestNG configuration), let's remove the unnecessary code and make the test more readable. To do that, you have to perform the following steps:

  1. Annotate your test with @RunWith(MockitoJUnitRunner.class).
  2. Define the collaborators that you would like to mock.
  3. Annotate those dependencies with @Mock annotation.

Of course, this JUnit approach will work only if you haven't already annotated your test class with another @RunWith annotation.

Now, let's take a look at the test written for JUnit (remember that I'm using the BDDMockito.given(...) and AssertJ's BDDAssertions.then(...) static methods. Refer to Chapter 7, Verifying Behavior with Object Matchers, to learn how to work with AssertJ or how to do the same with Hamcrest's assertThat(...)). Have a look at the following code:

@RunWith(MockitoJUnitRunner.class)
public class MeanTaxFactorCalculatorTest {

    static final double TAX_FACTOR = 10;

    @Mock TaxService taxService;

    @InjectMocks MeanTaxFactorCalculator systemUnderTest;

    @Test
    public void should_calculate_mean_tax_factor() {
        // given
        given(taxService.getCurrentTaxFactorFor(any(Person.class))).willReturn(TAX_FACTOR);

        // when
        double meanTaxFactor = systemUnderTest.calculateMeanTaxFactorFor(new Person());

        // then
        then(meanTaxFactor).isEqualTo(TAX_FACTOR);
    }

}

How it works...

A more precise description of how MockitoJUnitRunner works and how it creates mocks is provided in Chapter 1, Getting Started with Mockito. So please refer to it for more details.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.179.225