This is the 10th day of my participation in the August More Text Challenge. For details, see:August is more challenging
Include a column
Spring Boot quick start
Java full stack Architect
preface
When we are developing, we often need to write test code to verify that the completed function is running according to the pre-designed business logic. Unit tests will be used. Of course, using junit to write some appropriate tests can quickly detect the correctness of the business logic and adjust and optimize our code in time. This article will begin by introducing the Spring Boot integration JUnit. Let’s start with JUnit.
What JUnit
Junit is a unit testing framework for the Java language. Founded by Kent Beck and Erich Gamma, it has grown to become the most successful of sUnit’s xUnit family originating from Kent Beck. JUnit has its own JUnit extension ecosystem. Most Java development environments have incorporated JUnit as a unit testing tool. JUnit has been the most popular unit testing framework in the Java field for decades.
JUnit role
Junit allows us to quickly complete unit tests, and write tests and code are incremental, write a bit and test a bit, in the future code if found problems can be traced to the cause of the problem quickly, reducing the difficulty of error correction regression.
JUnit features
JUnit is an open source Java testing framework for writing and running repeatable tests. It is an instance of the unit testing framework xUnit (for the Java language). It includes the following features: 1. assertions for testing expected results 2. test tools for sharing common test data 3. test suites for easy organization and running tests 4. graphical and textual test runners
JUnit advantages
- Simple installation
- Extreme programming (xp)
- refactoring
Quick start
Led package
This test is using a Java project built by Maven, so you should first introduce the appropriate dependencies in pom.xml. The specific use needs to be combined with the version of the framework being used. Find the right version. The dependencies used in this article are as follows:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions></dependency> <! -- junjit --><dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
Copy the code
Commonly used annotations
Commonly used annotations | describe |
---|---|
@Test | Mark a method as a test method |
@Before | The method that must be executed before each test method call |
@After | The method that must be executed after each test method call |
@BeforeClass | All test methods are executed once before they are called. They are loaded before the test class is instantiated |
@AfterClass | All test methods are called once. They are loaded before the test class is instantiated. They need to be static |
@lgnore | This method is not implemented for now |
@Timeout | Indicates that the test method will return an error if it runs longer than the specified time |
@ExtendWith | Provides an extended class reference for a test class or test method |
@Disabled | Indicates that the test class or test method is not executed |
The execution sequence is @beforeClass –> @before –> @test –> @After –> @AfterClass
Spring Boot annotations
- @ SpringBootTest (classes = DemoJunitApplication. Class) : the introduction of an annotation for the test
- RunWith(springrunner.class) is a runner that specifies the class to run and has the tests run in the Spring container environment.
- Rollback: Rollback data to avoid contaminating the database with test data
- AutoConfigureMockMvc: Automatically set values
The project structure
The source code
pom.xml
<? xml version="1.0" encoding="UTF-8"? ><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0. RELEASE</version>
<relativePath/> <! -- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>junit</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
<name>junit</name>
<description>Demo project for Spring Boot and MyBatis and Swagger</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<! -- commons start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<! -- commons end -->
<! -- mybatis start-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<! -- mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<! -- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.11</version>
</dependency>
<! -- mybatis end-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<! -- junjit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Copy the code
The test class
package com.example.demo;
import com.example.demo.controller.TestController;
import com.example.demo.service.UserService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = DemoJunitApplication.class)
@RunWith(SpringRunner.class)
@Rollback
@AutoConfigureMockMvc
class DemoApplicationTests {
@Autowired
private TestController testController;
@Autowired
private UserService userService;
/ * * *@ClassName Test interface@Description: Obtains all users *@Author JavaZhan @public id :Java full stack Architect *@Date 2020/6/13
* @Version V1.0 * * /
@Test
void getTest() {
System.out.println(testController.getTest());
}
/ * * *@ClassName getAllUser
* @Description: Obtains all users *@Author JavaZhan @public id :Java full stack Architect *@Date 2020/6/13
* @Version V1.0 * * /
@Test
void getAllUser(){ userService.getAllUser().forEach(user -> System.out.println(user)); }}Copy the code
The results
conclusion
This project based on Spring Boot integration JUnit is complete, I believe you have a simple understanding of JUnit unit testing, hope this article can help you. Thanks for reading.
Author introduction: [little ajie] a love to tinker with the program ape, JAVA developers and enthusiasts. Public account [Java full stack architect] maintainer, welcome to pay attention to reading communication.
Well, thank you for reading, I hope you like it, if it is helpful to you, welcome to like the collection. If there are any shortcomings, please comment and correct them. See you next time.