SpringExtension for JUnit 5: Why Do We Need It?
SpringExtension integrates the Spring TestContext Framework into JUnit 5 Jupiter extension model, sets up the ApplicationContext, and manages dependencies.
SpringExtension integrates the Spring TestContext Framework into JUnit 5 Jupiter extension model, sets up the ApplicationContext, and manages dependencies.
Learn to run JUnit tests using Maven Surefire Plugin. We will learn to run a single test, run only selected tests or run all the tests in the project.
Unit test a Spring Boot service with JUnit and Mockito. Mock the repository with @Mock, build the service with @InjectMocks, stub with when().thenReturn() and check calls with verify(). Also covers @MockitoBean in @SpringBootTest, thenThrow, ArgumentCaptor, BDDMockito, strict stubs and the Mockito Java agent on Java 21+.
JUnit 5 vs JUnit 4 compared with the same test in both versions, an annotation and rule mapping table, the Vintage engine and an OpenRewrite migration.
JUnit 5 assertThrows() examples that check the exception type, message and cause, plus assertThrowsExactly(), assertDoesNotThrow() and JUnit 4 migration.
JUnit assertions explained with examples for JUnit 5 and 6, from assertEquals and assertAll to assertLinesMatch, assertInstanceOf, timeouts and fail().
Learn some recommended ways to configure Log4j2 – specific to junit testcases and different from used in production.
Test Spring Security authentication with JUnit 6 and Spring Boot 4.1.1 using @WithMockUser, @WithUserDetails, httpBasic(), csrf() and 401 and 403 checks.
JUnit test listener examples for JUnit 6 and 4, a TestExecutionListener registered via ServiceLoader, a TestWatcher extension and a JUnit 4 RunListener.
JUnitCore runs JUnit 4 tests from code. Learn its JUnit 6 replacement, the Launcher API, with selectors, tag filters, summaries, fail fast and the console.
It is overwhelmingly easy to write bad unit tests that add very little value to a project while astronomically inflating the cost of code changes. This post will go through the JUnit best practices we must consider while writing the test cases. In programming, “Unit testing is a method by …
HowToDoInJava provides tutorials and how-to guides on Java and related technologies.
It also shares the best practices, algorithms & solutions and frequently asked interview questions.