Skip to content

Commit 5969089

Browse files
Johannes Mockenhauptodrotbohm
authored andcommitted
DATACMNS-300 - Fixed exception handling in ReflectionEntityInstantiator.
ReflectionEntityInstantiator now actually throws the wrapped exception created in the catch block handling BeanInstantiationExceptions. Pull request: #25.
1 parent 9781f60 commit 5969089

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/main/java/org/springframework/data/convert/ReflectionEntityInstantiator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public <T, E extends PersistentEntity<? extends T, P>, P extends PersistentPrope
6161
return BeanUtils.instantiateClass(entity.getType());
6262
}
6363
} catch (BeanInstantiationException e) {
64-
new MappingInstantiationException(entity, Collections.emptyList(), e);
64+
throw new MappingInstantiationException(entity, Collections.emptyList(), e);
6565
}
6666
}
6767

src/test/java/org/springframework/data/convert/ReflectionEntityInstantiatorUnitTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
* Unit tests for {@link ReflectionEntityInstantiator}.
4848
*
4949
* @author Oliver Gierke
50+
* @author Johannes Mockenhaupt
5051
*/
5152
@RunWith(MockitoJUnitRunner.class)
5253
public class ReflectionEntityInstantiatorUnitTests<P extends PersistentProperty<P>> {
@@ -91,6 +92,19 @@ public void instantiatesTypeWithPreferredConstructorUsingParameterValueProvider(
9192
verify(provider, times(1)).getParameterValue((Parameter) constructor.getParameters().iterator().next());
9293
}
9394

95+
/**
96+
* @see DATACMNS-300
97+
*/
98+
@Test(expected = MappingInstantiationException.class)
99+
@SuppressWarnings({ "unchecked", "rawtypes" })
100+
public void throwsExceptionOnBeanInstantiationException() {
101+
102+
when(entity.getPersistenceConstructor()).thenReturn(null);
103+
when(entity.getType()).thenReturn((Class) PersistentEntity.class);
104+
105+
INSTANCE.createInstance(entity, provider);
106+
}
107+
94108
/**
95109
* @see DATACMNS-134
96110
*/
@@ -134,8 +148,10 @@ public void capturesContextOnInstantiationException() throws Exception {
134148
List<Object> parameters = Arrays.asList((Object) "FOO", (Object) "FOO");
135149

136150
try {
151+
137152
INSTANCE.createInstance(entity, provider);
138153
fail("Expected MappingInstantiationException!");
154+
139155
} catch (MappingInstantiationException o_O) {
140156

141157
assertThat(o_O.getConstructor(), is(constructor));
@@ -146,7 +162,6 @@ public void capturesContextOnInstantiationException() throws Exception {
146162
assertThat(o_O.getMessage(), containsString(Long.class.getName()));
147163
assertThat(o_O.getMessage(), containsString(String.class.getName()));
148164
assertThat(o_O.getMessage(), containsString("FOO"));
149-
System.out.println(o_O.getMessage());
150165
}
151166
}
152167

0 commit comments

Comments
 (0)