Relax TestEntityManager @ID restriction

Change TestEntityManager so that entities with an ID can be persisted.

Fixes gh-6546
pull/6859/merge
Phillip Webb 8 years ago
parent 25c4e261e9
commit 3e19f8aa8d

@ -90,8 +90,6 @@ public class TestEntityManager {
* @return the persisted entity
*/
public <E> E persist(E entity) {
Assert.state(getId(entity) == null,
"Entity " + entity.getClass().getName() + " already has an ID");
getEntityManager().persist(entity);
return entity;
}

@ -74,7 +74,7 @@ public class TestEntityManagerTests {
public void persistAndGetIdShouldPersistAndGetId() throws Exception {
bindEntityManager();
TestEntity entity = new TestEntity();
given(this.persistenceUnitUtil.getIdentifier(entity)).willReturn(null, 123);
given(this.persistenceUnitUtil.getIdentifier(entity)).willReturn(123);
Object result = this.testEntityManager.persistAndGetId(entity);
verify(this.entityManager).persist(entity);
assertThat(result).isEqualTo(123);
@ -84,7 +84,7 @@ public class TestEntityManagerTests {
public void persistAndGetIdForTypeShouldPersistAndGetId() throws Exception {
bindEntityManager();
TestEntity entity = new TestEntity();
given(this.persistenceUnitUtil.getIdentifier(entity)).willReturn(null, 123);
given(this.persistenceUnitUtil.getIdentifier(entity)).willReturn(123);
Integer result = this.testEntityManager.persistAndGetId(entity, Integer.class);
verify(this.entityManager).persist(entity);
assertThat(result).isEqualTo(123);
@ -99,17 +99,6 @@ public class TestEntityManagerTests {
assertThat(result).isSameAs(entity);
}
@Test
public void persistWhenAlreadyHasIdShouldThrowException() throws Exception {
bindEntityManager();
TestEntity entity = new TestEntity();
given(this.persistenceUnitUtil.getIdentifier(entity)).willReturn(123);
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage(
"Entity " + TestEntity.class.getName() + " already has an ID");
this.testEntityManager.persistAndGetId(entity, Integer.class);
}
@Test
public void persistAndFlushShouldPersistAndFlush() throws Exception {
bindEntityManager();
@ -125,7 +114,7 @@ public class TestEntityManagerTests {
bindEntityManager();
TestEntity entity = new TestEntity();
TestEntity found = new TestEntity();
given(this.persistenceUnitUtil.getIdentifier(entity)).willReturn(null, 123);
given(this.persistenceUnitUtil.getIdentifier(entity)).willReturn(123);
given(this.entityManager.find(TestEntity.class, 123)).willReturn(found);
TestEntity result = this.testEntityManager.persistFlushFind(entity);
verify(this.entityManager).persist(entity);

Loading…
Cancel
Save