Once I add the RunWith
annotation, that is @RunWith(PowerMockRunner.class)
the Spring Autowire does not work anymore!
class B {
@Autowire
SessionFactory session;
}
@RunWith(PowerMockRunner.class)
@PrepareForTest{SomeClass.class}
class Testing {
@Test
methodA(){
//mehod
}
@Test
methodD(){
}
}
Now, method A makes a call to class B, but due to RunWith(PowerMockRunner)
annotation, the Autowiring is not working. Any help would be greatly appreciated!
In 2016 you can use runner delegate with PowerMockito, effectively using two runners:
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
More details here: https://github.com/jayway/powermock/wiki/JUnit_Delegating_Runner
Spring is not magic (even if it seems like it sometimes). No where in your code are you giving spring a chance to start up an application context and do its work of auto wiring your beans. You need to either start the context yourself or switch your test to use @RunWith(SpringJUnit4ClassRunner.class)
. Unfortunately JUnit only supports a single runner at a time so you'll need to stop using the PowerMockRunner then.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With