我正在尝试使用@Test(expected = RuntimeException.class)注释 为了测试预期的异常。我的代码如下:
@Test(expected = RuntimeException.class)
public void testSaveThrowsRuntimeException(){
User user = domain.save(null);
}
我的保存方法很简单:
public User save(User newUser) {
if(newUser == null) {
throw new RuntimeException();
}
//saving code goes here
}
在调试代码之后,我发现代码抛出了预期的异常,但它在spring框架类之间的某处被吃掉了。
我尝试使用旧方式(尝试catch块)但仍然无法在测试中捕获该异常并且测试在Junit的runafter方法中不断抛出错误:
org.springframework.transaction.UnexpectedRollbackException: JTA transaction unexpectedly rolled back (maybe due to a timeout); nested exception is javax.transaction.RollbackException
at org.springframework.transaction.jta.JtaTransactionManager.doCommit(JtaTransactionManager.java:1031)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:709)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:678)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener$TransactionContext.endTransaction(TransactionalTestExecutionListener.java:504)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.endTransaction(TransactionalTestExecutionListener.java:277)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.afterTestMethod(TransactionalTestExecutionListener.java:170)
at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:344)
at org.springframework.test.context.junit4.SpringMethodRoadie.runAfters(SpringMethodRoadie.java:307)
at org.springframework.test.context.junit4.SpringMethodRoadie$RunBeforesThenTestThenAfters.run(SpringMethodRoadie.java:338)
at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(SpringMethodRoadie.java:217)
at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(SpringMethodRoadie.java:197)
at org.springframework.test.context.junit4.SpringMethodRoadie.run(SpringMethodRoadie.java:143)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:142)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: javax.transaction.RollbackException
at org.objectweb.jotm.TransactionImpl.commit(TransactionImpl.java:245)
at org.objectweb.jotm.Current.commit(Current.java:488)
at org.springframework.transaction.jta.JtaTransactionManager.doCommit(JtaTransactionManager.java:1028)
... 23 more
我确信这是因为RuntimeException我投入了save但是无法捕获它或者通过expect子句传递测试。
任何人都知道什么是错的?
答案 0 :(得分:6)
原来我的第一个答案是错的。 @Test(expected = ...)和@ExpectedException都有效,但the Spring TestContext and Junit 4.5之间存在一些不兼容的问题。使用Junit 4.4解决了我的问题。最后。
答案 1 :(得分:5)
这是我在Junit 4.5中找到的解决方法 - 将@Transactional和@ExpectedException分离为嵌套函数。 我猜这个问题与aop的东西有关,Spring围绕一个@Transactional方法。
@Test
@ExpectedException(org.springframework.dao.DataIntegrityViolationException.class)
public void Test10UniqueName()
{
DoTest10UniqueName();
}
@Transactional
public void DoTest10UniqueName()
{
final String NAME = "NAME";
ProductCategoryDAO dao = DAOFactory.getProductCategoryDAO();
ProductCategory test1 = new ProductCategory();
test1.setName(NAME);
ProductCategory test2 = new ProductCategory();
test2.setName(NAME);
dao.save(test1);
dao.save(test2);
}
答案 2 :(得分:1)
要么你正在运行一个单元测试,在这种情况下Spring TX不应该进入,或者你正在运行某种集成测试,你要测试运行时异常时save方法的作用< em>被吞下。我认为什么都不会出错,你只需要确保你明白你想要测试的是什么。