hibernate - JPA DAO integration test not throwing exception when duplicate object saved? -


I'm in the process of Hibernate test as the unit is built with a DAO Spring / JPA provider. Before running the test

, DBUnet put a user record with the username "PowerUser" - the user key is in the user table. Here integration testing method:

  @Test @ExpectedException (EntityExistsException.class) public void save_UserTestDataSaveUserWithPreExistingId_EntityExistsException () {user NEWUSER = new UserImpl ( "power"); NewUser.setEmail ("kuser@null.com"); NewUser.setFirstName ("new"); NewUser.setLastName ("user"); NewUser.setPassword ("secret"); Dao.persist (NEWUSER); }  

I do not normally verify that recorded at the beginning of this method in the database if it's relevant, but if I have a dao.flush () function:

  javax.persistence. PersistenceException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update  

I assume that you are using the Transactional Integration Test? These tests each test method rollback at the end, you're really missing flush.

I do not know if it is useful, but if you were autogenerating the ID, then what would happen in a flush (existence) because the database requires a trip to get the generated ID but if you If you are not specifying your ID, then there is a delay in the flush as long as you are not committed.


Comments