Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • MOCKITO COOKBOOK
  • Toc
  • feedback
MOCKITO COOKBOOK

MOCKITO COOKBOOK

By : Grzejszczak
4.3 (4)
close
MOCKITO COOKBOOK

MOCKITO COOKBOOK

4.3 (4)
By: Grzejszczak

Overview of this book

This is a focused guide with lots of practical recipes with presentations of business issues and presentation of the whole test of the system. This book shows the use of Mockito's popular unit testing frameworks such as JUnit, PowerMock, TestNG, and so on. If you are a software developer with no testing experience (especially with Mockito) and you want to start using Mockito in the most efficient way then this book is for you. This book assumes that you have a good knowledge level and understanding of Java-based unit testing frameworks.
Table of Contents (12 chapters)
close
11
Index

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.

bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete