How to use ClassWithSimpleInternalState class of org.powermock.reflect.testclasses package

Best Powermock code snippet using org.powermock.reflect.testclasses.ClassWithSimpleInternalState

Source:WhiteBoxTest.java Github

copy

Full Screen

...48import org.powermock.reflect.testclasses.ClassWithPrivateMethods;49import org.powermock.reflect.testclasses.ClassWithSerializableState;50import org.powermock.reflect.testclasses.ClassWithSeveralMethodsWithSameName;51import org.powermock.reflect.testclasses.ClassWithSeveralMethodsWithSameNameOneWithoutParameters;52import org.powermock.reflect.testclasses.ClassWithSimpleInternalState;53import org.powermock.reflect.testclasses.ClassWithStaticAndInstanceInternalStateOfSameType;54import org.powermock.reflect.testclasses.ClassWithStaticMethod;55import org.powermock.reflect.testclasses.ClassWithUniquePrivateMethods;56import org.powermock.reflect.testclasses.ClassWithVarArgsConstructor;57import org.powermock.reflect.testclasses.ClassWithVarArgsConstructor2;58import java.io.InputStream;59import java.io.Serializable;60import java.lang.reflect.Field;61import java.lang.reflect.Method;62import java.lang.reflect.Proxy;63import java.sql.Connection;64import java.util.Set;65import static org.assertj.core.api.Java6Assertions.assertThat;66import static org.junit.Assert.assertArrayEquals;67import static org.junit.Assert.assertEquals;68import static org.junit.Assert.assertNotNull;69import static org.junit.Assert.assertNull;70import static org.junit.Assert.assertSame;71import static org.junit.Assert.assertTrue;72import static org.junit.Assert.fail;73/**74 * Tests the WhiteBox's functionality.75 */76public class WhiteBoxTest {77 @Rule78 public ExpectedException expectedException = ExpectedException.none();79 @Test80 public void testFindMethod_classContainingMethodWithNoParameters() throws Exception {81 Method expected = ClassWithSeveralMethodsWithSameNameOneWithoutParameters.class.getMethod("getDouble");82 Method actual = WhiteboxImpl.findMethodOrThrowException(83 ClassWithSeveralMethodsWithSameNameOneWithoutParameters.class, "getDouble");84 assertEquals(expected, actual);85 }86 @Test87 public void testFindMethod_classContainingOnlyMethodsWithParameters() throws Exception {88 try {89 WhiteboxImpl.findMethodOrThrowException(ClassWithSeveralMethodsWithSameName.class, "getDouble");90 fail("Should throw runtime exception!");91 } catch (RuntimeException e) {92 assertTrue("Error message did not match", e.getMessage().contains(93 "Several matching methods found, please specify the argument parameter types"));94 }95 }96 @Test97 public void testFindMethod_noMethodFound() throws Exception {98 try {99 WhiteboxImpl.findMethodOrThrowException(ClassWithSeveralMethodsWithSameName.class, "getDouble2");100 fail("Should throw runtime exception!");101 } catch (RuntimeException e) {102 assertEquals("Error message did not match",103 "No method found with name 'getDouble2' with parameter types: [ <none> ] in class "104 + ClassWithSeveralMethodsWithSameName.class.getName() + ".", e.getMessage());105 }106 }107 @Test108 public void testGetInternalState_object() throws Exception {109 ClassWithInternalState tested = new ClassWithInternalState();110 tested.increaseInteralState();111 Object internalState = Whitebox.getInternalState(tested, "internalState");112 assertTrue("InternalState should be instanceof Integer", internalState instanceof Integer);113 assertEquals(1, internalState);114 }115 @SuppressWarnings("deprecation")116 @Test117 public void testGetInternalState_parmaterizedType() throws Exception {118 ClassWithInternalState tested = new ClassWithInternalState();119 tested.increaseInteralState();120 int internalState = Whitebox.getInternalState(tested, "internalState", tested.getClass(), int.class);121 assertEquals(1, internalState);122 }123 @Test124 public void testSetInternalState() throws Exception {125 ClassWithInternalState tested = new ClassWithInternalState();126 tested.increaseInteralState();127 Whitebox.setInternalState(tested, "anotherInternalState", 2);128 assertEquals(2, tested.getAnotherInternalState());129 }130 @Test131 public void testSetInternalStateWithMultipleValues() throws Exception {132 ClassWithInternalState tested = new ClassWithInternalState();133 final ClassWithPrivateMethods classWithPrivateMethods = new ClassWithPrivateMethods();134 final String stringState = "someStringState";135 Whitebox.setInternalState(tested, classWithPrivateMethods, stringState);136 assertEquals(stringState, Whitebox.getInternalState(tested, String.class));137 assertSame(classWithPrivateMethods, Whitebox.getInternalState(tested, ClassWithPrivateMethods.class));138 }139 @Test140 public void testSetInternalState_superClass() throws Exception {141 ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState();142 tested.increaseInteralState();143 Whitebox.setInternalState(tested, "anotherInternalState", 2, ClassWithInternalState.class);144 assertEquals(2, tested.getAnotherInternalState());145 }146 @Test147 public void testGetInternalState_superClass_object() throws Exception {148 ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState();149 Object internalState = Whitebox.getInternalState(tested, "internalState", ClassWithInternalState.class);150 assertEquals(0, internalState);151 }152 @SuppressWarnings("deprecation")153 @Test154 public void testGetInternalState_superClass_parameterized() throws Exception {155 ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState();156 int internalState = Whitebox.getInternalState(tested, "internalState", ClassWithInternalState.class, int.class);157 assertEquals(0, internalState);158 }159 @Test160 public void testInvokePrivateMethod_primtiveType() throws Exception {161 assertTrue(Whitebox.<Boolean>invokeMethod(new ClassWithPrivateMethods(), "primitiveMethod", 8.2));162 }163 @Test164 public void testInvokePrivateMethod_primtiveType_withoutSpecifyingMethodName() throws Exception {165 assertTrue((Boolean) Whitebox.invokeMethod(new ClassWithUniquePrivateMethods(), 8.2d, 8.4d));166 }167 /**168 * This test actually invokes the <code>finalize</code> method of169 * <code>java.lang.Object</code> because we supply no method name and no170 * arguments. <code>finalize</code> is thus the first method found and it'll171 * be executed.172 *173 * @throws Exception174 */175 @Test176 @Ignore("Invokes different methods on PC and MAC (hashCode on mac)")177 public void testInvokePrivateMethod_withoutSpecifyingMethodName_noArguments() throws Exception {178 assertNull(Whitebox.invokeMethod(new ClassWithUniquePrivateMethods()));179 }180 @Test181 public void testInvokePrivateMethod_withoutSpecifyingMethodName_assertThatNullWorks() throws Exception {182 assertTrue(Whitebox.invokeMethod(new ClassWithUniquePrivateMethods(), 8.2d, 8.3d, null) instanceof Object);183 }184 /**185 * This test should actually fail since equals takes an Object and we pass186 * in a primitive wrapped as a Double. Thus PowerMock cannot determine187 * whether to invoke the single argument method defined in188 * {@link ClassWithUniquePrivateMethods} or the189 * {@link Object#equals(Object)} method because we could potentially invoke190 * equals with a Double.191 */192 @Test(expected = TooManyMethodsFoundException.class)193 public void testInvokePrivateMethod_withoutSpecifyingMethodName_onlyOneArgument() throws Exception {194 Whitebox.invokeMethod(new ClassWithUniquePrivateMethods(), 8.2d);195 }196 @Test(expected = TooManyMethodsFoundException.class)197 public void testInvokeStaticPrivateMethod_withoutSpecifyingMethodName_onlyOneArgument() throws Exception {198 assertTrue((Boolean) Whitebox.invokeMethod(ClassWithUniquePrivateMethods.class, 8.2d));199 }200 @Test201 public void testInvokePrivateMethod_primtiveType_Wrapped() throws Exception {202 assertTrue((Boolean) Whitebox.invokeMethod(new ClassWithPrivateMethods(), "primitiveMethod", new Double(8.2)));203 }204 @Test205 public void testInvokePrivateMethod_wrappedType() throws Exception {206 assertTrue((Boolean) Whitebox.invokeMethod(new ClassWithPrivateMethods(), "wrappedMethod", new Double(8.2)));207 }208 @Test209 public void testInvokePrivateMethod_wrappedType_primitive() throws Exception {210 assertTrue((Boolean) Whitebox.invokeMethod(new ClassWithPrivateMethods(), "wrappedMethod", 8.2));211 }212 @Test213 public void testMethodWithPrimitiveIntAndString_primitive() throws Exception {214 assertEquals("My int value is: " + 8, Whitebox.invokeMethod(new ClassWithPrivateMethods(),215 "methodWithPrimitiveIntAndString", 8, "My int value is: "));216 }217 @Test218 public void testMethodWithPrimitiveIntAndString_Wrapped() throws Exception {219 assertEquals("My int value is: " + 8, Whitebox.invokeMethod(new ClassWithPrivateMethods(),220 "methodWithPrimitiveIntAndString", Integer.valueOf(8), "My int value is: "));221 }222 @Test223 public void testMethodWithPrimitiveAndWrappedInt_primtive_wrapped() throws Exception {224 assertEquals(17, Whitebox.invokeMethod(new ClassWithPrivateMethods(), "methodWithPrimitiveAndWrappedInt",225 new Class[]{int.class, Integer.class}, 9, Integer.valueOf(8)));226 }227 @Test228 public void testStaticState() {229 int expected = 123;230 Whitebox.setInternalState(ClassWithInternalState.class, "staticState", expected);231 assertEquals(expected, ClassWithInternalState.getStaticState());232 assertEquals(expected, Whitebox.getInternalState(ClassWithInternalState.class, "staticState"));233 }234 @Test235 public void testStaticFinalPrimitiveState() {236 Whitebox.setInternalState(ClassWithInternalState.class, "staticFinalIntState", 123);237 assertEquals(123, Whitebox.getInternalState(ClassWithInternalState.class, "staticFinalIntState"));238 }239 @Test240 public void testStaticFinalStringState() throws NoSuchFieldException {241 Whitebox.setInternalState(ClassWithInternalState.class, "staticFinalStringState", "Brand new string");242 assertEquals("Brand new string", Whitebox.getInternalState(ClassWithInternalState.class, "staticFinalStringState"));243 }244 @Test245 public void testStaticFinalObject() throws NoSuchFieldException {246 int modifiersBeforeSet = ClassWithInternalState.class.getDeclaredField("staticFinalIntegerState").getModifiers();247 Integer newValue = ClassWithInternalState.getStaticFinalIntegerState() + 1;248 Whitebox.setInternalState(ClassWithInternalState.class, "staticFinalIntegerState", newValue);249 int modifiersAfterSet = ClassWithInternalState.class.getDeclaredField("staticFinalIntegerState").getModifiers();250 assertEquals(newValue, ClassWithInternalState.getStaticFinalIntegerState());251 assertEquals(modifiersBeforeSet, modifiersAfterSet);252 }253 /**254 * Verifies that the http://code.google.com/p/powermock/issues/detail?id=6255 * is fixed.256 */257 @Test(expected = IllegalArgumentException.class)258 public void testInvokeMethodWithNullParameter() throws Exception {259 Whitebox.invokeMethod(null, "method");260 }261 @Test(expected = IllegalArgumentException.class)262 public void testInvokeConstructorWithNullParameter() throws Exception {263 Whitebox.invokeConstructor(null, "constructor");264 }265 @Test(expected = IllegalArgumentException.class)266 public void testGetInternalWithNullParameter() throws Exception {267 Whitebox.getInternalState(null, "state");268 }269 @Test(expected = IllegalArgumentException.class)270 public void testSetInternalWithNullParameter() throws Exception {271 Whitebox.setInternalState(null, "state", new Object());272 }273 @Test274 public void testInstantiateVarArgsOnlyConstructor() throws Exception {275 final String argument1 = "argument1";276 final String argument2 = "argument2";277 ClassWithVarArgsConstructor instance = Whitebox.invokeConstructor(ClassWithVarArgsConstructor.class, argument1,278 argument2);279 String[] strings = instance.getStrings();280 assertEquals(2, strings.length);281 assertEquals(argument1, strings[0]);282 assertEquals(argument2, strings[1]);283 }284 @Test285 public void testInstantiateVarArgsOnlyConstructor_noArguments() throws Exception {286 ClassWithVarArgsConstructor instance = Whitebox.invokeConstructor(ClassWithVarArgsConstructor.class);287 String[] strings = instance.getStrings();288 assertEquals(0, strings.length);289 }290 @Test291 public void testInvokeVarArgsMethod_multipleValues() throws Exception {292 ClassWithPrivateMethods tested = new ClassWithPrivateMethods();293 assertEquals(6, Whitebox.invokeMethod(tested, "varArgsMethod", 1, 2, 3));294 }295 @Test296 public void testInvokeVarArgsMethod_noArguments() throws Exception {297 ClassWithPrivateMethods tested = new ClassWithPrivateMethods();298 assertEquals(0, Whitebox.invokeMethod(tested, "varArgsMethod"));299 }300 @Test301 public void testInvokeVarArgsMethod_oneArgument() throws Exception {302 ClassWithPrivateMethods tested = new ClassWithPrivateMethods();303 assertEquals(4, Whitebox.invokeMethod(tested, "varArgsMethod", 2));304 }305 @Test306 public void testInvokeVarArgsMethod_invokeVarArgsWithOneArgument() throws Exception {307 ClassWithPrivateMethods tested = new ClassWithPrivateMethods();308 assertEquals(1, Whitebox.invokeMethod(tested, "varArgsMethod", new Class<?>[]{int[].class}, 1));309 }310 @Test311 public void testInvokePrivateMethodWithASubTypeOfTheArgumentType() throws Exception {312 ClassWithPrivateMethods tested = new ClassWithPrivateMethods();313 ClassWithChildThatHasInternalState argument = new ClassWithChildThatHasInternalState();314 assertSame(argument, Whitebox.invokeMethod(tested, "methodWithObjectArgument", argument));315 }316 @Test317 public void testInvokePrivateMethodWithAClassArgument() throws Exception {318 ClassWithPrivateMethods tested = new ClassWithPrivateMethods();319 assertEquals(ClassWithChildThatHasInternalState.class, Whitebox.invokeMethod(tested, "methodWithClassArgument",320 ClassWithChildThatHasInternalState.class));321 }322 @Test323 public void testSetInternalStateInChildClassWithoutSpecifyingTheChildClass() throws Exception {324 final int value = 22;325 final String fieldName = "internalState";326 ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState() {327 };328 Whitebox.setInternalState(tested, fieldName, value);329 assertEquals(value, Whitebox.getInternalState(tested, fieldName));330 }331 @Test332 public void testSetInternalStateInClassAndMakeSureThatTheChildClassIsNotAffectedEvenThoughItHasAFieldWithTheSameName()333 throws Exception {334 final int value = 22;335 final String fieldName = "anotherInternalState";336 ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState() {337 };338 Whitebox.setInternalState(tested, fieldName, value);339 assertEquals(value, Whitebox.getInternalState(tested, fieldName));340 assertEquals(-1, Whitebox.getInternalState(tested, fieldName, ClassWithInternalState.class));341 }342 @Test(expected = IllegalArgumentException.class)343 public void testSetInternalStateWithInvalidArgumentType() throws Exception {344 final int value = 22;345 final String fieldName = "internalState";346 ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState() {347 };348 Whitebox.setInternalState(tested, fieldName, new Object());349 assertEquals(value, Whitebox.getInternalState(tested, fieldName));350 }351 @Test(expected = IllegalArgumentException.class)352 public void testSetInternalStateWithNull() throws Exception {353 final int value = 22;354 final String fieldName = "internalState";355 ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState() {356 };357 Whitebox.setInternalState(tested, fieldName, (Object) null);358 assertEquals(value, Whitebox.getInternalState(tested, fieldName));359 }360 @Test361 public void testSetAndGetInternalStateBasedOnFieldType() throws Exception {362 final int value = 22;363 ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState();364 Whitebox.setInternalState(tested, int.class, value);365 assertEquals(value, (int) Whitebox.getInternalState(tested, int.class));366 assertEquals(value, Whitebox.getInternalState(tested, "anotherInternalState"));367 assertEquals(value, Whitebox.getInternalState(tested, "anotherInternalState",368 ClassWithChildThatHasInternalState.class));369 }370 @Test371 public void testSetAndGetInternalStateAtASpecificPlaceInTheHierarchyBasedOnFieldType() throws Exception {372 final int value = 22;373 ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState();374 Whitebox.setInternalState(tested, int.class, value, ClassWithInternalState.class);375 assertEquals(42, (int) Whitebox.getInternalState(tested, int.class));376 assertEquals(value, (int) Whitebox.getInternalState(tested, int.class, ClassWithInternalState.class));377 assertEquals(value, Whitebox.getInternalState(tested, "staticState", ClassWithInternalState.class));378 }379 @Test380 public void testSetInternalStateBasedOnObjectType() throws Exception {381 final String value = "a string";382 ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState();383 Whitebox.setInternalState(tested, value);384 assertEquals(value, Whitebox.getInternalState(tested, String.class));385 }386 @SuppressWarnings("deprecation")387 @Test388 public void testSetInternalStateBasedOnObjectTypeWhenArgumentIsAPrimitiveType() throws Exception {389 final int value = 22;390 ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState();391 Whitebox.setInternalState(tested, value);392 assertEquals((Integer) value, Whitebox.getInternalState(tested, "anotherInternalState",393 ClassWithChildThatHasInternalState.class, Integer.class));394 }395 @Test396 public void testSetInternalStateBasedOnObjectTypeWhenArgumentIsAPrimitiveTypeUsingGenerics() throws Exception {397 final int value = 22;398 ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState();399 Whitebox.setInternalState(tested, value);400 assertEquals((Integer) value, Whitebox.<Integer>getInternalState(tested, "anotherInternalState",401 ClassWithChildThatHasInternalState.class));402 }403 @Test404 public void testSetInternalStateBasedOnObjectTypeAtASpecificPlaceInTheClassHierarchy() throws Exception {405 final String value = "a string";406 ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState();407 Whitebox.setInternalState(tested, (Object) value, ClassWithInternalState.class);408 assertEquals(value, Whitebox.getInternalState(tested, "finalString"));409 }410 @Test411 public void testSetInternalStateBasedOnObjectTypeAtASpecificPlaceInTheClassHierarchyForPrimitiveType()412 throws Exception {413 final long value = 31;414 ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState();415 Whitebox.setInternalState(tested, value, ClassWithInternalState.class);416 assertEquals(value, tested.getInternalLongState());417 }418 @Test419 public void testSetInternalStateBasedOnObjectTypeAtANonSpecificPlaceInTheClassHierarchyForPrimitiveType()420 throws Exception {421 final long value = 31;422 ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState();423 Whitebox.setInternalState(tested, value);424 assertEquals(value, tested.getInternalLongState());425 }426 @Test427 public void testSetInternalMultipleOfSameTypeOnSpecificPlaceInHierarchy() throws Exception {428 final int value = 31;429 ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState();430 try {431 Whitebox.setInternalState(tested, value, ClassWithInternalState.class);432 fail("should throw TooManyFieldsFoundException!");433 } catch (TooManyFieldsFoundException e) {434 assertEquals("Two or more fields matching type int.", e.getMessage());435 }436 }437 @Test438 public void testSetInternalMultipleOfSameType() throws Exception {439 final int value = 31;440 ClassWithInternalState tested = new ClassWithInternalState();441 try {442 Whitebox.setInternalState(tested, value);443 fail("should throw TooManyFieldsFoundException!");444 } catch (TooManyFieldsFoundException e) {445 assertEquals("Two or more fields matching type int.", e.getMessage());446 }447 }448 @Test449 public void testSetInternalStateBasedOnObjectSubClassTypeAtASpecificPlaceInTheClassHierarchy() throws Exception {450 final ClassWithPrivateMethods value = new ClassWithPrivateMethods() {451 };452 ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState();453 Whitebox.setInternalState(tested, value, ClassWithInternalState.class);454 assertSame(value, tested.getClassWithPrivateMethods());455 }456 @Test457 public void testSetInternalStateBasedOnObjectSubClassType() throws Exception {458 final ClassWithPrivateMethods value = new ClassWithPrivateMethods() {459 };460 ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState() {461 };462 Whitebox.setInternalState(tested, value);463 assertSame(value, tested.getClassWithPrivateMethods());464 }465 @Test466 public void testGetAllInstanceFields() throws Exception {467 Set<Field> allFields = Whitebox.getAllInstanceFields(new ClassWithChildThatHasInternalState());468 assertEquals(8, allFields.size());469 }470 @Test471 public void testGetAllInstanceFieldsOnClass() {472 Set<Field> allFields = Whitebox.getAllInstanceFields(ClassWithChildThatHasInternalState.class);473 assertEquals(8, allFields.size());474 }475 @Test476 public void testGetAllStaticFields_assertNoFieldsFromParent() throws Exception {477 Set<Field> allFields = Whitebox.getAllStaticFields(ClassWithChildThatHasInternalState.class);478 assertEquals(0, allFields.size());479 }480 @Test481 public void testGetAllStaticFields() throws Exception {482 Set<Field> allFields = Whitebox.getAllStaticFields(ClassWithInternalState.class);483 assertEquals(4, allFields.size());484 }485 @Test486 public void testMethodWithNoMethodName_noMethodFound() throws Exception {487 try {488 Whitebox.getMethod(ClassWithInternalState.class, int.class);489 fail("Should throw MethodNotFoundException");490 } catch (MethodNotFoundException e) {491 assertEquals(492 "No method was found with parameter types: [ int ] in class org.powermock.reflect.testclasses.ClassWithInternalState.",493 e.getMessage());494 }495 }496 @Test497 public void testMethodWithNoMethodName_tooManyMethodsFound() throws Exception {498 try {499 Whitebox.getMethod(ClassWithSeveralMethodsWithSameName.class);500 fail("Should throw TooManyMethodsFoundException");501 } catch (TooManyMethodsFoundException e) {502 assertTrue(e503 .getMessage()504 .contains(505 "Several matching methods found, please specify the method name so that PowerMock can determine which method you're referring to"));506 }507 }508 @Test509 public void testMethodWithNoMethodName_ok() throws Exception {510 final Method method = Whitebox.getMethod(ClassWithSeveralMethodsWithSameName.class, double.class);511 assertEquals(method, ClassWithSeveralMethodsWithSameName.class.getDeclaredMethod("getDouble", double.class));512 }513 @Test514 public void testGetTwoMethodsWhenNoneOfThemAreFound() throws Exception {515 try {516 Whitebox.getMethods(ClassWithSeveralMethodsWithSameName.class, "notFound1", "notFound2");517 } catch (MethodNotFoundException e) {518 assertEquals(519 "No methods matching the name(s) notFound1 or notFound2 were found in the class hierarchy of class org.powermock.reflect.testclasses.ClassWithSeveralMethodsWithSameName.",520 e.getMessage());521 }522 }523 @Test524 public void testGetThreeMethodsWhenNoneOfThemAreFound() throws Exception {525 try {526 Whitebox.getMethods(ClassWithSeveralMethodsWithSameName.class, "notFound1", "notFound2", "notFound3");527 } catch (MethodNotFoundException e) {528 assertEquals(529 "No methods matching the name(s) notFound1, notFound2 or notFound3 were found in the class hierarchy of class org.powermock.reflect.testclasses.ClassWithSeveralMethodsWithSameName.",530 e.getMessage());531 }532 }533 /**534 * Asserts that <a535 * href="http://code.google.com/p/powermock/issues/detail?id=118">issue536 * 118</a> is fixed. Thanks to cemcatik for finding this.537 */538 @Test539 public void testInvokeConstructorWithBothNormalAndVarArgsParameter() throws Exception {540 ClassWithVarArgsConstructor2 instance = Whitebox.invokeConstructor(ClassWithVarArgsConstructor2.class, "first",541 "second", "third");542 assertArrayEquals(new String[]{"first", "second", "third"}, instance.getStrings());543 }544 /**545 * Asserts that <a546 * href="http://code.google.com/p/powermock/issues/detail?id=118">issue547 * 118</a> is fixed. Thanks to cemcatik for finding this.548 */549 @Test550 public void testInvokeMethodWithBothNormalAndVarArgsParameter() throws Exception {551 ClassWithPrivateMethods tested = new ClassWithPrivateMethods();552 assertEquals(4, Whitebox.invokeMethod(tested, "varArgsMethod2", 1, 2, 3));553 }554 @Test555 public void testInvokePrivateMethodWithArrayArgument() throws Exception {556 ClassWithPrivateMethods tested = new ClassWithPrivateMethods();557 assertEquals("Hello World", Whitebox.invokeMethod(tested, "evilConcatOfStrings", new Object[]{new String[]{558 "Hello ", "World"}}));559 }560 @Test561 public void testSetInternalStateFromContext_allStatesInSameOneContext() throws Exception {562 ClassWithSimpleInternalState tested = new ClassWithSimpleInternalState();563 MyContext context = new MyContext();564 Whitebox.setInternalStateFromContext(tested, context);565 assertEquals(context.getMyStringState(), tested.getSomeStringState());566 assertEquals(context.getMyIntState(), tested.getSomeIntState());567 }568 @Test569 public void testSetInternalStateFromContext_statesInDifferentContext() throws Exception {570 ClassWithSimpleInternalState tested = new ClassWithSimpleInternalState();571 MyIntContext myIntContext = new MyIntContext();572 MyStringContext myStringContext = new MyStringContext();573 Whitebox.setInternalStateFromContext(tested, myIntContext, myStringContext);574 assertEquals(myStringContext.getMyStringState(), tested.getSomeStringState());575 assertEquals(myIntContext.getSimpleIntState(), tested.getSomeIntState());576 }577 @Test578 public void testSetInternalStateFromContext_contextIsAClass() throws Exception {579 ClassWithSimpleInternalState tested = new ClassWithSimpleInternalState();580 Whitebox.setInternalStateFromContext(tested, MyContext.class);581 assertEquals(Whitebox.getInternalState(MyContext.class, long.class), (Long) tested.getSomeStaticLongState());582 }583 @Test584 public void testSetInternalStateFromContext_contextIsAClassAndAnInstance() throws Exception {585 ClassWithSimpleInternalState tested = new ClassWithSimpleInternalState();586 MyContext myContext = new MyContext();587 Whitebox.setInternalStateFromContext(tested, MyContext.class, myContext);588 assertEquals(myContext.getMyStringState(), tested.getSomeStringState());589 assertEquals(myContext.getMyIntState(), tested.getSomeIntState());590 assertEquals((Long) myContext.getMyLongState(), (Long) tested.getSomeStaticLongState());591 }592 @Test593 public void testSetInternalStateFromContext_contextHasOneInstanceAndOneStaticFieldOfSameType_onlyInstanceContext()594 throws Exception {595 ClassWithStaticAndInstanceInternalStateOfSameType.reset();596 ClassWithStaticAndInstanceInternalStateOfSameType tested = new ClassWithStaticAndInstanceInternalStateOfSameType();597 OneInstanceAndOneStaticFieldOfSameTypeContext context = new OneInstanceAndOneStaticFieldOfSameTypeContext();598 Whitebox.setInternalStateFromContext(tested, context);599 assertEquals(context.getMyStringState(), tested.getStringState());600 assertEquals("Static String state", tested.getStaticStringState());601 }602 @Test603 public void testSetInternalStateFromContext_contextHasOneInstanceAndOneStaticFieldOfSameType_onlyStaticContext()604 throws Exception {605 ClassWithStaticAndInstanceInternalStateOfSameType.reset();606 ClassWithStaticAndInstanceInternalStateOfSameType tested = new ClassWithStaticAndInstanceInternalStateOfSameType();607 Whitebox.setInternalStateFromContext(tested, OneInstanceAndOneStaticFieldOfSameTypeContext.class);608 assertEquals(OneInstanceAndOneStaticFieldOfSameTypeContext.getMyStaticStringState(), tested609 .getStaticStringState());610 assertEquals("String state", tested.getStringState());611 }612 @Test613 public void setInternalStateFromInstanceContextCopiesMatchingContextFieldsToTargetObjectByDefault()614 throws Exception {615 ClassWithSimpleInternalState tested = new ClassWithSimpleInternalState();616 InstanceFieldsNotInTargetContext fieldsNotInTargetContext = new InstanceFieldsNotInTargetContext();617 assertThat(tested.getSomeStringState()).isNotEqualTo(fieldsNotInTargetContext.getString());618 Whitebox.setInternalStateFromContext(tested, fieldsNotInTargetContext);619 assertEquals(tested.getSomeStringState(), fieldsNotInTargetContext.getString());620 }621 @Test622 public void setInternalStateFromInstanceContextCopiesMatchingContextFieldsToTargetObjectWhenSpecifyingMatchingStrategy()623 throws Exception {624 ClassWithSimpleInternalState tested = new ClassWithSimpleInternalState();625 InstanceFieldsNotInTargetContext fieldsNotInTargetContext = new InstanceFieldsNotInTargetContext();626 assertThat(tested.getSomeStringState()).isNotEqualTo(fieldsNotInTargetContext.getString());627 Whitebox.setInternalStateFromContext(tested, fieldsNotInTargetContext, FieldMatchingStrategy.MATCHING);628 assertEquals(tested.getSomeStringState(), fieldsNotInTargetContext.getString());629 }630 @Test(expected = FieldNotFoundException.class)631 public void setInternalStateFromInstanceContextThrowsExceptionWhenContextContainsFieldsNotDefinedInTargetObjectWhenSpecifyingStrictStrategy()632 throws Exception {633 ClassWithSimpleInternalState tested = new ClassWithSimpleInternalState();634 InstanceFieldsNotInTargetContext fieldsNotInTargetContext = new InstanceFieldsNotInTargetContext();635 assertThat(tested.getSomeStringState()).isNotEqualTo(fieldsNotInTargetContext.getString());636 Whitebox.setInternalStateFromContext(tested, fieldsNotInTargetContext, FieldMatchingStrategy.STRICT);637 assertEquals(tested.getSomeStringState(), fieldsNotInTargetContext.getString());638 }639 @Test640 public void setInternalStateFromClassContextCopiesMatchingContextFieldsToTargetObjectByDefault() throws Exception {641 long state = ClassWithSimpleInternalState.getLong();642 try {643 assertThat(state).isNotEqualTo(ClassFieldsNotInTargetContext.getLong());644 Whitebox.setInternalStateFromContext(ClassWithSimpleInternalState.class,645 ClassFieldsNotInTargetContext.class);646 assertEquals(ClassFieldsNotInTargetContext.getLong(), ClassWithSimpleInternalState.getLong());647 } finally {648 // Restore the state649 ClassWithSimpleInternalState.setLong(state);650 }651 }652 @Test653 public void setInternalStateFromClassContextCopiesMatchingContextFieldsToTargetObjectWhenSpecifyingMatchingStrategy()654 throws Exception {655 long state = ClassWithSimpleInternalState.getLong();656 try {657 assertThat(state).isNotEqualTo(ClassFieldsNotInTargetContext.getLong());658 Whitebox.setInternalStateFromContext(ClassWithSimpleInternalState.class,659 ClassFieldsNotInTargetContext.class, FieldMatchingStrategy.MATCHING);660 assertEquals(ClassFieldsNotInTargetContext.getLong(), ClassWithSimpleInternalState.getLong());661 } finally {662 // Restore the state663 ClassWithSimpleInternalState.setLong(state);664 }665 }666 @Test(expected = FieldNotFoundException.class)667 public void setInternalStateFromClassContextThrowsExceptionWhenContextContainsFieldsNotDefinedInTargetObjectWhenSpecifyingStrictStrategy()668 throws Exception {669 long state = ClassWithSimpleInternalState.getLong();670 try {671 assertThat(state).isNotEqualTo(ClassFieldsNotInTargetContext.getLong());672 Whitebox.setInternalStateFromContext(ClassWithSimpleInternalState.class,673 ClassFieldsNotInTargetContext.class, FieldMatchingStrategy.STRICT);674 } finally {675 // Restore the state676 ClassWithSimpleInternalState.setLong(state);677 }678 }679 @Test680 public void assertThatErrorMessageIsCorrectWhenNoInstanceFieldFound() throws Exception {681 ClassWithInternalState classWithInternalState = new ClassWithInternalState();682 try {683 Whitebox.setInternalState(classWithInternalState, (byte) 23);684 fail("Should throw a FieldNotFoundException.");685 } catch (FieldNotFoundException e) {686 assertEquals(687 "No instance field assignable from \"java.lang.Byte\" could be found in the class hierarchy of "688 + ClassWithInternalState.class.getName() + ".", e.getMessage());689 }690 }...

Full Screen

Full Screen

ClassWithSimpleInternalState

Using AI Code Generation

copy

Full Screen

1ClassWithSimpleInternalState classWithSimpleInternalState = new ClassWithSimpleInternalState();2ClassWithStaticInternalState classWithStaticInternalState = new ClassWithStaticInternalState();3ClassWithFinalInternalState classWithFinalInternalState = new ClassWithFinalInternalState();4ClassWithFinalInternalState classWithFinalInternalState2 = new ClassWithFinalInternalState();5ClassWithFinalInternalState classWithFinalInternalState3 = new ClassWithFinalInternalState();6ClassWithFinalInternalState classWithFinalInternalState4 = new ClassWithFinalInternalState();7ClassWithFinalInternalState classWithFinalInternalState5 = new ClassWithFinalInternalState();8ClassWithFinalInternalState classWithFinalInternalState6 = new ClassWithFinalInternalState();9ClassWithFinalInternalState classWithFinalInternalState7 = new ClassWithFinalInternalState();10ClassWithFinalInternalState classWithFinalInternalState8 = new ClassWithFinalInternalState();11ClassWithFinalInternalState classWithFinalInternalState9 = new ClassWithFinalInternalState();12ClassWithFinalInternalState classWithFinalInternalState10 = new ClassWithFinalInternalState();13ClassWithFinalInternalState classWithFinalInternalState11 = new ClassWithFinalInternalState();14ClassWithFinalInternalState classWithFinalInternalState12 = new ClassWithFinalInternalState();15ClassWithFinalInternalState classWithFinalInternalState13 = new ClassWithFinalInternalState();16ClassWithFinalInternalState classWithFinalInternalState14 = new ClassWithFinalInternalState();17ClassWithFinalInternalState classWithFinalInternalState15 = new ClassWithFinalInternalState();18ClassWithFinalInternalState classWithFinalInternalState16 = new ClassWithFinalInternalState();19ClassWithFinalInternalState classWithFinalInternalState17 = new ClassWithFinalInternalState();20ClassWithFinalInternalState classWithFinalInternalState18 = new ClassWithFinalInternalState();21ClassWithFinalInternalState classWithFinalInternalState19 = new ClassWithFinalInternalState();22ClassWithFinalInternalState classWithFinalInternalState20 = new ClassWithFinalInternalState();23ClassWithFinalInternalState classWithFinalInternalState21 = new ClassWithFinalInternalState();

Full Screen

Full Screen

ClassWithSimpleInternalState

Using AI Code Generation

copy

Full Screen

1package org.powermock.reflect.testclasses;2import org.powermock.reflect.internal.ClassWithSimpleInternalState$InternalState;3import org.powermock.reflect.internal.ClassWithSimpleInternalState;4public class ClassWithSimpleInternalStateTest {5 public void testInternalState() {6 ClassWithSimpleInternalState classWithSimpleInternalState = new ClassWithSimpleInternalState();7 InternalState internalState = new InternalState(classWithSimpleInternalState);8 internalState.set("state", "new value");9 assertEquals("new value", internalState.get("state"));10 }11}12package org.powermock.reflect.testclasses;13import org.powermock.reflect.internal.ClassWithSimpleInternalState$InternalState;14public class ClassWithSimpleInternalState {15 private String state;16 public void setState(String state) {17 this.state = state;18 }19 public String getState() {20 return state;21 }22 public String getStateWithInternalState() {23 return new InternalState(this).get("state");24 }25}26package org.powermock.reflect.internal;27public class ClassWithSimpleInternalState$InternalState {28 private final ClassWithSimpleInternalState classWithSimpleInternalState;29 public ClassWithSimpleInternalState$InternalState(ClassWithSimpleInternalState classWithSimpleInternalState) {30 this.classWithSimpleInternalState = classWithSimpleInternalState;31 }32 public void set(String name, Object value) {33 classWithSimpleInternalState.setState((String) value);34 }35 public Object get(String name) {36 return classWithSimpleInternalState.getState();37 }38}39package org.powermock.reflect.internal;40public class ClassWithSimpleInternalState {41 private String state;42 public void setState(String state) {43 this.state = state;44 }45 public String getState() {46 return state;47 }48 public String getStateWithInternalState() {49 return new InternalState(this).get("state");

Full Screen

Full Screen

ClassWithSimpleInternalState

Using AI Code Generation

copy

Full Screen

1import org.powermock.reflect.testclasses.ClassWithSimpleInternalState2import org.powermock.reflect.testclasses.internal.ClassWithSimpleInternalState$InnerClass3import org.powermock.reflect.testclasses.internal2.ClassWithSimpleInternalState$InnerClass24import org.powermock.reflect.testclasses.internal3.ClassWithSimpleInternalState$InnerClass35import org.powermock.reflect.testclasses.internal4.ClassWithSimpleInternalState$InnerClass46import org.powermock.reflect.Whitebox7import static org.junit.Assert.assertEquals8import static org.junit.Assert.assertNotNull9import static org.junit.Assert.assertNull10import static org.junit.Assert.assertSame11import static org.junit.Assert.assertTrue12import org.junit.Test13public class WhiteboxImplTest {14 public void testSetInternalStateWithObject() throws Exception {15 final ClassWithSimpleInternalState instance = new ClassWithSimpleInternalState()16 Whitebox.setInternalState(instance, "string", expected)17 assertSame(expected, Whitebox.getInternalState(instance, "string"))18 }19 public void testSetInternalStateWithClass() throws Exception {20 final ClassWithSimpleInternalState instance = new ClassWithSimpleInternalState()21 Whitebox.setInternalState(ClassWithSimpleInternalState.class, instance, "string", expected)22 assertSame(expected, Whitebox.getInternalState(instance, "string"))23 }24 public void testSetInternalStateWithClassAndString() throws Exception {25 final ClassWithSimpleInternalState instance = new ClassWithSimpleInternalState()26 Whitebox.setInternalState(ClassWithSimpleInternalState.class.getName(), instance, "string", expected)27 assertSame(expected, Whitebox.getInternalState(instance, "string"))28 }29 public void testGetInternalStateWithObject() throws Exception {

Full Screen

Full Screen

ClassWithSimpleInternalState

Using AI Code Generation

copy

Full Screen

1### ClassWithSimpleInternalState()2### ClassWithSimpleInternalState(String)3### public String getString()4### public String getString(String)5### public void setString(String)6### public void setString(String, String)7### public void setString(String, String, String)8### public void setString(String, String, String, String)9### public void setString(String, String, String, String, String)10### public void setString(String, String, String, String, String, String)11### public void setString(String, String, String, String, String, String, String)12### public void setString(String, String, String, String, String, String, String, String)

Full Screen

Full Screen

ClassWithSimpleInternalState

Using AI Code Generation

copy

Full Screen

1ClassWithSimpleInternalState classWithSimpleInternalState = new ClassWithSimpleInternalState();2Whitebox.setInternalState(classWithSimpleInternalState, "internalState", 10);3int internalState = Whitebox.getInternalState(classWithSimpleInternalState, "internalState");4assertThat(internalState, is(10));5Whitebox.setInternalState(classWithSimpleInternalState, "internalState", "Hello World");6String internalStateString = Whitebox.getInternalState(classWithSimpleInternalState, "internalState");7assertThat(internalStateString, is("Hello World"));8Whitebox.setInternalState(classWithSimpleInternalState, "internalState", null);9Object internalStateObject = Whitebox.getInternalState(classWithSimpleInternalState, "internalState");10assertThat(internalStateObject, is(nullValue()));11Whitebox.setInternalState(classWithSimpleInternalState, "internalState", null);12Object internalStateObject = Whitebox.getInternalState(classWithSimpleInternalState, "internalState");13assertThat(internalStateObject, is(nullValue()));14Whitebox.setInternalState(classWithSimpleInternalState, "internalState", null);15Object internalStateObject = Whitebox.getInternalState(classWithSimpleInternalState, "internalState");16assertThat(internalStateObject, is(nullValue()));17Whitebox.setInternalState(classWithSimpleInternalState, "internalState", null);18Object internalStateObject = Whitebox.getInternalState(classWithSimpleInternalState, "internalState");19assertThat(internalStateObject, is

Full Screen

Full Screen

ClassWithSimpleInternalState

Using AI Code Generation

copy

Full Screen

1ClassWithSimpleInternalState state = new ClassWithSimpleInternalState();2state.setInternalState("someValue");3org.powermock.reflect.testclasses.ClassWithSimpleInternalState state = new org.powermock.reflect.testclasses.ClassWithSimpleInternalState();4state.setInternalState("someValue");5The same problem exists for the code snippets that are using methods from the classes that are not imported in the snippet. The solution is to use the fully qualified name of the method in the code snippet:6org.powermock.reflect.testclasses.ClassWithSimpleInternalState state = new org.powermock.reflect.testclasses.ClassWithSimpleInternalState();7state.setInternalState("someValue");8org.powermock.reflect.testclasses.ClassWithSimpleInternalState state = new org.powermock.reflect.testclasses.ClassWithSimpleInternalState();9state.setInternalState("someValue");10The same problem exists for the code snippets that are using methods from the classes that are not imported in the snippet. The solution is to use the fully qualified name of the method in the code snippet:

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Powermock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in ClassWithSimpleInternalState

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful