Dan
Dan

Reputation: 741

PowerMockRule with JDK 11

I was using PowerMockRule to cover a unit test which was working perfectly fine in JDK 8 but when I upgraded my application with JDK 11, it started failing the unit test.

I tried a couple of dependency combinations with power mock but nothing worked for me. Trying to figure out the root cause and solution. Can some please help me with this?

Error getting on JDK 11

java.io.RuntimeException: java.io.IOException: Can not attach to current VM
at org.powermock.modules.agent.AgentLoader.attachToThisVM(AgentLoader.java:140)
at org.powermock.modules.agent.AgentLoader.loadAgent(AgentLoader.java:87)
at org.powermock.modules.agent.AgentInitialization.initializeAccordingToJDKVersion(AgentInitialization.java:40)

Below is a simple unit test (not going for complex unit test - but even a simple one is giving the same error)

class PowerMockSimpleSpec extend Specification {

  @Rule
  public PowerMockRule rule = new PowerMockRule()  

  def "one plus one should equal two"() {
    expect:
    1 + 1 == 2
  }
}

Dependencies worked for JDK 8 but not for JDK 11

testImplementation group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.9'
testImplementation group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.9'
testImplementation group: 'org.powermock', name: 'powermock-module-junit4-rule-agent', version: '2.0.9'

tried by adding below dependencies but not luck

testImplementation group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.9'
testImplementation group: 'org.powermock', name: 'powermock-module-junit4-rule', version: '2.0.9'
testImplementation group: 'org.powermock', name: 'powermock-classloading-xstream', version: '2.0.9'

Also referred below documentation but again no luck with depedencies https://github.com/powermock/powermock/wiki/PowerMockRule

Upvotes: 0

Views: 1635

Answers (1)

tgdavies
tgdavies

Reputation: 11464

Looks like you need to add -Djdk.attach.allowAttachSelf=true

See https://github.com/powermock/powermock/issues/979#issuecomment-485678666

Upvotes: 2

Related Questions