Search in sources :

Example 1 with ParameterizedConstructorInstantiator

use of org.mockito.internal.util.reflection.FieldInitializer.ParameterizedConstructorInstantiator in project mockito by mockito.

the class ParameterizedConstructorInstantiatorTest method should_fail_if_an_argument_instance_type_do_not_match_wanted_type.

@Test
public void should_fail_if_an_argument_instance_type_do_not_match_wanted_type() throws Exception {
    Observer observer = mock(Observer.class);
    Set<?> wrongArg = mock(Set.class);
    given(resolver.resolveTypeInstances(Matchers.<Class<?>[]>anyVararg())).willReturn(new Object[] { observer, wrongArg });
    try {
        new ParameterizedConstructorInstantiator(this, field("withMultipleConstructor"), resolver).instantiate();
        fail();
    } catch (MockitoException e) {
        assertThat(e.getMessage()).contains("argResolver").contains("incorrect types");
    }
}
Also used : ParameterizedConstructorInstantiator(org.mockito.internal.util.reflection.FieldInitializer.ParameterizedConstructorInstantiator) MockitoException(org.mockito.exceptions.base.MockitoException) Observer(java.util.Observer) Test(org.junit.Test)

Example 2 with ParameterizedConstructorInstantiator

use of org.mockito.internal.util.reflection.FieldInitializer.ParameterizedConstructorInstantiator in project mockito by mockito.

the class ParameterizedConstructorInstantiatorTest method should_report_failure_if_constructor_throws_exception.

@Test
public void should_report_failure_if_constructor_throws_exception() throws Exception {
    given(resolver.resolveTypeInstances(Matchers.<Class<?>[]>anyVararg())).willReturn(new Object[] { null });
    try {
        new ParameterizedConstructorInstantiator(this, field("withThrowingConstructor"), resolver).instantiate();
        fail();
    } catch (MockitoException e) {
        assertThat(e.getMessage()).contains("constructor").contains("raised an exception");
    }
}
Also used : ParameterizedConstructorInstantiator(org.mockito.internal.util.reflection.FieldInitializer.ParameterizedConstructorInstantiator) MockitoException(org.mockito.exceptions.base.MockitoException) Test(org.junit.Test)

Example 3 with ParameterizedConstructorInstantiator

use of org.mockito.internal.util.reflection.FieldInitializer.ParameterizedConstructorInstantiator in project mockito by mockito.

the class ParameterizedConstructorInstantiatorTest method should_instantiate_type_if_resolver_provide_matching_types.

@Test
public void should_instantiate_type_if_resolver_provide_matching_types() throws Exception {
    Observer observer = mock(Observer.class);
    Map map = mock(Map.class);
    given(resolver.resolveTypeInstances(Matchers.<Class<?>[]>anyVararg())).willReturn(new Object[] { observer, map });
    new ParameterizedConstructorInstantiator(this, field("withMultipleConstructor"), resolver).instantiate();
    assertNotNull(withMultipleConstructor);
    assertNotNull(withMultipleConstructor.observer);
    assertNotNull(withMultipleConstructor.map);
}
Also used : ParameterizedConstructorInstantiator(org.mockito.internal.util.reflection.FieldInitializer.ParameterizedConstructorInstantiator) Observer(java.util.Observer) Map(java.util.Map) Test(org.junit.Test)

Example 4 with ParameterizedConstructorInstantiator

use of org.mockito.internal.util.reflection.FieldInitializer.ParameterizedConstructorInstantiator in project mockito by mockito.

the class ParameterizedConstructorInstantiatorTest method should_fail_if_no_parameterized_constructor_found___excluding_inner_and_others_kind_of_types.

@Test
public void should_fail_if_no_parameterized_constructor_found___excluding_inner_and_others_kind_of_types() throws Exception {
    try {
        new ParameterizedConstructorInstantiator(this, field("withNoArgConstructor"), resolver).instantiate();
        fail();
    } catch (MockitoException me) {
        assertThat(me.getMessage()).contains("no parameterized constructor").contains("withNoArgConstructor").contains("NoArgConstructor");
    }
}
Also used : ParameterizedConstructorInstantiator(org.mockito.internal.util.reflection.FieldInitializer.ParameterizedConstructorInstantiator) MockitoException(org.mockito.exceptions.base.MockitoException) Test(org.junit.Test)

Example 5 with ParameterizedConstructorInstantiator

use of org.mockito.internal.util.reflection.FieldInitializer.ParameterizedConstructorInstantiator in project mockito by mockito.

the class ParameterizedConstructorInstantiatorTest method should_instantiate_type_with_vararg_constructor.

@Test
public void should_instantiate_type_with_vararg_constructor() throws Exception {
    Observer[] vararg = new Observer[] {};
    given(resolver.resolveTypeInstances(Matchers.<Class<?>[]>anyVararg())).willReturn(new Object[] { "", vararg });
    new ParameterizedConstructorInstantiator(this, field("withVarargConstructor"), resolver).instantiate();
    assertNotNull(withVarargConstructor);
}
Also used : ParameterizedConstructorInstantiator(org.mockito.internal.util.reflection.FieldInitializer.ParameterizedConstructorInstantiator) Observer(java.util.Observer) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 ParameterizedConstructorInstantiator (org.mockito.internal.util.reflection.FieldInitializer.ParameterizedConstructorInstantiator)5 Observer (java.util.Observer)3 MockitoException (org.mockito.exceptions.base.MockitoException)3 Map (java.util.Map)1