1

I need to mock a static method. I know of two tools to help do this: PowerMockito and JMockit, both of which require usage of the @RunWith annotation. However, I've already used @RunWith for SpringRunner to set up some @Autowired dependencies, and it seems you can't use @RunWith twice.

I see that PowerMockito has a PowerMockRunnerDelegate, which looks promising. But for various reasons, I'm stuck using JMockit for now. Is there a JMockit equivalent for this?

I essentially need to mock a static method and set up @Autowired dependencies for my test at the same time.

Azianese
  • 554
  • 7
  • 21

2 Answers2

0

It looks like I can circumvent the need to use any specialized static mocking tool by wrapping the static method call.

Azianese
  • 554
  • 7
  • 21
  • JMockit is not a "specialized" mocking tool. In fact, it is more "feature complete" than any other mocking library for Java. It has actually become a lot more than a mocking library. I personally use it more for integration tests, rather than unit tests. – Rogério Jun 19 '20 at 04:32
0

The latest version of jmockit (1.49) does not utilize the @RunWith annotation. Instead, it has you setup a javaagent as a vmarg. There's full documentation on the jmockit site: https://jmockit.github.io/tutorial/Introduction.html#runningTests. I'd definitely encourage you to use JMockIt rather than the EasyMock+PowerMock combo, JMockIt is self-contained and handles statics naturally.

Jeff Bennett
  • 996
  • 7
  • 18