Search in sources :

Example 1 with ClassWithChildThatHasInternalState

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

the class WhiteBoxTest method testSetInternalStateBasedOnObjectTypeWhenArgumentIsAPrimitiveTypeUsingGenerics.

@Test
public void testSetInternalStateBasedOnObjectTypeWhenArgumentIsAPrimitiveTypeUsingGenerics() throws Exception {
    final int value = 22;
    ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState();
    Whitebox.setInternalState(tested, value);
    assertEquals((Integer) value, Whitebox.<Integer>getInternalState(tested, "anotherInternalState", ClassWithChildThatHasInternalState.class));
}
Also used : ClassWithChildThatHasInternalState(org.powermock.reflect.testclasses.ClassWithChildThatHasInternalState) Test(org.junit.Test)

Example 2 with ClassWithChildThatHasInternalState

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

the class WhiteBoxTest method testSetInternalStateBasedOnObjectType.

@Test
public void testSetInternalStateBasedOnObjectType() throws Exception {
    final String value = "a string";
    ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState();
    Whitebox.setInternalState(tested, value);
    assertEquals(value, Whitebox.getInternalState(tested, String.class));
}
Also used : ClassWithChildThatHasInternalState(org.powermock.reflect.testclasses.ClassWithChildThatHasInternalState) Test(org.junit.Test)

Example 3 with ClassWithChildThatHasInternalState

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

the class WhiteBoxTest method testSetAndGetInternalStateAtASpecificPlaceInTheHierarchyBasedOnFieldType.

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

Example 4 with ClassWithChildThatHasInternalState

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

the class WhiteBoxTest method testSetInternalStateBasedOnObjectTypeAtASpecificPlaceInTheClassHierarchyForPrimitiveType.

@Test
public void testSetInternalStateBasedOnObjectTypeAtASpecificPlaceInTheClassHierarchyForPrimitiveType() throws Exception {
    final long value = 31;
    ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState();
    Whitebox.setInternalState(tested, value, ClassWithInternalState.class);
    assertEquals(value, tested.getInternalLongState());
}
Also used : ClassWithChildThatHasInternalState(org.powermock.reflect.testclasses.ClassWithChildThatHasInternalState) Test(org.junit.Test)

Example 5 with ClassWithChildThatHasInternalState

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

the class WhiteBoxTest method testSetInternalStateInChildClassWithoutSpecifyingTheChildClass.

@Test
public void testSetInternalStateInChildClassWithoutSpecifyingTheChildClass() throws Exception {
    final int value = 22;
    final String fieldName = "internalState";
    ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState() {
    };
    Whitebox.setInternalState(tested, fieldName, value);
    assertEquals(value, Whitebox.getInternalState(tested, fieldName));
}
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