3

When I use PowerMockito.whenNew...thenAnswer - the answer run immediately! my code:

new Integer(9);
PowerMockito.whenNew(Integer.class).withAnyArguments().thenAnswer(invocation -> {
    System.out.println("kuku int");
    return null;
});
System.out.println("test");
new Integer(10);

output:

kuku int
test
kuku int

How to solve this.

Azriel Berger
  • 160
  • 4
  • 17
  • Probably (I'm not sure though), is that PowerMockito, replaces the Integer constructor during compilation, not during runtime, which causes All `new Integer()` calls to go through PowerMockito – GuyKhmel Dec 18 '17 at 11:55
  • 1
    The above code works as expected for me. I hope you have this on top of your Test Class `@RunWith(PowerMockRunner.class) @PrepareForTest({Integer.class})` I got the correct output : test followed by kuku int – pvpkiran Dec 18 '17 at 12:08

0 Answers0