这篇文章主要介绍了SpringBoot使用@SpringBootTest注解开发单元测试教程,本文通过详细的案例过程来说明如何使用该项技术,需要的朋友可以参考下
概述
@SpringBootTest注解是SpringBoot自1.4.0版本开始引入的一个用于测试的注解。基本用法如下:
1.添加依赖:
4.0.0com.cxh.testgenerator0.0.1-SNAPSHOT org.springframework.bootspring-boot-starter-parent1.5.1.RELEASE<!-- lookup parent from repository --> UTF-8UTF-81.8 <!-- spring boot web 依赖 --> org.springframework.bootspring-boot-starter-web org.springframework.bootspring-boot-starter-test<!-- 修改后立即生效,热部署 --> org.springframeworkspringloaded org.springframework.bootspring-boot-devtools<!-- 视图 --> org.springframework.bootspring-boot-starter-thymeleaf<!-- mybatis 依赖 --> org.mybatis.spring.bootmybatis-spring-boot-starter1.3.0<!-- mysql 依赖 --> mysqlmysql-connector-java5.0.4<!-- alibaba的druid数据库连接池 --> com.alibabadruid-spring-boot-starter1.1.9 org.springframework.cloudspring-cloud-dependenciesCamden.SR6pomimport
2. 编写启动入口类
package com.cxh.study.platform; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * * @desciption * @author ChenXiHua * */ @SpringBootApplication public class GeneratorApp { /** * @param args */ public static void main(String[] args) { SpringApplication.run(GeneratorApp.class, args); } }
3. 编写Controller类
package com.cxh.study.platform; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * * @desciption * @author ChenXiHua * */ @SpringBootApplication public class GeneratorApp { /** * @param args */ public static void main(String[] args) { SpringApplication.run(GeneratorApp.class, args); } }
4. 编写service类
package com.cxh.study.platform.service; import java.util.List; import com.cxh.study.platform.entity.User; public interface IUserService { // 获取所有用户 List getAll(); // 根据id获取用户 User getUserById(Integer id); }
package com.cxh.study.platform.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.cxh.study.platform.entity.User; import com.cxh.study.platform.mapper.IUserMapper; import com.cxh.study.platform.service.IUserService; @Service("userServiceImpl") public class UserServiceImpl implements IUserService { @Autowired private IUserMapper userMapper; @Override public List getAll() { return userMapper.getAll(); } @Override public User getUserById(Integer id) { return userMapper.getUserById(id); } }
5. 编写mapper类
package com.cxh.study.platform.mapper; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.cxh.study.platform.entity.User; @Mapper public interface IUserMapper { //获取所有用户 ListgetAll(); //根据id获取用户 User getUserById(Integer id); }
<!--获取所有用户 --> SELECT * FROM s_user <!-- 根据id获取单个用户 --> SELECT * FROM s_user where id=#{id}
6. 编写测试类
package com.cxh.test; import java.net.URL; import java.util.List; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner; import com.cxh.study.platform.GeneratorApp; import com.cxh.study.platform.entity.User; /** * * @desciption 用户管理测试类 * @author ChenXiHua * @date 2019年2月19日 * */ @RunWith(SpringRunner.class) @SpringBootTest(classes = GeneratorApp.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class UserTest { /** * @LocalServerPort 提供了 @Value("${local.server.port}") 的代替 */ @LocalServerPort private int port; private URL base; @Autowired private TestRestTemplate restTemplate; @Before public void setUp() throws Exception { String url = String.format("http://localhost:%d/", port); System.out.println(String.format("port is : [%d]", port)); this.base = new URL(url); } /** * 向"/test"地址发送请求,并打印返回结果 * @throws Exception */ //@Test public void test1() throws Exception { ResponseEntity response = this.restTemplate.getForEntity( this.base.toString() + "/test", String.class, ""); System.out.println(String.format("测试结果为:%s", response.getBody())); } //@Test publi<span style="color:transparent">来源gaodai#ma#com搞*!代#%^码$网</span>c void getAllTest() throws Exception { ResponseEntity response = this.restTemplate.getForEntity( this.base.toString() + "/getAll", List.class, ""); System.out.println(String.format("测试结果为:%s", response.getBody())); } @Test public void getUserByIdTest() throws Exception { ResponseEntity response = this.restTemplate.getForEntity( this.base.toString() + "/getUserById?id=1", User.class, ""); System.out.println(String.format("测试结果为:%s", response.getBody().toString())); } }
其中,classes属性指定启动类,SpringBootTest.WebEnvironment.RANDOM_PORT经常和测试类中@LocalServerPort一起在注入属性时使用。会随机生成一个端口号。
7.测试结果:
2019-02-19 16:18:27.933 INFO 6676 — [ main] com.cxh.test.UserTest : Started UserTest in 25.637 seconds (JVM running for 27.647)
port is : [14067]
2019-02-19 16:18:31.366 INFO 6676 — [o-auto-1-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet ‘dispatcherServlet’
2019-02-19 16:18:31.367 INFO 6676 — [o-auto-1-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet ‘dispatcherServlet’: initialization started
2019-02-19 16:18:31.462 INFO 6676 — [o-auto-1-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet ‘dispatcherServlet’: initialization completed in 95 ms
测试结果为:User [id=1, account=cxh, password=123, type=1, status=1]
2019-02-19 16:18:35.326 INFO 6676 — [ Thread-5] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@191004a: startup date [Tue Feb 19 16:18:05 CST 2019]; root of context hierarchy
以上就是SpringBoot使用@SpringBootTest注解开发单元测试教程的详细内容,更多请关注gaodaima搞代码网其它相关文章!