获取 java.lang.NoSuchMethodException: io.jsonwebtoken.impl.crypto.MacProvider.generateKey 仅在 jUnit 的情况下
Getting java.lang.NoSuchMethodException: io.jsonwebtoken.impl.crypto.MacProvider.generateKey only in case of jUnits
我最近开始遇到以下异常,只有当 junits 正在 运行 时。在正常流程中,方法 运行 没问题。 (事实上 ,甚至 jUnits 过去一段时间都可以正常工作)
java.lang.NoSuchMethodException:
io.jsonwebtoken.impl.crypto.MacProvider.generateKey(io.jsonwebtoken.SignatureAlgorithm)
方法如下:
public static String generateDummyJWT(String userName)
{
return Jwts.builder().claim("user_name", StringUtils.defaultString(userName)).setAudience("client1").signWith(Keys.secretKeyFor(SignatureAlgorithm.HS384)).compact();
}
和 jjwt 版本:
以下是完整的堆栈跟踪:
Caused by: java.lang.IllegalStateException: Unable to invoke class
method io.jsonwebtoken.impl.crypto.MacProvider#generateKey. Ensure
the necessary implementation is in the runtime classpath. at
io.jsonwebtoken.lang.Classes.invokeStatic(Classes.java:202) at
io.jsonwebtoken.security.Keys.secretKeyFor(Keys.java:121) at
com.random.util.ServiceSpecificUtil.generateDummyJWT(ServiceSpecificUtil.java:143)
at
com.random.util.ServiceConstants.(ServiceConstants.java:203)
at com.random.MyClass.isUserBranch(MyClass.java:67) at
com.random.MyClass.validName(MyClass.java:93) at
com.random.MyClass.preConditionCheck(MyClass.java:82) at
com.random.MyClass.get(MyClass.java:46) at
com.random.MyClass2.evaluateExpression(MyClass2.java:218) 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.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:243)
at
org.mockito.internal.invocation.realmethod.CleanTraceRealMethod.invoke(CleanTraceRealMethod.java:30)
at
org.mockito.internal.invocation.InvocationImpl.callRealMethod(InvocationImpl.java:112)
at
org.mockito.internal.stubbing.answers.CallsRealMethods.answer(CallsRealMethods.java:41)
at
org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:93)
at
org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.performIntercept(MockitoMethodInvocationControl.java:266)
at
org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:192)
at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:132)
at org.powermock.core.MockGateway.methodCall(MockGateway.java:63) at
com.random.MyClass2.evaluateExpression(MyClass2.java) at
com.random.MyClass2.isPermitted(MyClass2.java:107) 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.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:243)
at
org.mockito.internal.invocation.realmethod.CleanTraceRealMethod.invoke(CleanTraceRealMethod.java:30)
at
org.mockito.internal.invocation.InvocationImpl.callRealMethod(InvocationImpl.java:112)
at
org.mockito.internal.stubbing.answers.CallsRealMethods.answer(CallsRealMethods.java:41)
at
org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:93)
at
org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.performIntercept(MockitoMethodInvocationControl.java:266)
at
org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:192)
... 32 more Caused by: java.lang.reflect.InvocationTargetException
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
io.jsonwebtoken.lang.Classes.invokeStatic(Classes.java:198) ... 66
more Caused by: java.lang.IllegalStateException: The HmacSHA384
algorithm is not available. This should never happen on JDK 7 or
later - please report this to the JJWT developers. at
io.jsonwebtoken.impl.crypto.MacProvider.generateKey(MacProvider.java:94)
at
io.jsonwebtoken.impl.crypto.MacProvider.generateKey(MacProvider.java:63)
... 71 more Caused by: java.security.NoSuchAlgorithmException:
HmacSHA384 KeyGenerator not available at
javax.crypto.KeyGenerator.(KeyGenerator.java:169) at
javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:223) at
io.jsonwebtoken.impl.crypto.MacProvider.generateKey(MacProvider.java:92)
... 72 more
我能够解决我的问题。
团队中有人在像这样初始化静态最终变量时添加了对上述函数的调用:
public static String TOKEN_GUEST = ServiceSpecificUtil.generateDummyJWT(USERNAME);
现在,由于大量使用反射和字节码操作,似乎访问 java.security 包通常不能很好地与 PowerMock
一起使用:
尽管我参考了 this SO post 并尝试了建议的使用解决方案
@PowerMockIgnore
喜欢:
@PowerMockIgnore({"org.apache.http.conn.ssl.*", "javax.net.ssl.*" , "javax.crypto.*"})
这可能有助于防止从另一个函数调用上述函数时出现错误。但这并不能防止静态变量初始化中的错误。
public static String TOKEN_GUEST = ServiceSpecificUtil.generateDummyJWT(USERNAME);
当我更深入地了解为什么错误仅在静态变量初始化的情况下而不是在从另一个函数调用 ServiceSpecificUtil.generateDummyJWT(USERNAME)
时出现时,我将更新此 post。
我最近开始遇到以下异常,只有当 junits 正在 运行 时。在正常流程中,方法 运行 没问题。 (事实上 ,甚至 jUnits 过去一段时间都可以正常工作)
java.lang.NoSuchMethodException: io.jsonwebtoken.impl.crypto.MacProvider.generateKey(io.jsonwebtoken.SignatureAlgorithm)
方法如下:
public static String generateDummyJWT(String userName)
{
return Jwts.builder().claim("user_name", StringUtils.defaultString(userName)).setAudience("client1").signWith(Keys.secretKeyFor(SignatureAlgorithm.HS384)).compact();
}
和 jjwt 版本:
以下是完整的堆栈跟踪:
Caused by: java.lang.IllegalStateException: Unable to invoke class method io.jsonwebtoken.impl.crypto.MacProvider#generateKey. Ensure the necessary implementation is in the runtime classpath. at io.jsonwebtoken.lang.Classes.invokeStatic(Classes.java:202) at io.jsonwebtoken.security.Keys.secretKeyFor(Keys.java:121) at com.random.util.ServiceSpecificUtil.generateDummyJWT(ServiceSpecificUtil.java:143) at com.random.util.ServiceConstants.(ServiceConstants.java:203) at com.random.MyClass.isUserBranch(MyClass.java:67) at com.random.MyClass.validName(MyClass.java:93) at com.random.MyClass.preConditionCheck(MyClass.java:82) at com.random.MyClass.get(MyClass.java:46) at com.random.MyClass2.evaluateExpression(MyClass2.java:218) 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.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:243) at org.mockito.internal.invocation.realmethod.CleanTraceRealMethod.invoke(CleanTraceRealMethod.java:30) at org.mockito.internal.invocation.InvocationImpl.callRealMethod(InvocationImpl.java:112) at org.mockito.internal.stubbing.answers.CallsRealMethods.answer(CallsRealMethods.java:41) at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:93) at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.performIntercept(MockitoMethodInvocationControl.java:266) at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:192) at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:132) at org.powermock.core.MockGateway.methodCall(MockGateway.java:63) at com.random.MyClass2.evaluateExpression(MyClass2.java) at com.random.MyClass2.isPermitted(MyClass2.java:107) 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.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:243) at org.mockito.internal.invocation.realmethod.CleanTraceRealMethod.invoke(CleanTraceRealMethod.java:30) at org.mockito.internal.invocation.InvocationImpl.callRealMethod(InvocationImpl.java:112) at org.mockito.internal.stubbing.answers.CallsRealMethods.answer(CallsRealMethods.java:41) at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:93) at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.performIntercept(MockitoMethodInvocationControl.java:266) at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:192) ... 32 more Caused by: java.lang.reflect.InvocationTargetException 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 io.jsonwebtoken.lang.Classes.invokeStatic(Classes.java:198) ... 66 more Caused by: java.lang.IllegalStateException: The HmacSHA384 algorithm is not available. This should never happen on JDK 7 or later - please report this to the JJWT developers. at io.jsonwebtoken.impl.crypto.MacProvider.generateKey(MacProvider.java:94) at io.jsonwebtoken.impl.crypto.MacProvider.generateKey(MacProvider.java:63) ... 71 more Caused by: java.security.NoSuchAlgorithmException: HmacSHA384 KeyGenerator not available at javax.crypto.KeyGenerator.(KeyGenerator.java:169) at javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:223) at io.jsonwebtoken.impl.crypto.MacProvider.generateKey(MacProvider.java:92) ... 72 more
我能够解决我的问题。
团队中有人在像这样初始化静态最终变量时添加了对上述函数的调用:
public static String TOKEN_GUEST = ServiceSpecificUtil.generateDummyJWT(USERNAME);
现在,由于大量使用反射和字节码操作,似乎访问 java.security 包通常不能很好地与 PowerMock
一起使用:
尽管我参考了 this SO post 并尝试了建议的使用解决方案
@PowerMockIgnore
喜欢:
@PowerMockIgnore({"org.apache.http.conn.ssl.*", "javax.net.ssl.*" , "javax.crypto.*"})
这可能有助于防止从另一个函数调用上述函数时出现错误。但这并不能防止静态变量初始化中的错误。
public static String TOKEN_GUEST = ServiceSpecificUtil.generateDummyJWT(USERNAME);
当我更深入地了解为什么错误仅在静态变量初始化的情况下而不是在从另一个函数调用 ServiceSpecificUtil.generateDummyJWT(USERNAME)
时出现时,我将更新此 post。