尝试从类javax.crypto.Cipher JUNIT访问类javax.crypto.JceSecurity。

huangapple go评论109阅读模式
英文:

tried to access class javax.crypto.JceSecurity from class javax.crypto.Cipher JUNIT

问题

我试图在一个实际执行AES加密的方法中模拟cipher.doFinal(),使用JUNIT。

在启动JUNIT测试用例时,我遇到了异常 "tried to access class javax.crypto.JceSecurity from class javax.crypto.Cipher"

我使用以下依赖项进行powermock

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.6.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito</artifactId>
    <version>1.6.2</version>
    <scope>test</scope>
</dependency>

堆栈跟踪:

java.lang.IllegalAccessError: tried to access class javax.crypto.JceSecurity from class javax.crypto.Cipher
    at javax.crypto.Cipher.getInstance(Cipher.java:514)
    -----------------------------------------------------------------------
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    ...
    at org.junit.runner.JUnitCore.main(JUnitCore.java:115)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:210)

应用程序配置:

Spring Boot 2.3.0
JDK 1.8
英文:

I am trying to mock the cipher.doFinal() in a method which actually doing an AES encryption, using JUNIT.
I am getting the exception while starting the JUNIT test case &quot;tried to access class javax.crypto.JceSecurity from class javax.crypto.Cipher&quot;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Cipher.class)
@PowerMockIgnore({&quot;javax.crypto.*&quot; })
public class AesDecryptionUtilityTest {

    @Test
	public void decryptionTest() throws Exception {
		Cipher cipher = PowerMockito.mock(Cipher.class);
        PowerMockito.when(cipher.doFinal(null)).thenReturn(decrypted.getBytes());
        cipher.doFinal(null);

		utility.performDecryption(...);
	}

}

I am using below dependencies for powermock

        &lt;dependency&gt;
		    &lt;groupId&gt;org.powermock&lt;/groupId&gt;
		    &lt;artifactId&gt;powermock-module-junit4&lt;/artifactId&gt;
		    &lt;version&gt;1.6.2&lt;/version&gt;
		    &lt;scope&gt;test&lt;/scope&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
		    &lt;groupId&gt;org.powermock&lt;/groupId&gt;
		    &lt;artifactId&gt;powermock-api-mockito&lt;/artifactId&gt;
		    &lt;version&gt;1.6.2&lt;/version&gt;
		    &lt;scope&gt;test&lt;/scope&gt;
		&lt;/dependency&gt;

Stack trace:

java.lang.IllegalAccessError: tried to access class javax.crypto.JceSecurity from class javax.crypto.Cipher
	at javax.crypto.Cipher.getInstance(Cipher.java:514)
	-----------------------------------------------------------------------
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
	at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
	at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
	at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
	at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
	at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
	at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
	at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
	at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
	at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
	at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:43)
	at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
	at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
	at java.util.Iterator.forEachRemaining(Iterator.java:116)
	at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
	at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
	at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
	at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
	at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
	at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
	at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:82)
	at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:73)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248)
	at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)
	at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:141)
	at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)

Application configuration:

Spring boot 2.3.0
JDK 1.8

答案1

得分: 3

Add this on top your test class:

@PowerMockIgnore({"javax.net.ssl.*", "javax.management.*", "javax.crypto.JceSecurity.*", "javax.crypto.*" })
public class AesDecryptionUtilityTest {
...
}
英文:

Add this on top your test class:

@PowerMockIgnore({&quot;javax.net.ssl.*&quot;, &quot;javax.management.*&quot;, &quot;javax.crypto.JceSecurity.*&quot;, &quot;javax.crypto.*&quot; })
public class AesDecryptionUtilityTest {
...
}

huangapple
  • 本文由 发表于 2020年9月24日 23:49:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64049979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定