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

  • Mockito Cookbook
  • Marcin Grzejszczak
  • 216字
  • 2021-07-16 11:35:00

Adding additional Mockito warnings to your tests (JUnit) (Experimental)

If you would like Mockito to append some additional warning messages to the console, which would help you when your test fails, then this recipe is perfect for you. It's very much related to the previous one so, in order to understand the background, please refer to the introductory part of the previous recipe.

Note

Remember that this feature is experimental and the API, name, or anything related to it may change in time. What's more, the whole feature may get deleted in time!

How to do it...

If you want to have more information presented in your error message, you have to perform the following steps:

  1. Annotate your JUnit test with @RunWith(ConsoleSpammingMockitoJUnitRunner.class).
  2. Define your mocks and perform stubbing inside the test method (unfortunately, you can't use annotations or initialize fields outside test methods).

What happens then is that additional exception messages gets printed on the console after the exception that is thrown:

This stubbing was never used   -> at ....MeanTaxFactorCalculatorTest.should_calculate_mean_tax_factor(MeanTaxFactorCalculatorTest.java:25)

How it works...

When the test is run, ConsoleSpammingMockitoJUnitRunner appends a listener that finds all the stubs through WarningsCollector, including the unused stubs for given mocks. When all of the warnings get collected, the ConsoleMockitoLogger class prints them to the console after the test has failed.

主站蜘蛛池模板: 南昌县| 莲花县| 界首市| 鹿泉市| 汝城县| 丁青县| 云梦县| 屏山县| 琼结县| 常熟市| 兖州市| 华亭县| 高尔夫| 安陆市| 资源县| 禹城市| 桃江县| 古蔺县| 浦北县| 济宁市| 崇明县| 绍兴县| 达拉特旗| 太仆寺旗| 鸡泽县| 平凉市| 沅江市| 鄂伦春自治旗| 浠水县| 鄂伦春自治旗| 柞水县| 建始县| 瓦房店市| 清水县| 襄垣县| 阳山县| 松原市| 新化县| 迭部县| 达拉特旗| 陆丰市|