Spring測試框架springContextTest

    ssh版本:spring4.2/struts2.3/hibernate4;除了一些必要的jar包外,使用spring自帶的測試框架,還要額外導入spring-test-4.2.2.jar。java

junit使用的是junit4.7,相關測試代碼以下:spring

package TestContext;

import java.util.List;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import entity.User;
import service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:beans.xml")
@Transactional
public class ListActionTest extends AbstractTransactionalJUnit4SpringContextTests{
	
	
	private UserService userService;
	

	public UserService getUserService() {
		return userService;
	}


	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}


	//@Rollback(true)默認true 
	@Test
	public void test(){
		User user = userService.getUser(50);
		System.out.println(user.getUname());
		
		List<User> users = userService.getUsers();
		System.out.println(users.size());
	}
	
}

run as junit Test時報錯:框架

java.lang.ExceptionInInitializerError
     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
     at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
     at java.lang.reflect.Constructor.newInstance(Unknown Source)
     at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
     at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
     at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
     at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
     at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
     at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
     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)
Caused by: java.lang.IllegalStateException: Failed to find class [org.junit.runners.model.MultipleFailureException]: SpringJUnit4ClassRunner requires JUnit 4.9 or higher.
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<clinit>(SpringJUnit4ClassRunner.java:102)
     ... 17 more

報錯信息裏提到:eclipse

SpringJUnit4ClassRunner requires JUnit 4.9 or higher

後來換成junit-4.12.jar,運行報錯:ssh

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

原來 Since JUnit 4.11 the junit.jar itself has no Hamcrest classes,後來導入harmcrest-core-1.3.jar,繼續運行,ok!測試

相關文章
相關標籤/搜索