Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PowerMockAgent does not support java >=10 #979

Closed
carstenartur opened this issue Mar 27, 2019 · 1 comment

Comments

Projects
None yet
1 participant
@carstenartur
Copy link

commented Mar 27, 2019

On java 11 you get an error in your junit test when the code in PowerMockAgent is used because of this:

`public final class PowerMockAgent
{

static final String javaSpecVersion = System.getProperty("java.specification.version");
static final boolean jdk6OrLater = "1.6".equals(javaSpecVersion) || "1.7".equals(javaSpecVersion) || "1.8".equals(javaSpecVersion)
    || "9".equals(javaSpecVersion);

`

So computing the information about the version after jdk6 or before it does not take into account that for java 11 the javaSpecVersion variable is "11".
Consequently you get the following error:

java.lang.RuntimeException: java.lang.IllegalStateException: PowerMock requires a Java 5 VM or later.
at org.powermock.modules.agent.PowerMockClassRedefiner.redefine(PowerMockClassRedefiner.java:40)
at org.powermock.modules.agent.support.PowerMockAgentTestInitializer.redefine(PowerMockAgentTestInitializer.java:62)
at org.powermock.modules.agent.support.PowerMockAgentTestInitializer.redefineClassesToPrepare(PowerMockAgentTestInitializer.java:57)
at org.powermock.modules.agent.support.PowerMockAgentTestInitializer.redefineClasses(PowerMockAgentTestInitializer.java:44)
at org.powermock.modules.agent.support.PowerMockAgentTestInitializer.initialize(PowerMockAgentTestInitializer.java:34)
at org.powermock.modules.junit4.rule.PowerMockRule.apply(PowerMockRule.java:47)
at org.junit.runners.BlockJUnit4ClassRunner.withMethodRules(BlockJUnit4ClassRunner.java:365)
at org.junit.runners.BlockJUnit4ClassRunner.withRules(BlockJUnit4ClassRunner.java:355)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:246)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.withRulesReflectively(SpringJUnit4ClassRunner.java:313)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: java.lang.IllegalStateException: PowerMock requires a Java 5 VM or later.
at org.powermock.modules.agent.AgentInitialization.initializeAccordingToJDKVersion(AgentInitialization.java:48)
at org.powermock.modules.agent.PowerMockAgent.verifyInitialization(PowerMockAgent.java:84)
at org.powermock.modules.agent.PowerMockAgent.instrumentation(PowerMockAgent.java:77)
at org.powermock.modules.agent.PowerMockClassRedefiner.redefine(PowerMockClassRedefiner.java:38)
... 31 more

carstenartur added a commit to carstenartur/powermock that referenced this issue Mar 29, 2019

see powermock#979
Signed-off-by: Carsten Hammer <Carsten.Hammer@t-online.de>

thekingnothing added a commit that referenced this issue Apr 21, 2019

thekingnothing added a commit that referenced this issue Apr 21, 2019

[ci maven-central-release] Fixes #979 Generalise getting current java…
… version to fix be able to use JavaAgent with latest java (#985)

Fixes #979 Generalise getting current java version to fix be able to use Javaagent with the latest java
@carstenartur

This comment has been minimized.

Copy link
Author

commented Apr 23, 2019

Could it be that with Java 11 you need additionally "-Djdk.attach.allowAttachSelf=true" to make it run?
Without that I get

Caused by: java.io.IOException: Can not attach to current VM
at jdk.attach/sun.tools.attach.HotSpotVirtualMachine.(HotSpotVirtualMachine.java:75)
at jdk.attach/sun.tools.attach.VirtualMachineImpl.(VirtualMachineImpl.java:48)
at jdk.attach/sun.tools.attach.AttachProviderImpl.attachVirtualMachine(AttachProviderImpl.java:69)
at jdk.attach/com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:207)
at org.powermock.modules.agent.AgentLoader.attachToThisVM(AgentLoader.java:136)
... 32 more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.