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

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.

主站蜘蛛池模板: 穆棱市| 肥城市| 襄城县| 安国市| 昆山市| 射阳县| 寻乌县| 灯塔市| 正蓝旗| 兴业县| 沈丘县| 阿尔山市| 罗城| 吴桥县| 沙湾县| 麟游县| 汕头市| 大安市| 哈巴河县| 平果县| 呼和浩特市| 犍为县| 宁津县| 尉犁县| 阿克| 龙陵县| 沧州市| 鄂州市| 兴义市| 固镇县| 小金县| 库车县| 绥芬河市| 丹棱县| 二手房| 灌南县| 阿坝县| 华池县| 海兴县| 渝北区| 林周县|