如何将java安全策略文件设置为在通过gradle执行junit测试时只避免system.exit调用?

uurv41yg  于 2021-07-05  发布在  Java
关注(0)|答案(0)|浏览(260)

我想通过gradle执行junit测试,我想避免在这些测试中调用system.exit()调用。我读过,设置一个安全策略文件就可以了。
这是我正在使用的安全策略文件

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

我在gradle构建文件中设置seecurity.policy和security manager属性,如下所示:

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

但当我运行测试时,测试任务失败,堆栈跟踪如下:

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

看起来安全管理器正在阻止对jmockit等代理的其他权限。我需要像往常一样对代码库应用除system.exit()调用以外的所有其他权限。
我该怎么做?我错过什么了吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题