Search in sources :

Example 16 with SuppressMethod

use of samples.suppressmethod.SuppressMethod in project powermock by powermock.

the class StubMethodTest method whenStubbingInstanceMethodByPassingTheMethodTheMethodReturnsTheStubbedValue.

@Test
public void whenStubbingInstanceMethodByPassingTheMethodTheMethodReturnsTheStubbedValue() throws Exception {
    String expected = "Hello";
    stub(method(SuppressMethod.class, "getObject")).toReturn(expected);
    SuppressMethod tested = new SuppressMethod();
    assertEquals(expected, tested.getObject());
    assertEquals(expected, tested.getObject());
}
Also used : SuppressMethod(samples.suppressmethod.SuppressMethod) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 17 with SuppressMethod

use of samples.suppressmethod.SuppressMethod in project powermock by powermock.

the class StubMethodTest method whenStubbingInstanceMethodWithWrongReturnTypeThenClasscastExceptionIsThrown.

@Test(expected = ClassCastException.class)
public void whenStubbingInstanceMethodWithWrongReturnTypeThenClasscastExceptionIsThrown() throws Exception {
    String illegalReturnType = "Hello";
    stub(method(SuppressMethod.class, "getFloat")).toReturn(illegalReturnType);
    SuppressMethod tested = new SuppressMethod();
    tested.getFloat();
}
Also used : SuppressMethod(samples.suppressmethod.SuppressMethod) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 18 with SuppressMethod

use of samples.suppressmethod.SuppressMethod in project powermock by powermock.

the class SupressMethodExampleTest method verifySuppression.

@Test
public void verifySuppression() throws Exception {
    getObjectSuppression.doIt();
    getIntSuppression.doIt();
    fieldSuppression.doIt();
    assertEquals("getObject return-value", getObjectSuppression.expectedReturnValue, new SuppressMethod().getObject());
    assertEquals("getInt return-value", getIntSuppression.expectedReturnValue, new SuppressMethod().getInt());
    assertThat("Value from field", new SuppressField().getDomainObject(), is(fieldSuppression.expectation));
    if (suppressConstructor) {
        suppress(constructor(SuppressConstructorHierarchy.class));
    } else {
        expectedException.expect(RuntimeException.class);
    }
    SuppressConstructorHierarchy tested = new SuppressConstructorHierarchy("message");
    assertTrue("Or a runtime exception should have been thrown by now", suppressConstructor);
    assertEquals(42, tested.getNumber());
    assertNull(tested.getMessage());
}
Also used : SuppressField(samples.suppressfield.SuppressField) SuppressMethod(samples.suppressmethod.SuppressMethod) SuppressConstructorHierarchy(samples.suppressconstructor.SuppressConstructorHierarchy) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 19 with SuppressMethod

use of samples.suppressmethod.SuppressMethod in project powermock by powermock.

the class MemberModificationExampleTest method changingReturnValueExample.

@Test
public void changingReturnValueExample() throws Exception {
    replace(method(SuppressMethod.class, "getObjectWithArgument")).with(new ReturnValueChangingInvocationHandler());
    final SuppressMethod tested = new SuppressMethod();
    assertThat(tested.getObjectWithArgument("don't do anything"), is(instanceOf(Object.class)));
    assertEquals("hello world", tested.getObjectWithArgument("make it a string"));
}
Also used : SuppressMethod(samples.suppressmethod.SuppressMethod) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 20 with SuppressMethod

use of samples.suppressmethod.SuppressMethod in project powermock by powermock.

the class MemberModificationExampleTest method suppressAllMethodsExample.

@Test
public void suppressAllMethodsExample() throws Exception {
    suppress(methodsDeclaredIn(SuppressMethod.class));
    final SuppressMethod tested = new SuppressMethod();
    assertNull(tested.getObject());
    assertNull(SuppressMethod.getObjectStatic());
    assertEquals(0, tested.getByte());
}
Also used : SuppressMethod(samples.suppressmethod.SuppressMethod) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Test (org.junit.Test)39 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)39 SuppressMethod (samples.suppressmethod.SuppressMethod)39 MethodNotFoundException (org.powermock.reflect.exceptions.MethodNotFoundException)2 TooManyMethodsFoundException (org.powermock.reflect.exceptions.TooManyMethodsFoundException)2 SuppressMethodExample (samples.suppressmethod.SuppressMethodExample)2 SuppressMethodParent (samples.suppressmethod.SuppressMethodParent)2 Callable (java.util.concurrent.Callable)1 Ignore (org.junit.Ignore)1 SuppressConstructorHierarchy (samples.suppressconstructor.SuppressConstructorHierarchy)1 SuppressField (samples.suppressfield.SuppressField)1