2

I have the following test where I need to show that my transaction works and can do rollback. The problem here is my RuntimeException is executed before the data is entered into the database. So rollback doesn't happen. How do I set up this test?

@Test @Rollback(true) public void testForTransactionRollback() throws ClassNotFoundException, SQLException { User user = new User(); user.setFirstName("Ivan"); user.setLastName("Ivanov"); user.setPhoneNumber("18000000"); user.setEmail("[email protected]"); user.setCardNumber("4111111111111111"); user.setDeliveryAddress("address"); user.setComment("comment"); String result = ""; final ClientDAO clientDao1 = Mockito.spy(new ClientDAOImpl()); final ClientDAOImpl clientDaoImpl = (ClientDAOImpl) clientDao1; Mockito.when(clientDao1.insert(user)).thenThrow(new RuntimeException()); try { clientDao1.insert(user); } catch (final RuntimeException e) { System.out.println("Expected failure to trigger transaction caught"); } finally { user = clientDao1.get(1L); if (user != null) { result = user.getFirstName(); } } Assert.assertEquals("it should be equal", "", result); } 
1
  • 1
    Why is your test case annotated with Rollback(true)? I'm assuming you want to test an actual method in your production code, otherwise this test will actually prove nothing other than your test being able to perform a rollback. You should call your method responsible for handling the transaction, ensure that it fails and then assert that nothing was updated in the database. Commented Aug 4, 2020 at 17:08

1 Answer 1

1

I think the problem you're facing is caused by two inner transactions (one inside the other): first one is inside clientDao1.insert(user) and a second one inside your test (providing that you annotated your test class with @Transactional or @DataJpaTest, but you've not shown that). If RuntimeException is thrown within inner transaction, which is the case because of

Mockito.when(clientDao1.insert(user)).thenThrow(new RuntimeException()); 

then it means your inner clientDao1.insert method cannot recover from that exception and the outer method (test one) has nothing to say.

That's why data will NEVER be written to the database or even persisted (hibernate caching, of course if you use hibernate as your JPA provider).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.