斯波克(Spock):类转换异常,但在实际调用中有效。

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

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 &#39;app&#39;; java.lang.String is in module java.base of loader &#39;bootstrap&#39;)

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 &#39;authenticate throws exception&#39;() {

        given:
        authentication.getCredentials() == &quot;Token&quot;

        when:
        sut.authenticate(authentication)
        then:
        ForbiddenException ex = thrown()
    }
}

Class Method Testing

 override fun authenticate(authentication: Authentication): Mono&lt;Authentication&gt; {
        //Unit Test Class Cast Exception is being thrown Here
        val token = authentication.credentials as String
        if (token.isNullOrEmpty()) {
            throw ForbiddenException(&quot;Invalid access token supplied.&quot;)
        } else {
            log.info { &quot;JWT Token Validation Success&quot;  }
        }
        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() == &quot;Token&quot;

看起来是错误的。在given:块中比较两个值将不会对你有所帮助,据我所见:

  • 如果authentication是一个模拟对象,它将只会返回null,因此比较将会产生false,但不会有任何有意义的操作。
  • 如果authentication是一个间谍对象,它将返回原始方法的结果,比较结果将取决于那个结果。

我认为你可能尝试去存根化方法的结果。为了实现这个目的,你应该使用&gt;&gt;,如下所示:

given:
authentication.getCredentials() &gt;&gt; &quot;Token&quot;
英文:

This part

given:
authentication.getCredentials() == &quot;Token&quot;

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 return null, hence the comparison will yield false 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 &gt;&gt; as in

given:
authentication.getCredentials() &gt;&gt; &quot;Token&quot;

huangapple
  • 本文由 发表于 2020年10月8日 07:20:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64253617.html
匿名

发表评论

匿名网友

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

确定