Adding JUnit Support to Spring Boot Application

This tutorial will teach you how to add JUnit support to your maven-based Spring Boot application.

Spring Boot Starter Test Dependency

To add JUnit and Mockito support to your maven-based Spring Boot application, add the following dependency to pom.xml file.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

The above dependency will add to your project many useful libraries, including JUnit Jupiter, Mockito and Hamcrest. Let’s look at the complete list of compile dependencies you will get by adding spring-boot-started-test dependency to your project.

  • JUnit Jupiter,
  • Mockito,
  • Mockito-core,
  • Mockito JUnit Jupiter support,
  • json-path,
  • jackarta.xml.bind-api,
  • assertj-core,
  • hamcrest,
  • jsonassert,
  • spring-core,
  • spring-test,
  • xmlunit-core,
  • spring-boot-starter,
  • spring-boot-test,
  • spring-boot-test-autoconfigure.

Once you add the spring-boot-started-test dependency to pom.xml file of your application, you should be ready to start writing your Unit Tests.

Spring Security Test

In the Spring Boot application that has Spring Security enabled, there is one more dependency to add.

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-test</artifactId>
    <scope>test</scope>
</dependency>

The spring-security-test dependency will help your test methods interact with the Spring Security layer.

Video Tutorial

If you want to learn more about how to test your Java application with JUnit and Mockito, look at the list of tutorials in JUnit category.