Search in sources :

Example 1 with MethodCall

use of org.powermock.core.transformers.mock.MockGatewaySpy.MethodCall in project powermock by powermock.

the class ConstructorCallMockTransformerTest method should_provide_correct_constructor_param_and_arguments.

@Test
public void should_provide_correct_constructor_param_and_arguments() throws Exception {
    assumeClassLoaderMode();
    MockGatewaySpy.returnOnMethodCall(SUPPRESS);
    Class<?> clazz = loadWithMockClassLoader(SuperClassCallSuperConstructor.class.getName());
    final Constructor<?> constructor = clazz.getConstructor(String.class, String.class, double.class);
    constructor.newInstance("name", "field", 100);
    assertThatCorrectConstructorTypeProvided();
    final MethodCall methodCall = MockGatewaySpy.constructorCalls().get(0);
    assertThat(methodCall.args).as("Correct constructor arguments are provided").containsExactly("name", 100.0);
    assertThat(methodCall.sig).as("Correct constructor signature is provided").containsExactly(String.class, double.class);
}
Also used : SuperClassCallSuperConstructor(powermock.test.support.MainMockTransformerTestSupport.SuperClassCallSuperConstructor) MethodCall(org.powermock.core.transformers.mock.MockGatewaySpy.MethodCall) Test(org.junit.Test)

Example 2 with MethodCall

use of org.powermock.core.transformers.mock.MockGatewaySpy.MethodCall in project powermock by powermock.

the class ConstructorCallMockTransformerTest method should_provide_correct_constructor_param_and_arguments_when_cast_required.

@Test
public void should_provide_correct_constructor_param_and_arguments_when_cast_required() throws Exception {
    assumeClassLoaderMode();
    MockGatewaySpy.returnOnMethodCall(SUPPRESS);
    final Class<?> clazz = loadWithMockClassLoader(SuperClassCallSuperConstructorWithCast.class.getName());
    final Class<?> paramClass = loadWithMockClassLoader(ParameterInterface.class.getName());
    final Object param = loadWithMockClassLoader(ParameterImpl.class.getName()).newInstance();
    final Constructor<?> constructor = clazz.getConstructor(paramClass);
    constructor.newInstance(param);
    assertThatCorrectConstructorTypeProvided();
    final MethodCall methodCall = MockGatewaySpy.constructorCalls().get(0);
    assertThat(methodCall.args).as("Correct constructor arguments are provided").containsExactly(param);
    assertThat(methodCall.sig).as("Correct constructor signature is provided").hasSize(1).extracting(new Extractor<Class<?>, Object>() {

        @Override
        public Object extract(final Class<?> input) {
            return input.getName();
        }
    }).containsExactly(ParameterImpl.class.getName());
}
Also used : ParameterInterface(powermock.test.support.MainMockTransformerTestSupport.ParameterInterface) ParameterImpl(powermock.test.support.MainMockTransformerTestSupport.ParameterImpl) NestedTestClass(powermock.test.support.MainMockTransformerTestSupport.ParentTestClass.NestedTestClass) PublicSuperClass(powermock.test.support.MainMockTransformerTestSupport.SupportClasses.PublicSuperClass) SubClass(powermock.test.support.MainMockTransformerTestSupport.SupportClasses.SubClass) ParentTestClass(powermock.test.support.MainMockTransformerTestSupport.ParentTestClass) IndicateReloadClass(org.powermock.core.IndicateReloadClass) EnumClass(powermock.test.support.MainMockTransformerTestSupport.SupportClasses.EnumClass) Extractor(org.assertj.core.api.iterable.Extractor) MethodCall(org.powermock.core.transformers.mock.MockGatewaySpy.MethodCall) SuperClassCallSuperConstructorWithCast(powermock.test.support.MainMockTransformerTestSupport.SuperClassCallSuperConstructorWithCast) Test(org.junit.Test)

Example 3 with MethodCall

use of org.powermock.core.transformers.mock.MockGatewaySpy.MethodCall in project powermock by powermock.

the class ConstructorCallMockTransformerTest method assertThatCorrectConstructorTypeProvided.

private void assertThatCorrectConstructorTypeProvided() {
    final MethodCall methodCall = MockGatewaySpy.constructorCalls().get(0);
    assertThat(methodCall.type.getName()).as("Correct constructor type is provided").isEqualTo(SupperClassThrowsException.class.getName());
}
Also used : SupperClassThrowsException(powermock.test.support.MainMockTransformerTestSupport.ConstructorCall.SupperClassThrowsException) MethodCall(org.powermock.core.transformers.mock.MockGatewaySpy.MethodCall)

Example 4 with MethodCall

use of org.powermock.core.transformers.mock.MockGatewaySpy.MethodCall in project powermock by powermock.

the class ConstructorCallMockTransformerTest method should_provide_correct_constructor_param_and_arguments_when_parameters_vararg.

@Test
public void should_provide_correct_constructor_param_and_arguments_when_parameters_vararg() throws Exception {
    assumeClassLoaderMode();
    MockGatewaySpy.returnOnMethodCall(SUPPRESS);
    final Class<?> clazz = loadWithMockClassLoader(SuperClassCallSuperConstructorWithVararg.class.getName());
    final Class<?> paramClass = long[].class;
    final Constructor<?> constructor = clazz.getConstructor(paramClass);
    long[] params = { 1, 5, 6 };
    constructor.newInstance(new Object[] { params });
    assertThatCorrectConstructorTypeProvided();
    final MethodCall methodCall = MockGatewaySpy.constructorCalls().get(0);
    assertThat(methodCall.args).as("Constructor arguments have correct size").hasSize(1);
    assertThat((long[]) methodCall.args[0]).as("Correct constructor arguments are provided").containsExactly(params);
    assertThat(methodCall.sig).as("Correct constructor signature is provided").hasSize(1).containsExactly(long[].class);
}
Also used : SuperClassCallSuperConstructorWithVararg(powermock.test.support.MainMockTransformerTestSupport.SuperClassCallSuperConstructorWithVararg) MethodCall(org.powermock.core.transformers.mock.MockGatewaySpy.MethodCall) Test(org.junit.Test)

Aggregations

MethodCall (org.powermock.core.transformers.mock.MockGatewaySpy.MethodCall)4 Test (org.junit.Test)3 Extractor (org.assertj.core.api.iterable.Extractor)1 IndicateReloadClass (org.powermock.core.IndicateReloadClass)1 SupperClassThrowsException (powermock.test.support.MainMockTransformerTestSupport.ConstructorCall.SupperClassThrowsException)1 ParameterImpl (powermock.test.support.MainMockTransformerTestSupport.ParameterImpl)1 ParameterInterface (powermock.test.support.MainMockTransformerTestSupport.ParameterInterface)1 ParentTestClass (powermock.test.support.MainMockTransformerTestSupport.ParentTestClass)1 NestedTestClass (powermock.test.support.MainMockTransformerTestSupport.ParentTestClass.NestedTestClass)1 SuperClassCallSuperConstructor (powermock.test.support.MainMockTransformerTestSupport.SuperClassCallSuperConstructor)1 SuperClassCallSuperConstructorWithCast (powermock.test.support.MainMockTransformerTestSupport.SuperClassCallSuperConstructorWithCast)1 SuperClassCallSuperConstructorWithVararg (powermock.test.support.MainMockTransformerTestSupport.SuperClassCallSuperConstructorWithVararg)1 EnumClass (powermock.test.support.MainMockTransformerTestSupport.SupportClasses.EnumClass)1 PublicSuperClass (powermock.test.support.MainMockTransformerTestSupport.SupportClasses.PublicSuperClass)1 SubClass (powermock.test.support.MainMockTransformerTestSupport.SupportClasses.SubClass)1