From 3e19f8aa8d0ffa4531cc8b9ec7383f198745302a Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 15 Sep 2016 17:22:52 -0700 Subject: [PATCH] Relax TestEntityManager @ID restriction Change TestEntityManager so that entities with an ID can be persisted. Fixes gh-6546 --- .../orm/jpa/TestEntityManager.java | 2 -- .../orm/jpa/TestEntityManagerTests.java | 17 +++-------------- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestEntityManager.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestEntityManager.java index 2dce7952a9e..e3fb358b016 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestEntityManager.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestEntityManager.java @@ -90,8 +90,6 @@ public class TestEntityManager { * @return the persisted entity */ public E persist(E entity) { - Assert.state(getId(entity) == null, - "Entity " + entity.getClass().getName() + " already has an ID"); getEntityManager().persist(entity); return entity; } diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestEntityManagerTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestEntityManagerTests.java index 760b8ef5191..727fbe8cbd8 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestEntityManagerTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestEntityManagerTests.java @@ -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);