Search in sources :

Example 6 with ClassWithInternalState

use of org.powermock.reflect.testclasses.ClassWithInternalState in project powermock by powermock.

the class WhiteBoxTest method assertThatWhiteboxWorksWithGenericsWhenSpecifyingFieldName.

@Test
public void assertThatWhiteboxWorksWithGenericsWhenSpecifyingFieldName() throws Exception {
    ClassWithInternalState object = new ClassWithInternalState();
    Set<String> state = Whitebox.getInternalState(object, "genericState");
    assertSame(object.getGenericState(), state);
}
Also used : ClassWithInternalState(org.powermock.reflect.testclasses.ClassWithInternalState) Test(org.junit.Test)

Example 7 with ClassWithInternalState

use of org.powermock.reflect.testclasses.ClassWithInternalState in project powermock by powermock.

the class WhiteBoxTest method testSetInternalStateWithMultipleValues.

@Test
public void testSetInternalStateWithMultipleValues() throws Exception {
    ClassWithInternalState tested = new ClassWithInternalState();
    final ClassWithPrivateMethods classWithPrivateMethods = new ClassWithPrivateMethods();
    final String stringState = "someStringState";
    Whitebox.setInternalState(tested, classWithPrivateMethods, stringState);
    assertEquals(stringState, Whitebox.getInternalState(tested, String.class));
    assertSame(classWithPrivateMethods, Whitebox.getInternalState(tested, ClassWithPrivateMethods.class));
}
Also used : ClassWithInternalState(org.powermock.reflect.testclasses.ClassWithInternalState) ClassWithPrivateMethods(org.powermock.reflect.testclasses.ClassWithPrivateMethods) Test(org.junit.Test)

Example 8 with ClassWithInternalState

use of org.powermock.reflect.testclasses.ClassWithInternalState in project powermock by powermock.

the class WhiteBoxTest method testSetInternalMultipleOfSameType.

@Test
public void testSetInternalMultipleOfSameType() throws Exception {
    final int value = 31;
    ClassWithInternalState tested = new ClassWithInternalState();
    try {
        Whitebox.setInternalState(tested, value);
        fail("should throw TooManyFieldsFoundException!");
    } catch (TooManyFieldsFoundException e) {
        assertEquals("Two or more fields matching type int.", e.getMessage());
    }
}
Also used : ClassWithInternalState(org.powermock.reflect.testclasses.ClassWithInternalState) TooManyFieldsFoundException(org.powermock.reflect.exceptions.TooManyFieldsFoundException) Test(org.junit.Test)

Example 9 with ClassWithInternalState

use of org.powermock.reflect.testclasses.ClassWithInternalState in project powermock by powermock.

the class WhiteBoxTest method assertThatErrorMessageIsCorrectWhenNoInstanceFieldFound.

@Test
public void assertThatErrorMessageIsCorrectWhenNoInstanceFieldFound() throws Exception {
    ClassWithInternalState classWithInternalState = new ClassWithInternalState();
    try {
        Whitebox.setInternalState(classWithInternalState, (byte) 23);
        fail("Should throw a FieldNotFoundException.");
    } catch (FieldNotFoundException e) {
        assertEquals("No instance field assignable from \"java.lang.Byte\" could be found in the class hierarchy of " + ClassWithInternalState.class.getName() + ".", e.getMessage());
    }
}
Also used : ClassWithInternalState(org.powermock.reflect.testclasses.ClassWithInternalState) FieldNotFoundException(org.powermock.reflect.exceptions.FieldNotFoundException) Test(org.junit.Test)

Example 10 with ClassWithInternalState

use of org.powermock.reflect.testclasses.ClassWithInternalState in project powermock by powermock.

the class WhiteBoxTest method testFinalState.

@Test
@Ignore("Reflection and direct call returns different values.")
public void testFinalState() {
    ClassWithInternalState state = new ClassWithInternalState();
    String expected = "changed";
    Whitebox.setInternalState(state, "finalString", expected);
    assertEquals(expected, state.getFinalString());
    assertEquals(expected, Whitebox.getInternalState(state, "finalString"));
}
Also used : ClassWithInternalState(org.powermock.reflect.testclasses.ClassWithInternalState) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)11 ClassWithInternalState (org.powermock.reflect.testclasses.ClassWithInternalState)11 Ignore (org.junit.Ignore)1 FieldNotFoundException (org.powermock.reflect.exceptions.FieldNotFoundException)1 TooManyFieldsFoundException (org.powermock.reflect.exceptions.TooManyFieldsFoundException)1 ClassWithPrivateMethods (org.powermock.reflect.testclasses.ClassWithPrivateMethods)1