Search in sources :

Example 1 with MappingInstantiationException

use of org.springframework.data.mapping.model.MappingInstantiationException in project spring-data-commons by spring-projects.

the class ClassGeneratingEntityInstantiatorUnitTests method capturesContextOnInstantiationException.

// DATACMNS-283, DATACMNS-578
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void capturesContextOnInstantiationException() throws Exception {
    PersistentEntity<Sample, P> entity = new BasicPersistentEntity<>(from(Sample.class));
    doReturn("FOO").when(provider).getParameterValue(any(Parameter.class));
    Constructor constructor = Sample.class.getConstructor(Long.class, String.class);
    List<Object> parameters = Arrays.asList("FOO", "FOO");
    try {
        this.instance.createInstance(entity, provider);
        fail("Expected MappingInstantiationException!");
    } catch (MappingInstantiationException o_O) {
        assertThat(o_O.getConstructor()).hasValue(constructor);
        assertThat(o_O.getConstructorArguments()).isEqualTo(parameters);
        assertThat(o_O.getEntityType()).hasValue(Sample.class);
        assertThat(o_O.getMessage()).contains(Sample.class.getName());
        assertThat(o_O.getMessage()).contains(Long.class.getName());
        assertThat(o_O.getMessage()).contains(String.class.getName());
        assertThat(o_O.getMessage()).contains("FOO");
    }
}
Also used : MappingInstantiationException(org.springframework.data.mapping.model.MappingInstantiationException) Constructor(java.lang.reflect.Constructor) PreferredConstructor(org.springframework.data.mapping.PreferredConstructor) BasicPersistentEntity(org.springframework.data.mapping.model.BasicPersistentEntity) Parameter(org.springframework.data.mapping.PreferredConstructor.Parameter) Test(org.junit.Test)

Example 2 with MappingInstantiationException

use of org.springframework.data.mapping.model.MappingInstantiationException in project spring-data-commons by spring-projects.

the class ReflectionEntityInstantiatorUnitTests method capturesContextOnInstantiationException.

// DATACMNS-283
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void capturesContextOnInstantiationException() throws Exception {
    PersistentEntity<Sample, P> entity = new BasicPersistentEntity<>(from(Sample.class));
    doReturn("FOO").when(provider).getParameterValue(any(Parameter.class));
    Constructor constructor = Sample.class.getConstructor(Long.class, String.class);
    List<Object> parameters = Arrays.asList("FOO", "FOO");
    try {
        INSTANCE.createInstance(entity, provider);
        fail("Expected MappingInstantiationException!");
    } catch (MappingInstantiationException o_O) {
        assertThat(o_O.getConstructor()).hasValue(constructor);
        assertThat(o_O.getConstructorArguments()).isEqualTo(parameters);
        assertThat(o_O.getEntityType()).hasValue(Sample.class);
        assertThat(o_O.getMessage()).contains(Sample.class.getName());
        assertThat(o_O.getMessage()).contains(Long.class.getName());
        assertThat(o_O.getMessage()).contains(String.class.getName());
        assertThat(o_O.getMessage()).contains("FOO");
    }
}
Also used : MappingInstantiationException(org.springframework.data.mapping.model.MappingInstantiationException) Constructor(java.lang.reflect.Constructor) PreferredConstructor(org.springframework.data.mapping.PreferredConstructor) BasicPersistentEntity(org.springframework.data.mapping.model.BasicPersistentEntity) Parameter(org.springframework.data.mapping.PreferredConstructor.Parameter) Test(org.junit.Test)

Aggregations

Constructor (java.lang.reflect.Constructor)2 Test (org.junit.Test)2 PreferredConstructor (org.springframework.data.mapping.PreferredConstructor)2 Parameter (org.springframework.data.mapping.PreferredConstructor.Parameter)2 BasicPersistentEntity (org.springframework.data.mapping.model.BasicPersistentEntity)2 MappingInstantiationException (org.springframework.data.mapping.model.MappingInstantiationException)2