How to set java security policy file to only avoid System.exit calls while executing JUnit tests via gradle?

446 views Asked by At

I want to execute JUnit tests via gradle where I want to avoid invocation of System.exit() calls in these tests. I've read that setting a security policy file would do the trick.

This is the security policy file that I am using

grant {
    permission java.io.RuntimePermission "exitVM", "none";
};

I am setting the seecurity.policy and security manager properties in the gradle build file like this:

systemProperty 'java.security.policy', file(project.ext.projectHome + "/security.policy").absolutePath
jvmArgs "-Djava.security.manager"

But when I run the test, the test task fails with the following stack trace:

uccessfully started process 'Gradle Test Executor 20'
Exception in thread "main" java.lang.reflect.InvocationTargetException
        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 java.instrument/sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:513)
        at java.instrument/sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:525)
Caused by: java.lang.ExceptionInInitializerError
        at mockit.internal.state.TestRun.<init>(TestRun.java:43)
        at mockit.internal.state.TestRun.<clinit>(TestRun.java:22)
        at mockit.internal.faking.FakeMethods.registerFakeStates(FakeMethods.java:210)
        at mockit.internal.faking.FakeClassSetup.registerFakeClassAndItsStates(FakeClassSetup.java:57)
        at mockit.internal.faking.FakeClassSetup.<init>(FakeClassSetup.java:48)
        at mockit.internal.faking.FakeClassSetup.<init>(FakeClassSetup.java:35)
        at mockit.MockUp.redefineClass(MockUp.java:122)
        at mockit.MockUp.<init>(MockUp.java:80)
        at mockit.integration.junit4.FakeRunNotifier.<init>(FakeRunNotifier.java:24)
        at mockit.internal.startup.JMockitInitialization.applyInternalStartupFakesAsNeeded(JMockitInitialization.java:37)
        at mockit.internal.startup.JMockitInitialization.initialize(JMockitInitialization.java:26)
        at mockit.internal.startup.Startup.applyStartupFakes(Startup.java:58)
        at mockit.internal.startup.Startup.initialize(Startup.java:50)
        at mockit.internal.startup.Startup.premain(Startup.java:44)
        ... 6 more
Caused by: java.security.AccessControlException: access denied ("java.lang.reflect.ReflectPermission" "suppressAccessChecks")
        at java.base/java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
        at java.base/java.security.AccessController.checkPermission(AccessController.java:897)
        at java.base/java.lang.SecurityManager.checkPermission(SecurityManager.java:322)
        at java.base/java.lang.reflect.AccessibleObject.checkPermission(AccessibleObject.java:83)
        at java.base/java.lang.reflect.Method.setAccessible(Method.java:191)
        at mockit.internal.faking.FakeClasses.<clinit>(FakeClasses.java:21)
FATAL ERROR in native method: processing of -javaagent failed
        ... 20 more

It looks like security manager is blocking other permissions to agents like JMockit. I need all the other permissions applied as usual to the codebase except the System.exit() call.

How can I do this? Am I missing something?

0

There are 0 answers