Hello,

I'm trying to use the @Test(expected = RuntimeException.class) annotation in order to test for an expected exception. My code is as follows:


@Test(expected = RuntimeException.class)
public void testSaveThrowsRuntimeException(){

User user = domain.save(null);

}

and my save method simple like this :


public User save(User newUser) {
if(newUser == null) {
throw new RuntimeException();
}
//saving code goes here
}

after debugging the code I found that code throwing the exception as expected but its getting eaten somewhere in between in spring framework classes.

I tried the same with old way (try catch block) but still I am not able to catch that exception in test and test keeps throwing errors in runafter method of Junit :

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

And I am sure this is because of that RuntimeException I am throwing in save but not able catch it or pass the test with expected clause.

anybody have any idea whats going wrong?

Thanks in advance,

Posted at at 5:54 AM on Thursday, July 23, 2009 by Posted by Ravindra Nikam | 0 comments   | Filed under:

Love & Marriage

A student asks a teacher, "What is love?"
The teacher said, "in order to answer your question, go to the wheat field and choose the biggest wheat and come back.
But the rule is: you can go through them only once and cannot turn back to pick."
The student went to the field, go thru first row, he saw one big wheat, but he wonders....may be there is a bigger one later.
Then he saw another bigger one... but may be there is an even bigger one waiting for him.
Later, when he finished more than half of the wheat field, he start to realize that the wheat is not as big as the previous one he saw, he know he has missed the biggest one, and he regretted.
So, he ended up went back to the teacher with empty hand.
The teacher told him, "...this is love... you keep looking for a better one, but when later you realise, you have already miss the person....
What is marriage then?"
the student asked.The teacher said, "in order to answer your question, go to the corn field and choose the biggest corn and come back.But the rule is: you can go through them only once and cannot turn back to pick.The student went to the corn field, this time he is careful not to repeat the previous mistake, when he reach the middle of the field, he has picked one medium corn that he feel satisfy, and come back to the teacher.
The teacher told him, "this time you bring back a corn.... you look for one that is just nice, and you have faith and believe this is the best one you get.... this is marriage.

Posted at at 2:15 AM on Wednesday, April 22, 2009 by Posted by Ravindra Nikam | 0 comments   | Filed under: