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://stackoverflow.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中如果有@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. [译]Spring Boot 构建一个RESTful Web服务

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

随机推荐

  1. jquery获取和失去焦点改变样式

    第一种:(文本框获取焦点后,它的颜色会有所变化,当失去焦点的时候,恢复为原来的样子) <html> <meta http-equiv="Content-Type" ...

  2. 解决ie6里png图片透明变白色bug

    加入这段js就行了. function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6. { var a ...

  3. SQL Server 恢复过程

    在恢复过程中.只会分析那些自最后一个检查点之后发生的更改,以确定是否需要重做还是撤销. 在最后一个检查点之前完成的操作都会精确的反应到数据文件中,恢复过程不需要做其它的事. 第一阶段: 分析. 这个阶 ...

  4. eclipse快捷键说明

    Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行  Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt ...

  5. 解决Robotium测试用例crash问题

    今天遇到一个棘手的问题 用robotium框架真机测试客户端时 跑到一半会crash 搜了一堆资料终于解决了 我的程序引起crash主要原因有两个: 1.用Robotium测试框架跑多个用例(写在同一 ...

  6. Mysql 语句汇总(性能篇)

    查询mysql 哪些表正在被锁状态: show open TABLES where In_use > 0; show open table from XXX(数据库名);//查看数据库哪些表正在 ...

  7. linux 磁盘空间扩容 vg(+pv) lv(+空间) lv(缩减磁盘空间)

    preFace APP scenario description: 当你未能合理的规划存储时,在后期的维护工作中可能会涉及的存储的 再规划(eg,某一个 or 数个App 对某一个lv 即挂载点写Bi ...

  8. spoj 7258 Lexicographical Substring Search (后缀自动机)

    spoj 7258 Lexicographical Substring Search (后缀自动机) 题意:给出一个字符串,长度为90000.询问q次,每次回答一个k,求字典序第k小的子串. 解题思路 ...

  9. IOS深入学习(3)之Control Object

    1 前言 今天我们来简单的学习一下IOS中用户点击屏幕后的事件处理,其中主要介绍一下Control Object,内容如下. 2 详述 Control是处于当用户用某种方式操作进行发送消息给另一个界面 ...

  10. linux内核源码阅读之facebook硬盘加速flashcache之五

    正常流程到flashcache_map的1623行或1625行,按顺序先看读流程: 1221static void 1222flashcache_read(struct cache_c *dmc, s ...