Improve ClassGeneratingEntityInstantiator error message if the to be instantiated type is abstract.

We now throw MappingInstantiationException with a nested BeanInstantiationException in alignment to ReflectionEntityInstantiator when attempting to create a new instance of an abstract class.

Closes #2348.
This commit is contained in:
Mark Paluch
2021-04-06 14:15:16 +02:00
parent d27ac79771
commit 8adfe47efa
2 changed files with 73 additions and 21 deletions

View File

@@ -39,6 +39,7 @@ import org.springframework.data.mapping.PreferredConstructor;
import org.springframework.data.mapping.PreferredConstructor.Parameter;
import org.springframework.data.mapping.model.ClassGeneratingEntityInstantiator.ObjectInstantiator;
import org.springframework.data.mapping.model.ClassGeneratingEntityInstantiatorUnitTests.Outer.Inner;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.util.ReflectionUtils;
/**
@@ -374,6 +375,13 @@ class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProperty<P>
assertThat(this.instance.shouldUseReflectionEntityInstantiator(entity)).isTrue();
}
@Test // GH-2348
void entityInstantiatorShouldFailForAbstractClass() {
assertThatExceptionOfType(MappingInstantiationException.class).isThrownBy(() -> this.instance
.createInstance(new BasicPersistentEntity<>(ClassTypeInformation.from(AbstractDto.class)), provider));
}
private void prepareMocks(Class<?> type) {
doReturn(type).when(entity).getType();
@@ -519,4 +527,8 @@ class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProperty<P>
ClassWithPackagePrivateConstructor() {}
}
public static abstract class AbstractDto {
}
}