JUnit Basics

Running JUnit Tests with Maven

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.

Spring Boot, Mockito and JUnit 5 Example

Learn to write unit tests for the service layer of Spring boot applications using JUnit 5 and Mockito testing frameworks. We are using Spring Boot 3 in this demo. For Spring Boot applications, we only need to change the import statements, and everything should work automatically. 1. Maven The spring-boot-starter-test …

Difference between JUnit 5 an JUnit 4

JUnit 5 aims to adapt java 8 style of coding and to be more robust and flexible than JUnit 4. In this post, JUnit 5 vs JUnit 4, we will focus on some major differences between them.

JUnit 5 Expected Exception: assertThrows() Example

When writing unit tests, it makes sense to check whether certain methods throw the expected exceptions when we supply invalid inputs or pre-conditions that are not satisfied. Junit 5 provides the following assertion methods for handling test exceptions. These method names are self-explanatory enough to suggest their usage. Please continue …

JUnit 5 Assertions – JUnit assert Examples

JUnit 5 assertions help in validating the expected output with actual output of a testcase. To keep things simple, all JUnit Jupiter assertions are static methods in the org.junit.jupiter.Assertions class.

Test Spring Security Auth with JUnit

Learn to test Spring security authentication using JUnit testcase using InMemoryDaoImpl. Also learn to build fully populated authentication object programmatically and then use it in application. SecurityContextHolder Spring security is based on security context, which is kind of static in nature. This essentially means that your do not need to …

JUnit JUnitCore Example

In any application, which is being built incrementally, often it is desired that we should be able to run only certain tests whenever a new feature is introduced. This can be achieved using JUnitCore class of JUnit framework. JUnitCore is an inbuilt class in JUnit package and it is based on facade …

Unit Testing Best Practices

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 …

About Us

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.