Spring Boot学习(四):使用@SpringBootTest注解进行单元测试

目录

一、简介

二、环境准备

三、代码示例


一、简介

项目中经常会遇到需要单元测试的情况,那么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

 

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值