Search in sources :

Example 11 with ClassWithChildThatHasInternalState

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

the class WhiteBoxTest method testSetInternalMultipleOfSameTypeOnSpecificPlaceInHierarchy.

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

Example 12 with ClassWithChildThatHasInternalState

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

the class WhiteBoxTest method testSetInternalStateInClassAndMakeSureThatTheChildClassIsNotAffectedEvenThoughItHasAFieldWithTheSameName.

@Test
public void testSetInternalStateInClassAndMakeSureThatTheChildClassIsNotAffectedEvenThoughItHasAFieldWithTheSameName() throws Exception {
    final int value = 22;
    final String fieldName = "anotherInternalState";
    ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState() {
    };
    Whitebox.setInternalState(tested, fieldName, value);
    assertEquals(value, Whitebox.getInternalState(tested, fieldName));
    assertEquals(-1, Whitebox.getInternalState(tested, fieldName, ClassWithInternalState.class));
}
Also used : ClassWithInternalState(org.powermock.reflect.testclasses.ClassWithInternalState) ClassWithChildThatHasInternalState(org.powermock.reflect.testclasses.ClassWithChildThatHasInternalState) Test(org.junit.Test)

Example 13 with ClassWithChildThatHasInternalState

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

the class WhiteBoxTest method testInvokePrivateMethodWithASubTypeOfTheArgumentType.

@Test
public void testInvokePrivateMethodWithASubTypeOfTheArgumentType() throws Exception {
    ClassWithPrivateMethods tested = new ClassWithPrivateMethods();
    ClassWithChildThatHasInternalState argument = new ClassWithChildThatHasInternalState();
    assertSame(argument, Whitebox.invokeMethod(tested, "methodWithObjectArgument", argument));
}
Also used : ClassWithPrivateMethods(org.powermock.reflect.testclasses.ClassWithPrivateMethods) ClassWithChildThatHasInternalState(org.powermock.reflect.testclasses.ClassWithChildThatHasInternalState) Test(org.junit.Test)

Example 14 with ClassWithChildThatHasInternalState

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

the class WhiteBoxTest method testSetInternalStateBasedOnObjectSubClassTypeAtASpecificPlaceInTheClassHierarchy.

@Test
public void testSetInternalStateBasedOnObjectSubClassTypeAtASpecificPlaceInTheClassHierarchy() throws Exception {
    final ClassWithPrivateMethods value = new ClassWithPrivateMethods() {
    };
    ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState();
    Whitebox.setInternalState(tested, value, ClassWithInternalState.class);
    assertSame(value, tested.getClassWithPrivateMethods());
}
Also used : ClassWithPrivateMethods(org.powermock.reflect.testclasses.ClassWithPrivateMethods) ClassWithChildThatHasInternalState(org.powermock.reflect.testclasses.ClassWithChildThatHasInternalState) Test(org.junit.Test)

Example 15 with ClassWithChildThatHasInternalState

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

the class WhiteBoxTest method testGetInternalState_superClass_parameterized.

@SuppressWarnings("deprecation")
@Test
public void testGetInternalState_superClass_parameterized() throws Exception {
    ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState();
    int internalState = Whitebox.getInternalState(tested, "internalState", ClassWithInternalState.class, int.class);
    assertEquals(0, internalState);
}
Also used : ClassWithChildThatHasInternalState(org.powermock.reflect.testclasses.ClassWithChildThatHasInternalState) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)20 ClassWithChildThatHasInternalState (org.powermock.reflect.testclasses.ClassWithChildThatHasInternalState)20 ClassWithPrivateMethods (org.powermock.reflect.testclasses.ClassWithPrivateMethods)3 ClassWithInternalState (org.powermock.reflect.testclasses.ClassWithInternalState)2 Field (java.lang.reflect.Field)1 TooManyFieldsFoundException (org.powermock.reflect.exceptions.TooManyFieldsFoundException)1