Spring Boot的一个测试用例
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的一个测试用例的更多相关文章
- spring cloud教程之使用spring boot创建一个应用
<7天学会spring cloud>第一天,熟悉spring boot,并使用spring boot创建一个应用. Spring Boot是Spring团队推出的新框架,它所使用的核心技术 ...
- Spring Boot实现一个监听用户请求的拦截器
项目中需要监听用户具体的请求操作,便通过一个拦截器来监听,并继续相应的日志记录 项目构建与Spring Boot,Spring Boot实现一个拦截器很容易. Spring Boot的核心启动类继承W ...
- 如何基于Spring Boot搭建一个完整的项目
前言 使用Spring Boot做后台项目开发也快半年了,由于之前有过基于Spring开发的项目经验,相比之下觉得Spring Boot就是天堂,开箱即用来形容是绝不为过的.在没有接触Spring B ...
- 记录Spring Boot大坑一个,在bean中如果有@Test单元测试,不会注入成功
记录Spring Boot大坑一个,在bean中如果有@Test单元测试,不会注入成功 记录Spring Boot大坑一个,在bean中如果有@Test单元测试,不会注入成功 记录Spring Boo ...
- 安装使用Spring boot 写一个hello1
一.创建springboot 项目 二.进行代编写 1.连接数据库:application.properties里配置 spring.datasource.driverClassName=com.my ...
- spring boot是一个应用框架生成工具?
spring boot是一个应用框架生成工具?
- 手把手教你用 Spring Boot搭建一个在线文件预览系统!支持ppt、doc等多种类型文件预览
昨晚搭建环境都花了好一会时间,主要在浪费在了安装 openoffice 这个依赖环境上(Mac 需要手动安装). 然后,又一步一步功能演示,记录,调试项目,并且简单研究了一下核心代码之后才把这篇文章写 ...
- 学记:为spring boot写一个自动配置
spring boot遵循"约定优于配置"的原则,使用annotation对一些常规的配置项做默认配置,减少或不使用xml配置,让你的项目快速运行起来.spring boot的神奇 ...
- [译]Spring Boot 构建一个RESTful Web服务
翻译地址:https://spring.io/guides/gs/rest-service/ 构建一个RESTful Web服务 本指南将指导您完成使用spring创建一个“hello world”R ...
随机推荐
- C#学习日志 day10 -------------- problem statement
Revision History Date Issue Description Author 15/May/2015 1.0 Finish most of the designed function. ...
- linux下gmplayer安装(亲测OK!)
需要的安装包及下载地址: mplayer源代码包(MPlayer-1.0rc4.tar.bz2)下载:http://www.mplayerhq.hu/MPlayer/releases/ 解码器安装包 ...
- 通过Orchard认识的Autofac
反射Reflection 这是.Net中获取运行时类型信息的方式,.Net的应用程序由几个部分:'程序集(Assembly)'.'模块(Module)'.'类型(class)'组成,而反射提供一种编程 ...
- C++设计模式之命令模式
厨师类: #include "Cooker.h" Cooker::Cooker(void) { } Cooker::~Cooker(void) { } void Cooker::M ...
- java 执行linux命令
原文地址: http://blog.csdn.net/xh16319/article/details/17302947 package scut.cs.cwh; import java.io.Inpu ...
- DMVsinSQLServer -- 备
/*************************************************************************************************** ...
- Referrer 还是 Referer?
上回我写了一篇文章介绍「Referrer Policy」,有小伙伴看完后问我:Referrer 这个单词到底怎么拼,为什么有时候中间有两个 r,有时候只有一个? 是的,这是一个很有趣的问题,这里就给有 ...
- Hibernate get 和load的区别
1 load是要用的时候才从数据库去查询,get 是马上查询. 2 对于不存在的记录,get会报空指针异常,load会报 org.hibernate.ObjectNotFoundException: ...
- Android View动画
Animation TypeEvaluator View的animate方法 ValueAnimator ObjectAnimator AnimatorSet 使用xml来创建动画 animation ...
- C. Table Decorations(Codeforces Round 273)
C. Table Decorations time limit per test 1 second memory limit per test 256 megabytes input standard ...