Here are a few questions about transactions and JUnit. But please read this before discarding it, as I cannot find anyone with the same problems.
I have a buisness method annotated by @Transactional . As part of this method, I will perform a software rollback if some special condition occurs. TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
Now let's not discuss whether software rollbacks are good or bad. Let's just take it there and take it to stay there and work with it.
If I run my application and test this buisness method in the old fashioned way, then everything works fine. When the material needs to be rolled back, it rolls back, and when everything is in order, then everything is in order. And I also ran a test without @Transactional to make sure nothing rolls back, even if necessary. Everything works as planned.
But the problems I'm having are related to JUnit. I currently have 2 JUnit tests of this method. 1, which should fail (and cause a software rollback), and one that will succeed without a rollback.
I tried many different settings of my Junit class. Currently it looks like this:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:springTestContext.xml", "classpath:springTestContext-dao.xml"}) @TransactionConfiguration(transactionManager = "txManager") public class MyManagerTest extends AbstractTransactionalJUnit4SpringContextTests { @Mock private ProductDao productDao; @InjectMocks MyManager myManager = new MyManagerImpl(); @Before public void setup() { MockitoAnnotations.initMocks(this); } @Test public void testUnParsableXml() { String xml = "adlsfas"; Response response = myManager.processXMLContent(xml); assertFalse(response.isSuccess()); System.out.println(response.getResponse()); } } @Service("myManager") public class MyManagerImpl extends BaseManager implements MyManager { @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW) public Response processXMLContent(String xml) { Response response = new Response(); try { parseXml();
SpringTestContext has an annotation <tx:annotation-driven , and dao-context has an operatormanager, entityfactory, and data source. Probably not even need these? Since this test has absolutely nothing to do with db. All I want to check is that a transaction rolls back if it fails.
But the reason why I added them was because of the error with which I am trying to get help here. Whenever a program rollback is called in the buisness method, I always get this error (only for junit tests, it works fine otherwise):
org.springframework.transaction.NoTransactionException: No transaction aspect-managed TransactionStatus in scope
So, my question to you is: what am I doing wrong. How can I get my buisness method for a transaction? And then as a bonus question, how can I verify that a transaction rollback was triggered?
Thanks for your time and help!