I have a test that correctly fails with an InaccessibleObjectException when I run it with JVM args --illegal-access=deny
in Eclipse. I want it to fail the same way when I run gradle check
.
I tried the solution from How to pass args to JVM which runs tests with Gradle:
# build.gradle
apply plugin: 'java'
test {
jvmArgs '--illegal-access=deny'
# also tried
# jvmArgs('--illegal-access', 'deny')
# jvmArgs '-Dillegal-access=deny'
}
The test passed instead of failing. I did see tests saying they were dirty because jvmArgs had changed.
Here's the JUnit test that fails to fail. Sorry it doesn't have an "expectedException" set up, but it does throw when run with --illegal-access=deny
from Eclipse.
import static org.junit.Assert.fail;
import java.lang.reflect.Field;
import org.junit.Test;
public class IllegalAccessTest {
@Test
public void testIllegalAccess() throws NoSuchFieldException, SecurityException {
Field libraries = ClassLoader.class.getDeclaredField("loadedLibraryNames");
System.out.println("About to set accessible");
libraries.setAccessible(true);
fail("Should fail before getting here when run with --illegal-access=deny");
}
}
The output from this test when run with Gradle shows -Dillegal-access=deny
is getting passed to Gradle, just not causing the test to fail:
Starting process 'Gradle Test Executor 33'. Working directory: xxx Command: /usr/java/jdk-11.0.4/bin/java -Dillegal-access=deny -Dorg.gradle.native=false -javaagent:xxx,jmx=false @/tmp/gradle-worker-classpath17509364376879385105txt -Xmx512m -Dfile.encoding=UTF-8 -Duser.country=US -Duser.language=en -Duser.variant -ea worker.org.gradle.process.internal.worker.GradleWorkerMain 'Gradle Test Executor 33'
Successfully started process 'Gradle Test Executor 33'
x.y.z.IllegalAccessTest > testIllegalAccessQS STANDARD_OUT
About to set accessible
x.y.z.IllegalAccessTest > testIllegalAccessQS FAILED
java.lang.AssertionError: Should fail before getting here when run with --illegal-access=deny
at org.junit.Assert.fail(Assert.java:88)
at x.y.z.IllegalAccessTest.testIllegalAccessQS(IllegalAccessTest.java:36)
The error message when run with Eclipse is the correct
java.lang.reflect.InaccessibleObjectException: Unable to make field private static final java.util.Set java.lang.ClassLoader.loadedLibraryNames accessible: module java.base does not "opens java.lang" to unnamed module @6b9651f3
apply plugin 'java'
. I gather from docs.gradle.org/current/userguide/java_plugin.html that thejava
plugin is a bit obsolete now in favor ofjava-library
orapplication
plugins. But I didn't find anything that explicitly said "This version or plugin doesn't work with Java 9 modules". – LacyjvmArgs("--illegal-access=deny")
. I'm guessing it's worth a try. – Bootlejava.lang.reflect.InaccessibleObjectException
. FWIW, I’m using OpenJDK 11.0.5 and Gradle 5.6.2. If you’d find my minimal example useful, then I could post it as an “answer”; just let me know. – Kori