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

引用

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应该不会再有问题了.

你可能感兴趣的:(apache,spring,maven,xml,JUnit)