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:

pom.xml:

<!--springboot Parent Project-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <!--springboot frame web assembly-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
        <!--mybatis integration springboot assembly-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>
        <!--mysql Database Connection Driver-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>
        <!--lombok assembly-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>
        <!--spring boot-test assembly-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>
        <!--unit testing junit assembly-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!--spring-test assembly-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.2.RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <!--springboot Of maven Plug-in unit-->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <compilerArgs>
                        <arg>-parameters</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>
  • 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.

    StudentServiceTest.java:

    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)
    @RunWith(SpringRunner.class)
    public class StudentServiceTest {
    
        @Resource
        private StudentService studentService;
    
        @Test
        public void findAllTest() {
            List<Student> list = studentService.findAll();
            for (Student student : list) {
                System.out.println(student);
            }
        }
    
        @Test
        public void findOneTest() {
            Student student = studentService.findOne(1L);
            System.out.println(student);
        }
    
        @Test
        public void findByStudentNoTest() {
            Student student = studentService.findByStudentNo("G030511");
            System.out.println(student);
        }
    
    }
    

    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