2

I have inherited a complex JUnit 4 test class which uses

@RunWith(MockitoJUnitRunner.class)

Because of a change in implementation I need to use some features from PowerMock to mock static and private methods. But when I try to switch at class level to

@RunWith(PowerMockRunner.class)
@PrepareForTest(MyClass.class)

I get lots of errors because Mockito annotations are not being properly handled.

The question is: can I keep the @RunWith(MockitoJUnitRunner.class) annotation and do programmatically the same as @PrepareForTest(MyClass.class) just when I need it for a class?

Serg M Ten
  • 5,568
  • 4
  • 25
  • 48
  • 1
    Not sure if [this](https://github.com/powermock/powermock/wiki/JUnit_Delegating_Runner) could work for your use case – troig Feb 26 '19 at 09:24
  • `@PowerMockRunnerDelegate` is probably a step in the right direction. Now I am into something like these annotations for the test class: `@RunWith(PowerMockRunner.class) @PowerMockIgnore({"org.springframework.*","javax.persistence.*"}) @PowerMockRunnerDelegate(MockitoJUnitRunner.class) @PrepareForTest({ObjectMapper.class}) // @ContextConfiguration(classes= {MyClass.class, EntityManager.class}) //@ContextConfiguration({"classpath:META-INF/beans.xml"})` – Serg M Ten Feb 27 '19 at 16:49
  • The current problem is having the dependencies injected by Spring, which only happens if I add the `@ContextConfiguration` annotation. But still can't inject interfaces like `EntityManager`. – Serg M Ten Feb 27 '19 at 16:49

0 Answers0