目录
一、简介
项目中经常会遇到需要单元测试的情况,那么SpringBoot如何实现这种需求,使用@SpringBootTest注解可以运行环境,测试后台代码。
二、环境准备
eclipse + maven + Spring Boot
三、代码示例
pom.xml文件中引入test包依赖,如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
编写测试类MyprojectApplicationTests.java,加入@SpringBootTest注解
package com.example.myproject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyprojectApplicationTests {
//TODO 此处可以注意需要测试的服务接口
@Test
public void contextLoads() {
System.out.println("测试类出来了");
//TODO 此处调用接口方法
}
}
运行结果
11:43:28.377 [main] DEBUG org.springframework.core.env.StandardEnvironment - Adding PropertySource 'Inlined Test Properties' with highest search precedence
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.3.RELEASE)
2018-09-13 11:43:28.666 INFO 2124 --- [ main] c.e.myproject.MyprojectApplicationTests : Starting MyprojectApplicationTests on DESKTOP-249EABQ with PID 2124 (started by admin in D:\WorkSpace\studyWorkspace\myproject)
2018-09-13 11:43:28.667 INFO 2124 --- [ main] c.e.myproject.MyprojectApplicationTests : No active profile set, falling back to default profiles: default
2018-09-13 11:43:28.702 INFO 2124 --- [ main] o.s.w.c.s.GenericWebApplicationContext : Refreshing org.springframework.web.context.support.GenericWebApplicationContext@5d7148e2: startup date [Thu Sep 13 11:43:28 CST 2018]; root of context hierarchy
2018-09-13 11:43:30.157 INFO 2124 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-13 11:43:30.374 INFO 2124 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.web.context.support.GenericWebApplicationContext@5d7148e2: startup date [Thu Sep 13 11:43:28 CST 2018]; root of context hierarchy
2018-09-13 11:43:30.461 INFO 2124 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/myhello/hello]}" onto public java.lang.String com.example.myproject.controller.HelloWorldController.index()
2018-09-13 11:43:30.471 INFO 2124 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-09-13 11:43:30.471 INFO 2124 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-09-13 11:43:30.521 INFO 2124 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-13 11:43:30.523 INFO 2124 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-13 11:43:31.133 INFO 2124 --- [ main] c.e.myproject.MyprojectApplicationTests : Started MyprojectApplicationTests in 2.746 seconds (JVM running for 3.855)
测试类出来了
2018-09-13 11:43:31.441 INFO 2124 --- [ Thread-2] o.s.w.c.s.GenericWebApplicationContext : Closing org.springframework.web.context.support.GenericWebApplicationContext@5d7148e2: startup date [Thu Sep 13 11:43:28 CST 2018]; root of context hierarchy