Spring Boot Tutorial - Test

Keywords: Java Spring Junit SpringBoot

1. Introduction to Application Testing

Usually after we have written the code, the test of the code will be given to a dedicated tester to test. If a test runs to your job and says to you that your code seems to have a Bug, you will be unhappy. That's what I am doing anyway(viii).So to show you that your code is a bit of a good quality, we'll test it for ourselves before the functionality is submitted for testing, and then we'll show you the Spring Boot Test application testing framework.

Spring Boot Test is actually Spring Test, but it's easier to integrate in Spring Boot. Like when we develop our own tests, it's usually unit test Junit test. Thank goodness for not having a bug. Spring Boot Test combined with JUnit provides an efficient and convenient test solution, while Spring Boot Test is in Spring Boot TestTest adds slice testing and enhances Mock capabilities.

The types of tests supported by Spring Boot Test are divided into three main categories:

  • Unit tests, Method-oriented tests, commonly noted with @Test.(This is usually used)

  • Functional testing, business-oriented testing, but also the ability to use Mock in faceted testing, commonly noted with @RunWith, @SpringBootTest, and so on.(This is also much more useful)

  • Tile testing, for boundary features that are difficult to test, is between unit testing and functional testing. Common notes are @RunWith, @WebMvcTest, and so on.

The key elements and support methods in the testing process are as follows:

  • Test the run environment and start the Spring container with @RunWith and @SpringBootTest.
  • Mock capability, Mockito provides Mock capability.
  • Assertion capability, AssertJ, Hamcrest, JsonPath provide assertion capability.

Next, I'll show you how to use the Spring Boot Test framework simply.

2. Use of Spring Boot Test

  • 2.1 Introducing dependencies

Opening a test in Spring Boot simply introduces a spring-boot-starter-test dependency and starts the test using the @RunWith and @SpringBootTest annotations.Let's just test the interface. First, we introduce a pom dependency:


<!--springboot Parent Project-->
        <relativePath/> <!-- lookup parent from repository -->

        <!--springboot frame web assembly-->
        <!--mybatis integration springboot assembly-->
        <!--mysql Database Connection Driver-->
        <!--lombok assembly-->
        <!--spring boot-test assembly-->
        <!--unit testing junit assembly-->
        <!--spring-test assembly-->

        <!--springboot Of maven Plug-in unit-->
  • 2.2 Coding

    The test code is usually written in the test folder level with the main folder. It is recommended that the name of the folder correspond to the folder under the main folder, and the name of the test class correspond to the folder under the main folder, as shown below. Of course, this is only a suggestion.

    I've written no more about Spring Boot's startup class, nothing special, just test it.


    Yes, every method tested here is StudentService.java The method names of this service class correspond, and we recommend that you do the same.

    package com.butterflytri.service;
    import com.butterflytri.TestApplication;
    import com.butterflytri.entity.Student;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    import javax.annotation.Resource;
    import java.util.List;
     * @author: WJF
     * @date: 2020/5/23
     * @description: StudentServiceTest
     * {@link SpringBootTest}: Read profile properties.Reads the configuration file and running environment of the boot class for the classes flag and loads it.
     * {@link RunWith}: 'RunWith'A comment is a runner that loads the Class test environment for value.
    @SpringBootTest(classes = TestApplication.class)
    public class StudentServiceTest {
        private StudentService studentService;
        public void findAllTest() {
            List<Student> list = studentService.findAll();
            for (Student student : list) {
        public void findOneTest() {
            Student student = studentService.findOne(1L);
        public void findByStudentNoTest() {
            Student student = studentService.findByStudentNo("G030511");

    Each test method can be run independently, because the test environment of Spring Test is loaded while running, and the configuration of the running environment of the project you are testing is loaded, using the configuration and environment of the project for our own testing.

3. Project Address

This project portal: spring-boot-test

This tutorial will be updated all the time. If you think the blogger can write it, pay attention to it, or it will be easier to learn it next time.

Posted by vinny69 on Sat, 30 May 2020 10:06:20 -0700