Search in sources :

Example 6 with SuppressConstructorHierarchy

use of samples.suppressconstructor.SuppressConstructorHierarchy in project powermock by powermock.

the class SuppressConstructorHierarchyDemoTest method testGetNumber.

/**
 * This simple test demonstrate that it's possible to continue execution
 * with the default {@code PrepareForTest} settings (i.e. using a
 * byte-code manipulated version of the SuppressConstructorHierarchyDemo
 * class).
 */
@Test
public void testGetNumber() throws Exception {
    suppress(constructor(SuppressConstructorHierarchy.class));
    SuppressConstructorHierarchy tested = new SuppressConstructorHierarchy("message");
    assertThat(tested.getNumber()).isEqualTo(42);
}
Also used : SuppressConstructorHierarchy(samples.suppressconstructor.SuppressConstructorHierarchy) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with SuppressConstructorHierarchy

use of samples.suppressconstructor.SuppressConstructorHierarchy in project powermock by powermock.

the class SuppressConstructorHierarchyDemoTest method testSuppressConstructor.

@Test
public void testSuppressConstructor() throws Exception {
    suppress(constructor(SuppressConstructorHierarchy.class));
    SuppressConstructorHierarchy tested = new SuppressConstructorHierarchy("message");
    final String message = tested.getMessage();
    assertNull("Message should have been null since we're skipping the execution of the constructor code. Message was \"" + message + "\".", message);
}
Also used : SuppressConstructorHierarchy(samples.suppressconstructor.SuppressConstructorHierarchy) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with SuppressConstructorHierarchy

use of samples.suppressconstructor.SuppressConstructorHierarchy 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 9 with SuppressConstructorHierarchy

use of samples.suppressconstructor.SuppressConstructorHierarchy in project powermock by powermock.

the class SuppressConstructorHierarchyDemoTest method directConstructorUsage.

@Test
public void directConstructorUsage() throws Exception {
    System.out.println("ClassLoader: " + getClass().getClassLoader());
    try {
        SuppressConstructorHierarchy tested = new SuppressConstructorHierarchy("message");
        if (suppress) {
            assertNull("Message should have been null since we're skipping the execution of the constructor code. Message was \"message\".", tested.getMessage());
            assertEquals("getNumber() value", 42, tested.getNumber());
        } else {
            fail("Expected RuntimeException");
        }
    } catch (RuntimeException ex) {
        if (suppress) {
            throw ex;
        } else {
            assertEquals("This should be suppressed!!", ex.getMessage());
        }
    }
}
Also used : SuppressConstructorHierarchy(samples.suppressconstructor.SuppressConstructorHierarchy) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 10 with SuppressConstructorHierarchy

use of samples.suppressconstructor.SuppressConstructorHierarchy in project powermock by powermock.

the class SuppressConstructorHierarchyDemoTest method testSuppressConstructorHierarchyAgain.

/**
 * This simple test demonstrate that it's possible to continue execution
 * with the default {@code PrepareForTest} settings (i.e. using a
 * byte-code manipulated version of the SuppressConstructorHierarchyDemo
 * class).
 */
@Test
public void testSuppressConstructorHierarchyAgain() throws Exception {
    suppress(constructor(SuppressConstructorHierarchy.class));
    SuppressConstructorHierarchy tested = new SuppressConstructorHierarchy("message");
    assertEquals(42, tested.getNumber());
}
Also used : SuppressConstructorHierarchy(samples.suppressconstructor.SuppressConstructorHierarchy) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

SuppressConstructorHierarchy (samples.suppressconstructor.SuppressConstructorHierarchy)10 Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 SuppressField (samples.suppressfield.SuppressField)1 SuppressMethod (samples.suppressmethod.SuppressMethod)1