浏览 7508 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-11-06
<properties> <spring.version>2.5.5</spring.version> <junit.version>4.5</junit.version> </properties> 在测试service类时报错. @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"/applicationContext-test.xml"}) public final class UserManagerTests{ @Test(expected=EmailAlreadyExistException.class) public void testEmailAlreadyExistException() throws EmailAlreadyExistException{ userManager.register(user3); fail(); } } 错误为 org/junit/Assume$AssumptionViolatedException; nested exception is java.lang.NoClassDefFoundError: org/junit/Assume$AssumptionViolatedException 百思不得其解,然后就翻Google,后来Spring的jira中翻到,原来Spring 2.5 branch中是使用得junit 4.4.同时Spring 2.5 branch已经处于静止状态,不会在更新了. 引用 I'm afraid we can't move to JUnit 4.5+ in the Spring 2.5.x branch. However, in the upcoming Spring 3.0 branch, we'll build on JUnit 4.5. Well, the 2.5 branch entered strict maintenance mode last week. While we are in principle still able to support new versions of third-party dependencies, we can't raise the minimum level unless the previously supported version was pre-release software. Unfortunately it seems that supporting JUnit 4.5 here means requiring JUnit 4.5+; we can't do that before Spring 3.0 since all sorts of present build and development environments have JUnit 4.4 baked into them. It's unpleasant but not our fault - there was no better way of doing the test context framework against the hard-to-extend JUnit 4.4. Juergen Henrik在jira给出的解决方案是下载junit-4.5.jar替换spring/lib/junit/junit-4.4.jar,然后让SpringJUnit4ClassRunner继承junit4.5的新类org.junit.runners.BlockJUnit4ClassRunner. 引用 On top of the patch - make sure you also replace the old spring/lib/junit/junit-4.4.jar with the new junit-4.5.jar, which can be downloaded here: http://sourceforge.net/project/showfiles.php?group_id=15278&package_id=12472 The patch offers a solution where SpringJUnit4ClassRunner extends the new JUnit4.5 class "BlockJUnit4Runner", which offers much better possibilities for extension and reuse than the old class JUnit4ClassRunner, which have been deprecated. This also means that backward-compability to JUnit-4.4 is broken but I think it is worth it. Its default class-runner JUnit4ClassRunner was not really supposed to be extended the way SpringJUnit4ClassRunner did it. (Not that there was any good alternative but still ...) Also, by switching to JUnit-4.5 the classes SpringTestMethod and SpringMethodRoadie can be removed. 新建一个BlockJUnit4ClassRunner的子类,取名叫作SpringJUnit45ClassRunner,代码如下: import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.runners.model.InitializationError; import org.junit.runners.BlockJUnit4ClassRunner; import org.springframework.test.context.TestContextManager; public class SpringJUnit45ClassRunner extends BlockJUnit4ClassRunner { private static final Log logger = LogFactory.getLog(SpringJUnit45ClassRunner.class); private final TestContextManager testContextManager; public SpringJUnit45ClassRunner(Class<?> clazz) throws InitializationError { super(clazz); if (logger.isDebugEnabled()) { logger.debug("SpringJUnit45ClassRunner constructor called with [" + clazz + "]."); } this.testContextManager = createTestContextManager(clazz); } protected Object createTest() throws Exception { Object testInstance = super.createTest(); getTestContextManager().prepareTestInstance(testInstance); return testInstance; } protected TestContextManager createTestContextManager(Class<?> clazz) { return new TestContextManager(clazz); } protected final TestContextManager getTestContextManager() { return this.testContextManager; } } 记得要把pom.xml里junit和spring-test的<scope>test</scope>干掉. 然后修改测试类的RunWith为新类. @RunWith(SpringJUnit45ClassRunner.class) 测试通过了.这样Spring2.5.X与Junit4.5应该不会再有问题了. 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-03-01
正好遇到这个问题,非常感谢!!!
|
|
返回顶楼 | |
发表时间:2009-03-28
我们也是遇到了同样的问题,把版本改加junit4.4了,
顺便问题一下,你的applicationContext-test.xml文件是怎么引用其他spring配置文件的? 我们的配置文件在WEB-INF/Spring/下面,有点麻烦 |
|
返回顶楼 | |
发表时间:2009-06-09
还是有一个问题请教楼主: 我使用了你给出的Runner, 但事务不会回滚! 使用默认的Runner虽然有这个异常, 但我看到事务回滚.
TransactionalTestExecutionListener - Rolled back transaction after test execution for test context |
|
返回顶楼 | |
发表时间:2009-06-10
注意spring2.5.6的,需要junit 升级到4.6 !!
|
|
返回顶楼 | |
发表时间:2009-07-01
最后修改:2009-07-01
kjj 写道 注意spring2.5.6的,需要junit 升级到4.6 !!
仍然会有问题,看下面这个简单的测试用例: @Test public void fun(){ int a = 10/0; System.out.println(a); } 在控制台会报这个错误,java.lang.NoClassDefFoundError: org/junit/Assume$AssumptionViolatedException.但是这个错误明显不是我们想看到的.按楼主的方式修改,也有个问题,就是对数据库没法回滚,即使设置了@Rollback也没用,没有看junit4.4~junit4.6的change log,不清楚这三个版本间升级了什么内容,不过对我们来说,降级使用Junit4.4是最好的选择了,毕竟spring的2.5系列已经不再更新了. |
|
返回顶楼 | |