This case is based on spring boot 1.5.1 junit4.1
Reprinted: http://blog.csdn.net/tengxing007/article/details/73332038 with slight modifications
foreword
Junit is a unit testing framework for the Java language that is used by developers to implement unit testing of applications, speed up programming, and improve coding quality. It is in development, and now it has reached junit5. It is integrated with many frameworks in javaEE development, making it very convenient for developers.
Junit common annotations:
- @Before: initialization method
- @After: release resources
- @Test: Test method, where you can test expect exceptions and timeouts
- @Ignore: ignored test method
- @BeforeClass: Executed only once for all tests, and must be static void
- @AfterClass: Executed only once for all tests, and must be static void
- @RunWith: Specifies the unit test execution class to use
Junit test case execution order:
@BeforeClass ==> @Before ==> @Test ==> @After ==> @AfterClass
process: load the simulated environment first, and then test .
test preparation
Dependency version (there are some differences between different versions)
- junit 4.12
- spring-test 4.3.6
- spring-boot-test 1.5.1
Add dependencies (required)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version> 1.5.1.RELEASE</version> </dependency>
editor (optional)
IntellijIDEA
test code
The test code is as follows:
import cn.yjxxclub.springboot.entity.Member; import cn.yjxxclub.springboot.mapper.MemberMapper; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.test.context.junit4.SpringRunner; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Author: Meet Xiaoxing * Email: [email protected] * Date: 17-6-16 * Time: 12:18 PM * Describe: member application test class */ @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class MemberTest { /** * Convenience alternative to Spring RestTemplate. You can get a normal or send basic HTTP authentication (with username and password) template * not used here */ @Autowired private TestRestTemplate testRestTemplate; @Autowired MemberMapper memberMapper; /** * 2017-06-16 14:08:09.884 INFO 13803 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited size:5 -----Testing completed------- 2017-06-16 14:08:09.955 INFO 13803 --- [ Thread-4] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@fd07cbb: startup date [Fri Jun 16 14:08:04 CST 2017]; root of context hierarchy */ @Test public void test(){ Map<String,Object> map = new HashMap(); map.put("start",0); map.put("size",8); List<Member> list = memberMapper.list(map); System.out.println("size:"+list.size()); System.out.println("-----Test completed-------"); } }
code description
- @RunWith is provided by junit, the preface has already said
- SpringRunner is the test execution unit class provided by spring-test (the new name of SpringJUnit4ClassRunner)
- @SpringBootTest is saying "bootstrap with Spring Boot's support", similar to the test boot entry of the springboot program
, please see the spring.io explanation: