官术网_书友最值得收藏!

  • Mockito Cookbook
  • Marcin Grzejszczak
  • 431字
  • 2021-07-16 11:35:01

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

主站蜘蛛池模板: 保德县| 虞城县| 卓资县| 龙南县| 康平县| 盘锦市| 达日县| 聊城市| 油尖旺区| 商城县| 台湾省| 芜湖市| 钦州市| 嘉兴市| 扎囊县| 吉林省| 岱山县| 郴州市| 天等县| 额敏县| 新巴尔虎右旗| 武乡县| 乾安县| 建宁县| 红河县| 错那县| 仲巴县| 洛阳市| 鹤山市| 稻城县| 利津县| 邳州市| 锡林郭勒盟| 哈巴河县| 大同县| 眉山市| 广东省| 宿迁市| 尼玛县| 赤峰市| 呼图壁县|