Azianese
Azianese

Reputation: 624

How to mock static method while using Spring Boot?

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.

Upvotes: 1

Views: 8510

Answers (2)

Jeff Bennett
Jeff Bennett

Reputation: 1056

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.

Upvotes: 0

Azianese
Azianese

Reputation: 624

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

Upvotes: 0

Related Questions