Spring Boot的一个测试用例

时间:2022-07-23 12:47:04
package tk.mybatis.springboot.mapper;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.springboot.Application;
import tk.mybatis.springboot.model.City2; import java.util.ArrayList;
import java.util.List; /**
* @author liuzh
* @since 2016-03-06 17:42
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@Transactional
@SpringApplicationConfiguration(Application.class)
public class MyBatis331Test {
private Logger logger = LoggerFactory.getLogger(getClass()); @Autowired
private MyBatis331Mapper mapper; @Test
@Rollback
public void testInsertList() {
List<City2> city2List = new ArrayList<City2>();
city2List.add(new City2("石家庄", "河北"));
city2List.add(new City2("邯郸", "河北"));
city2List.add(new City2("秦皇岛", "河北"));
Assert.assertEquals(3, mapper.insertCities(city2List));
for (City2 c2 : city2List) {
logger.info(c2.toString());
Assert.assertNotNull(c2.getId());
}
} @Test
public void testSelectById(){
City2 city2 = mapper.selectByCityId(1);
logger.info(city2.toString());
Assert.assertNotNull(city2);
Assert.assertNotNull(city2.getCityName());
Assert.assertNotNull(city2.getCityState());
} @Test
public void testSelectAll(){
List<City2> city2List = mapper.selectAll();
for(City2 c2 : city2List){
logger.info(c2.toString());
Assert.assertNotNull(c2);
Assert.assertNotNull(c2.getCityName());
Assert.assertNotNull(c2.getCityState());
}
} }
@Documented
@Inherited
@Retention(value=RUNTIME)
@Target(value=TYPE)
public @interface SpringApplicationConfiguration
Class-level annotation that is used to determine how to load and configure an ApplicationContext for integration tests.
Similar to the standard ContextConfiguration but uses Spring Boot's SpringApplicationContextLoader.
Author:
Dave Syer
See Also:
SpringApplicationContextLoader
http://docs.spring.io/spring-boot/docs/1.1.x/api/org/springframework/boot/test/SpringApplicationConfiguration.html

What you are trying to do can easily be done with the following code:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestContextConfiguration.class)
public class ReportResponseAssemblerTest { @Autowired
ReportInstanceResponseAssembler reportResponseAssembler; @Test
public void testPlaceHolder() {
Assert.assertNotNull(reportResponseAssembler);
}
} @EnableAutoConfiguration
@ComponentScan(basePackages = { "com.blahpackage.service.assembler" })
@Configuration
public class TestContextConfiguration { }

The three classes you mention need to be under com.blahpackage.service.assembler and also have to be annotated with some Spring stereotype annotation, like @Component or @Service. For example you would have:

@Component
public class ReportResponseAssembler { @Autowired
private ParameterResponseAssembler parameterResponseAssembler; @Autowired
private TimeRangeResponseAssembler timeRangeResponseAssembler; public ReportResponseAssembler makeResponse() {
return new ReportResponseAssembler();
}
} @Component
public class ParameterResponseAssembler {
//whatever
}

I would however advise that you use such a test rarely because of the performance implications. What I mean is that if you have a lot of these types of tests, Spring needs to create and destroy a different application context for each one, whereas if you use the same context and tests, Spring can (usually) cache the context. Check out this blog post for more details

http://*.com/questions/26370743/how-to-use-springapplicationconfiguration-or-contextconfiguration-to-load-smalle

Spring Boot的一个测试用例的更多相关文章

  1. spring cloud教程之使用spring boot创建一个应用

    <7天学会spring cloud>第一天,熟悉spring boot,并使用spring boot创建一个应用. Spring Boot是Spring团队推出的新框架,它所使用的核心技术 ...

  2. Spring Boot实现一个监听用户请求的拦截器

    项目中需要监听用户具体的请求操作,便通过一个拦截器来监听,并继续相应的日志记录 项目构建与Spring Boot,Spring Boot实现一个拦截器很容易. Spring Boot的核心启动类继承W ...

  3. 如何基于Spring Boot搭建一个完整的项目

    前言 使用Spring Boot做后台项目开发也快半年了,由于之前有过基于Spring开发的项目经验,相比之下觉得Spring Boot就是天堂,开箱即用来形容是绝不为过的.在没有接触Spring B ...

  4. 记录Spring Boot大坑一个,在bean中如果有&commat;Test单元测试,不会注入成功

    记录Spring Boot大坑一个,在bean中如果有@Test单元测试,不会注入成功 记录Spring Boot大坑一个,在bean中如果有@Test单元测试,不会注入成功 记录Spring Boo ...

  5. 安装使用Spring boot 写一个hello1

    一.创建springboot 项目 二.进行代编写 1.连接数据库:application.properties里配置 spring.datasource.driverClassName=com.my ...

  6. spring boot是一个应用框架生成工具?

    spring boot是一个应用框架生成工具?

  7. 手把手教你用 Spring Boot搭建一个在线文件预览系统!支持ppt、doc等多种类型文件预览

    昨晚搭建环境都花了好一会时间,主要在浪费在了安装 openoffice 这个依赖环境上(Mac 需要手动安装). 然后,又一步一步功能演示,记录,调试项目,并且简单研究了一下核心代码之后才把这篇文章写 ...

  8. 学记:为spring boot写一个自动配置

    spring boot遵循"约定优于配置"的原则,使用annotation对一些常规的配置项做默认配置,减少或不使用xml配置,让你的项目快速运行起来.spring boot的神奇 ...

  9. &lbrack;译&rsqb;Spring Boot 构建一个RESTful Web服务

    翻译地址:https://spring.io/guides/gs/rest-service/ 构建一个RESTful Web服务 本指南将指导您完成使用spring创建一个“hello world”R ...

随机推荐

  1. poj 3764 The xor-longest Path &lpar;01 Trie)

    链接:http://poj.org/problem?id=3764 题面: The xor-longest Path Time Limit: 2000MS   Memory Limit: 65536K ...

  2. Debian下安装中文包和输入法

    Debian下安装中文包和输入法 #aptitude install locales(没有aptitude的话可以先安装apt-get insall aptitude )  #dpkg-reconfi ...

  3. 第三章 JQuery&colon; HelloWorld--常见方法--css--选择器--筛选器--属性--效果--事件--数组操作--字符串操作--对象转换

    1.jQuery简介 为了简化JavaScript 的开发, 一些JavsScript 库诞生了. JavaScript库封装了很多预定义的对象和实用函数.能帮助使用者建立有高难度交互的页面, 并且兼 ...

  4. 纯HTML和CSS实现点击切换

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. laraval一键安装包的下载地址

    http://laravelacademy.org/resources-download

  6. OSGI企业应用开发(三)Eclipse中搭建Equinox运行环境

    上篇文章介绍了如何在Eclipse中搭建Felix的运行环境,我们需要將Bundle发布到Felix框架的bundle目录下,Felix框架启动时才会自动加载这些Bundle,否则需要在Felix框架 ...

  7. unity3d-----Collider 组件参考

    Collider 组件参考 点击 属性检查器 下面的 添加组件 按钮,然后从 添加碰撞组件 中选择需要的 Collider 组件,即可添加 Collider组件到节点上. Collider 组件属性 ...

  8. linux Makefile(中文版1)

    ############################################################################## Generic Makefile for ...

  9. 腾讯优图&amp&semi;港科大提出一种基于深度学习的非光流 HDR 成像方法

    目前最好的高动态范围(HDR)成像方法通常是先利用光流将输入图像对齐,随后再合成 HDR 图像.然而由于输入图像存在遮挡和较大运动,这种方法生成的图像仍然有很多缺陷.最近,腾讯优图和香港科技大学的研究 ...

  10. Math&period;random理解练习

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...