理一理Spring如何对接JUnit
测试代码
package org.simonme.srcstudy.spring3.demo.stub;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.simonme.srcstudy.spring3.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* <一句话功能简述>
* <功能详细描述>
*
* @author http://www.cnblogs.com/simoncook
* @version [版本号, 2017年11月4日]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext.xml"})
public class UserServiceAssemblyByJUnit
{
private UserService userService;
@Test
public void test()
{
assertNotNull(userService);
}
public UserService getUserService()
{
return userService;
}
@Autowired
public void setUserService(UserService userService)
{
this.userService = userService;
}
分析方式
this.userService = userService;
这一行直接断点
堆栈信息
org.simonme.srcstudy.spring3.demo.stub.UserServiceAssemblyByJUnit.setUserService(org.simonme.srcstudy.spring3.demo.service.UserService) line: 50
sun.reflect.NativeMethodAccessorImpl.invoke0(java.lang.reflect.Method, java.lang.Object, java.lang.Object[]) line: not available [native method]
sun.reflect.NativeMethodAccessorImpl.invoke(java.lang.Object, java.lang.Object[]) line: 39
sun.reflect.DelegatingMethodAccessorImpl.invoke(java.lang.Object, java.lang.Object[]) line: 25
java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object...) line: 597
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(java.lang.Object, java.lang.String, org.springframework.beans.PropertyValues) line: 582
org.springframework.beans.factory.annotation.InjectionMetadata.inject(java.lang.Object, java.lang.String, org.springframework.beans.PropertyValues) line: 84
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(org.springframework.beans.PropertyValues, java.beans.PropertyDescriptor[], java.lang.Object, java.lang.String) line: 282
org.springframework.beans.factory.support.DefaultListableBeanFactory(org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory).populateBean(java.lang.String, org.springframework.beans.factory.support.AbstractBeanDefinition, org.springframework.beans.BeanWrapper) line: 1074
org.springframework.beans.factory.support.DefaultListableBeanFactory(org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory).autowireBeanProperties(java.lang.Object, int, boolean) line: 374
org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(org.springframework.test.context.TestContext) line: 110
org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(org.springframework.test.context.TestContext) line: 75
org.springframework.test.context.TestContextManager.prepareTestInstance(java.lang.Object) line: 321
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest() line: 220
org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall() line: 301
org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1(org.junit.internal.runners.model.ReflectiveCallable).run() line: 12
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(org.junit.runners.model.FrameworkMethod) line: 303
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(org.junit.runners.model.FrameworkMethod, org.junit.runner.notification.RunNotifier) line: 240
org.springframework.test.context.junit4.SpringJUnit4ClassRunner(org.junit.runners.BlockJUnit4ClassRunner).runChild(java.lang.Object, org.junit.runner.notification.RunNotifier) line: 50
org.junit.runners.ParentRunner$3.run() line: 238
org.junit.runners.ParentRunner$1.schedule(java.lang.Runnable) line: 63
org.springframework.test.context.junit4.SpringJUnit4ClassRunner(org.junit.runners.ParentRunner<T>).runChildren(org.junit.runner.notification.RunNotifier) line: 236
org.junit.runners.ParentRunner<T>.access$000(org.junit.runners.ParentRunner, org.junit.runner.notification.RunNotifier) line: 53
org.junit.runners.ParentRunner$2.evaluate() line: 229
看junit的运作方式
从main方法到runner
翻看官方guide 很容易发现JUnitCore
是main方法所在类
JUnitCore
是如何到runner的呢? 看下面分析
org.junit.runner.JUnitCore.main(String...)
// 一些listener构造之类,然后通过AllDefaultPossibilitiesBuilder 构建runner
org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(Class<?>)
List<RunnerBuilder> builders = Arrays.asList(
ignoredBuilder(),
annotatedBuilder(),
suiteMethodBuilder(),
junit3Builder(),
junit4Builder());
for (RunnerBuilder each : builders) {
Runner runner = each.safeRunnerForClass(testClass);
if (runner != null) {
return runner;
}
}
找到一个就直接返回
ignoredBuilder 可以用过Ignore
注解忽略你的test case
annotatedBuilder 是找 RunWith
注解定义的自定义runner
junit3Builder 如果test case 继承自TestCase类 则用junit3的runner
junit4Builder 直接对接 BlockJUnit4ClassRunner
spring的SpringJUnit4ClassRunner
也是继承自BlockJUnit4ClassRunner
回头去看上面的堆栈信息 一目了然
关键点在于重写 org.junit.runners.BlockJUnit4ClassRunner.createTest() 这个方法
protected Object createTest() throws Exception {
return getTestClass().getOnlyConstructor().newInstance();
}
/**
* Delegates to the parent implementation for creating the test instance and
* then allows the {@link #getTestContextManager() TestContextManager} to
* prepare the test instance before returning it.
*
* @see TestContextManager#prepareTestInstance(Object)
*/
@Override
protected Object createTest() throws Exception {
Object testInstance = super.createTest();
getTestContextManager().prepareTestInstance(testInstance);
return testInstance;
}
也就是把JUnit原生的创建test case的instance的过程接用spring容器装配的方式接管过来就可以了。
理一理Spring如何对接JUnit的更多相关文章
- spring框架和junit框架结合使用案例
package ltssh; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.be ...
- Spring Boot 解决方案 - JUnit 测试
简单的 JUnit 项目 回顾一下创建并运行简单的 JUnit 测试项目,先添加 JUnit 依赖然后编写类似如下模板的测试类,使用 IDE 的话直接用插件运行就行, 使用 Maven 的话运行命令 ...
- Spring框架下Junit测试
Spring框架下Junit测试 一.设置 1.1 目录 设置源码目录和测试目录,这样在设置产生测试方法时,会统一放到一个目录,如果没有设置测试目录,则不会产生测试代码. 1.2 增加配置文件 Res ...
- Spring中的Junit
Spring中的Junit package com.imooc.test.base; import org.junit.After; import org.junit.Before; import o ...
- spring入门-整合junit和web
整合Junit 导入jar包 基本 :4+1 测试:spring-test-5.1.3.RELEASE.jar 让Junit通知spring加载配置文件 让spring容器自动进行注入 1234567 ...
- Spring Boot 整合Junit和redis
14. Spring Boot整合-Junit 目标:在Spring Boot项目中使用Junit进行单元测试UserService的方法 分析: 添加启动器依赖spring-boot-starter ...
- [Java] Spring + SpringMVC + Maven + JUnit 搭建
示例项目下载: https://github.com/yangyxd/SpringDemo 利用前面 SpringMVC 项目的配置方式,完成初步的项目创建.下面只讲一些不同之处. 传送门: [Jav ...
- spring && Cobertura && maven &&junit 单元测试以及测试覆盖率
1. 目的: junit 单元测试,Cobertura 测试覆盖率报告 项目目录结构 2. maven 配置 <project xmlns= ...
- Spring MVC实现Junit Case
Spring MVC中编写单元测试(WEB项目): 1. 首先开发一个基类,用于载入配置文件.以下所有的测试实现类都要继承这个类 package com.yusj.basecase; import o ...
随机推荐
- AngularJS(三):重复HTML元素、数据绑定
本文也同步发表在我的公众号“我的天空” 重复HTML元素 在前端的页面编写中,我们会经常遇到重复HTML元素,譬如绘制表格.菜单等,如以下代码显示一个简单的li列表: <body> ...
- 系统有问题基本出在数据库上,web层无状态
系统有问题基本出在数据库上,web层无状态.
- UVA 674 Coin Change 硬币转换(完全背包,常规)
题意:有5种硬币,个数无限的,组成n元的不同方案有多少种? 思路:常规完全背包.重点在dp[0]=1,dp[j]中记录的是组成 j 元的方案数.状态转移方程dp[j+coin[i]]+=dp[j]. ...
- 2017.10.5 QBXT 模拟赛
题目链接 T1 从小到大排序,用sum记录前缀和,然后枚举1~n个数 ,如果当前的前缀和 + 1小于a[i]的话 那么 sum + 1永远不可能拼出来 直接输出sum + 1 ,否则统计前缀和.最后如 ...
- 洛谷 P2827 蚯蚓
题目描述 本题中,我们将用符号\lfloor c \rfloor⌊c⌋表示对c向下取整,例如:\lfloor 3.0 \rfloor= \lfloor 3.1 \rfloor=\lfloor 3.9 ...
- SAP标准培训课程C4C10学习笔记(二)第二单元
第二单元目录: SAP Cloud for Customer的项目实施分为4个阶段: 这四个阶段的详细介绍在SAP社区上这篇博文里: SAP Hybrid Project implementation ...
- 2002-2003 ACM-ICPC Northeastern European Regional Contest (NEERC 02) A Amusing Numbers (数学)
其实挺简单的.先直接算出之前已经排在k这个数前面的数字.比如543是三位的,那么100~543都是可以的,两位的10~54. 如果还需要往前面补的话,那么依次考虑1000~5430,5430是上界不能 ...
- Elastic Search Java Api 创建索引结构,添加索引
创建TCP客户端 Client client = new TransportClient() .addTransportAddress(new InetSocketTransportAddress( ...
- 字符串的驻留(String Interning)
http://www.cnblogs.com/artech/archive/2007/03/04/663728.html 关于字符串的驻留的机制,对于那些了解它的人肯定会认为很简单,但是我相信会有很大 ...
- 关于C与C++的区别
笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...