Spring测试框架springContextTest
Spring测试框架springContextTest
liuhuics10 发表于2年前
Spring测试框架springContextTest
  • 发表于 2年前
  • 阅读 437
  • 收藏 1
  • 点赞 0
  • 评论 0

    ssh版本:spring4.2/struts2.3/hibernate4;除了一些必要的jar包外,使用spring自带的测试框架,还要额外导入spring-test-4.2.2.jar。

junit使用的是junit4.7,相关测试代码如下:

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

报错信息里提到:

SpringJUnit4ClassRunner requires JUnit 4.9 or higher

后来换成junit-4.12.jar,运行报错:

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

原来 Since JUnit 4.11 the junit.jar itself has no Hamcrest classes,后来导入harmcrest-core-1.3.jar,继续运行,ok!

共有 人打赏支持
粉丝 5
博文 96
码字总数 39010
×
liuhuics10
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: