英文:
Spock:Class Cast Exception but works with real call
问题
以下是翻译好的部分:
我在测试以下代码时,使用Spring Boot启动测试时正常运行,但在尝试使用Spock进行测试时失败。并且抛出以下错误:
java.lang.ClassCastException: 无法将类com.sun.proxy.$Proxy30转换为类java.lang.String(com.sun.proxy.$Proxy30位于加载器'app'的未命名模块中;java.lang.String位于加载器'bootstrap'的java.base模块中)。
如何解决这个问题。已更新帖子,包含代码和测试,测试在此处失败:
测试:
class JWTAuthenticationManagerSpec extends Specification {
private JWTAuthenticationManager sut
private Authentication authentication = Stub()
def 'authenticate抛出异常'() {
given:
authentication.getCredentials() == "Token"
when:
sut.authenticate(authentication)
then:
ForbiddenException ex = thrown()
}
}
类方法测试:
override fun authenticate(authentication: Authentication): Mono<Authentication> {
// 这里抛出了单元测试类转换异常
val token = authentication.credentials as String
if (token.isNullOrEmpty()) {
throw ForbiddenException("提供了无效的访问令牌。")
} else {
log.info { "JWT Token 验证成功" }
}
return Mono.empty()
}
我们是否需要添加额外的代码以消除这里抛出的类转换异常?
英文:
I am testing the following code when I test with spring boot start works fine ,but fails when I try to test with spock.And throws
java.lang.ClassCastException: class com.sun.proxy.$Proxy30 cannot be cast to class java.lang.String (com.sun.proxy.$Proxy30 is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
How to resolve this issue.Updated the post with code and test where the test is failing
Test:
class JWTAuthenticationManagerSpec extends Specification {
private JWTAuthenticationManager sut
private Authentication authentication = Stub()
def 'authenticate throws exception'() {
given:
authentication.getCredentials() == "Token"
when:
sut.authenticate(authentication)
then:
ForbiddenException ex = thrown()
}
}
Class Method Testing
override fun authenticate(authentication: Authentication): Mono<Authentication> {
//Unit Test Class Cast Exception is being thrown Here
val token = authentication.credentials as String
if (token.isNullOrEmpty()) {
throw ForbiddenException("Invalid access token supplied.")
} else {
log.info { "JWT Token Validation Success" }
}
return Mono.empty()
}
Do we have to add additional coding in order to remove class cast exception thats been throwing here
答案1
得分: 1
这部分
given:
authentication.getCredentials() == "Token"
看起来是错误的。在given:
块中比较两个值将不会对你有所帮助,据我所见:
- 如果
authentication
是一个模拟对象,它将只会返回null
,因此比较将会产生false
,但不会有任何有意义的操作。 - 如果
authentication
是一个间谍对象,它将返回原始方法的结果,比较结果将取决于那个结果。
我认为你可能尝试去存根化方法的结果。为了实现这个目的,你应该使用>>
,如下所示:
given:
authentication.getCredentials() >> "Token"
英文:
This part
given:
authentication.getCredentials() == "Token"
looks wrong. Comparing two values in a given:
block will not help you, as far as I can see:
- If
authentication
is a mock, it will just returnnull
, hence the comparison will yieldfalse
but not do anything meaningful. - If
authentication
is a spy, it will return the original method result and the comparison result will depend on that.
I think you might have tried to stub the result of the method. For that purpose, you want to use >>
as in
given:
authentication.getCredentials() >> "Token"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论