package org.musicrecital.service; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.musicrecital.model.User; import org.junit.Test; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests; import static org.junit.Assert.assertNotNull; @ContextConfiguration( locations = {"/applicationContext-service.xml", "/applicationContext-resources.xml", "classpath:/applicationContext-dao.xml"}) public class UserExistsExceptionTest extends AbstractTransactionalJUnit4SpringContextTests { @Autowired private UserManager manager; private Log log = LogFactory.getLog(UserExistsExceptionTest.class); @Test(expected = UserExistsException.class) public void testAddExistingUser() throws Exception { log.debug("entered 'testAddExistingUser' method"); assertNotNull(manager); User user = manager.getUser("-1"); // create new object with null id - Hibernate doesn't like setId(null) User user2 = new User(); BeanUtils.copyProperties(user, user2); user2.setId(null); user2.setVersion(null); user2.setRoles(null); // try saving as new user, this should fail UserExistsException b/c of unique keys manager.saveUser(user2); } }