How to use Mockito with JUnit5

There are different ways to use Mockito – I’ll go through them one by one. Manually Creating mocks manually with Mockito::mock works regardless of the JUnit version (or test framework for that matter). Annotation Based Using the @Mock-annotation and the corresponding call to MockitoAnnotations::initMocks to create mocks works regardless of the JUnit version (or test … Read more

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0

Run the Gradle build with a command line argument –warning-mode=all to see what exactly the deprecated features are. It will give you a detailed description of found issues with links to the Gradle docs for instructions how to fix your build. Adding –stacktrace to that, you will also be able to pinpoint where the warning … Read more

Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll

The code marked @Before is executed before each test, while @BeforeClass runs once before the entire test fixture. If your test class has ten tests, @Before code will be executed ten times, but @BeforeClass will be executed only once. In general, you use @BeforeClass when multiple tests need to share the same computationally expensive setup … Read more

JUnit 5: How to assert an exception is thrown?

You can use assertThrows(), which allows you to test multiple exceptions within the same test. With support for lambdas in Java 8, this is the canonical way to test for exceptions in JUnit. Per the JUnit docs: import static org.junit.jupiter.api.Assertions.assertThrows; @Test void exceptionTesting() { MyException thrown = assertThrows( MyException.class, () -> myObject.doThing(), “Expected doThing() to … Read more