我有一个测试,当我在Eclipse中使用JVM参数--illegal-access=deny
运行它时,它会因InaccessibleObjectException正确失败。当我运行gradle check
时,我希望它以相同的方式失败。
我尝试了How to pass args to JVM which runs tests with Gradle中的解决方案:
# build.gradle
apply plugin: 'java'
test {
jvmArgs '--illegal-access=deny'
}
测试通过而不是失败。我确实看到测试说它们很脏,因为jvmArgs已更改。
我尝试过:
# build.gradle
apply plugin: 'java'
test {
jvmArgs('--illegal-access', 'deny')
}
然后我得到
Successfully started process 'Gradle Test Executor 110'
Unrecognized option: --illegal-access
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
编辑:这是JUnit测试,如果您想尝试,该测试将失败。抱歉,它没有设置“ expectedException”,但在使用--illegal-access=deny
运行时会抛出异常。
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");
}
}
请尝试这个。这就是我从gradle传递JVM参数到我的测试的方法,并且可以正常工作。
test {
jvmArgs '-Dillegal-access=deny'
}