views:

66

answers:

1

I would like to test transaction rollbacks in my application service. As a result i do not want to use spring's AbstractTransactionalJUnit4SpringContextTests with the @Transactional annotation as that wraps my test method in a transaction.

I know spring also offers AbstractJUnit4SpringContextTests for tests without transactions. However i need to have some transactions to save data into my database and also query data for assertions after running the service under test.

How can i write a Junit 4 spring transactional test without the default transaction test management?

+1  A: 

The spring docs should cover this in their testing chapter

What you might want is to configure your test without the default TransactionalTestExecutionListener like this

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,DirtiesContextTestExecutionListener.class})
public class SimpleTest {

    @Test
    public void testMethod() {
        // execute test logic...
   }
}
Paul
Paul, would i then use the EntityManager to create transactions programatically when i need to use them?
JavaRocky
I'm sorry, I didn't quite read your original question completely. So you actually need a Transaction in the test. In that case, creating it programatically would probably work, I think you need the JPATransactionManager for that. Then again, maybe you could do what you need to do declaratively by controlling the propagation behaviour. Maybe if you could provide an example test of what you need to do...
Paul