Hi Everyone!
I created a simple application for using EntityManager with spring transaction. I have a user table where usernames are unique. When I try to create a new user with existing username it throws an exception.
I setted up transaction testing with following scenarios:
-When I create a new non existing user I can query it during the test, and at the end of the test it will rolled back automatically.
-When I create a new existing user I got an expected exception.
-The problem: When I create a new non existing user it works fine. But when I create it second time I did not get exception. Why?
The test case:
And the JUnit output:Code:@Test @ExpectedException(DataAccessException.class) public void testCreateUsers(){ UserEntity entity = new UserEntity(); entity.setPassword("password"); entity.setUsername("username"); entity.setRole(RoleList.ROLE_SUPERVISOR); userService.createUser(entity); UserEntity result = userService.loadUserByUsername("username"); assertEquals("username", result.getUsername()); UserEntity entity2 = new UserEntity(); entity2.setPassword("password"); entity2.setUsername("username"); entity2.setRole(RoleList.ROLE_SUPERVISOR); userService.createUser(entity2); }
java.lang.AssertionError: Expected exception: org.springframework.dao.DataAccessException


Reply With Quote
