Spring2.5.X与Junit4.5配搭问题

新项目中,使用Maven来构建开发环境.Spring版本选择2.5.5,Junit版本选择4.5.


<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已经处于静止状态,不会在更新了.

[quote]
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
[/quote]

Henrik在jira给出的解决方案是下载junit-4.5.jar替换spring/lib/junit/junit-4.4.jar,然后让SpringJUnit4ClassRunner继承junit4.5的新类org.junit.runners.BlockJUnit4ClassRunner.

[quote]
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.
[/quote]

新建一个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应该不会再有问题了.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值