您的位置:首页 > 编程语言 > Java开发

Spring4.3.0 Junit4.11 initializationError(org.junit.runner.manipulation.Filter)

2016-07-05 09:59 519 查看

Spring4.3.0 Junit4.11 initializationError(org.junit.runner.manipulation.Filter)

昨天手欠,在项目中把Spring3.2.14版本升级到4.3.0版本,结果在使用junit进行单元测试时抛出如下错误,耗了一个多小时才搞定,在此记录一下,以防遗忘。

具体解决方案:升级Junit4.11版本到4.12版本解决,初步怀疑是底层实现方式不兼容导致,但还没深究代码实现,不作100%保证

package com.mhy.aop;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.aspectj.annotation.AspectJProxyFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.mhy.aop.aspectj.PreGreetingAspect;
import com.mhy.aop.aspectj.service.WaiterService;
import com.mhy.aop.aspectj.service.impl.WaiterServiceImpl;

/**
* 增强类测试
* @author mahaiyuan
* @date 2016年7月2日 下午11:52:36
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:aspectj-beans.xml")
public class AspectJTest {

@Autowired
private WaiterService waiterService;

@Test
public void test02(){
waiterService.greetTo("王五");
System.out.println("============================");
waiterService.serveTo("赵六");
}
}


执行测试用例时,在eclipse里显示执行失败



竟然没有错误提示?!拷贝了一下该异常信息,具体内容如下:

AspectJTest.test02
initializationError(org.junit.runner.manipulation.Filter)
java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=test02], {ExactMatcher:fDisplayName=test02(com.mhy.aop.AspectJTest)], {LeadingIdentifierMatcher:fClassName=com.mhy.aop.AspectJTest,fLeadingIdentifier=test02]] from org.junit.internal.requests.ClassRequest@579bb367
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:35)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createFilteredTest(JUnit4TestLoader.java:77)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:68)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


大致的意思是没有找到匹配的测试方法,但明明有,为什么会说没有匹配上呢?

从google和stackoverflow查找了一翻,大部分都在写换IDE、重写RunWith下的类等等,不一而足,但最终没能解决这个问题,个人也感觉不靠谱,没办法只能回到实际情况本身来反向推论了。

产生原因:升级Spring的版本之后单元测试不能使用了

那如果把版本降为3.2.14是否还能正常,测试了一下,把版本降为3.2.14之后测试用例能正常执行了,那是否为Spring4与Junut4.11版本的兼容性问题呢?比如底层实现方式发生了变化,然后把项目中使用的Junit版本升级到4.12。

升级之后再次执行该测试用例,然后内牛满面了,结果运行正常

How are you
WaiterServiceImpl.greetTo name=王五
============================
WaiterServiceImpl.serveTo name=赵六


根据当前的情况来讲,问题产生在Spring与Junit不同版本之间的兼容性方面,具体是哪一步实现不兼容导致在未看底层实现方面不能保证具体原因,待后面查看源码时再补充上该内容。^_^
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: