当前位置: 首页 > 知识库问答 >
问题:

spock测试中抛出MissingPropertyException,其中块

叶展
2023-03-14

我已经使用spock对我的java项目进行了一段时间的单元测试,但遇到了一个问题。我有一个实用程序方法可以从http请求中获取参数,如果http请求为null并且正在尝试使用spock测试它,则可以获取空字符串。我的测试如下所示:

package foo.bar.test

import foo.bah.HttpRequestPropertyLoader
import spock.lang.Unroll
import javax.servlet.http.HttpServletRequest
import spock.lang.Specification

class HttpRequestPropertyLoaderTest extends Specification {

    HttpRequestPropertyLoader subjectUnderTest
    def result

    def setup() {
        subjectUnderTest = new HttpRequestPropertyLoader()
    }

    @Unroll("When my http request is #nullOrNot then when I get parameter from it the response=#response" )
    def "Test load data from request"() {
        given:
        HttpServletRequest mockHttpRequest = Mock()
        mockHttpRequest.getAttribute("foo") >> "bar"
        when:
        result = subjectUnderTest.loadStringFromHttpRequest(httpRequest, "foo")
        then:
        result == response
        where:
        httpRequest     | response | nullOrNot
        null            |  ""      | "null"
        mockHttpRequest | "bar"    | "not null"
    }
}

但是,当我运行这个测试时,我得到以下错误:

groovy。lang.MissingPropertyException:没有这样的属性:mockHttpRequest for class:foo。酒吧测验HttpRequestPropertyLoaderTest位于foo。酒吧测验HttpRequestPropertyLoaderTest。来自请求的测试加载数据(HttpRequestPropertyLoaderTest.groovy)

在做了一些研究之后,我明白了在给定的块之前运行的块,因此出现了错误,但是我只是想知道是否有一个变通方法?

我知道要使用测试之外的变量,我需要用@Shared注释来注释变量,这对我来说似乎是糟糕的做法。每个测试都应该与其他测试完全分开运行,所以真的不希望有一个对象在测试之间保持它的状态。

是否有可能设置Mock对象从where块返回任何其他方式?

共有1个答案

葛兴发
2023-03-14

按照tim_yates的建议来看https://code.google.com/p/spock/issues/detail?id=15#c4,我发现了一个相当优雅的解决方案,它不需要使用@Shared注释。测试定义现在如下所示:

package foo.bar.test

import foo.bah.HttpRequestPropertyLoader
import spock.lang.Unroll
import javax.servlet.http.HttpServletRequest
import spock.lang.Specification

class HttpRequestPropertyLoaderTest extends Specification {

    HttpRequestPropertyLoader subjectUnderTest
    def result

    def setup() {
        subjectUnderTest = new HttpRequestPropertyLoader()
    }

    @Unroll("When my http request is #nullOrNot then when I get parameter from it the response=#response" )
    def "Test load data from request"() {
        when:
        result = subjectUnderTest.loadStringFromHttpRequest(httpRequest, "foo")
        then:
        result == response
        where:
        httpRequest << {
            HttpServletRequest mockHttpRequest = Mock()
            mockHttpRequest.getAttribute("foo") >> "bar"
            [null, mockHttpRequest]
        }()
        response << ["", "bar"]
        nullOrNot << ["null", "not null"]
    }
}
 类似资料:
  • 我用Spock测试Java代码。我测试这段代码: 我写了一个测试: 它失败是因为抛出了另一个CustomException。但是在块中,我捕获这个异常并抛出一个,因此我希望我的方法将抛出,而不是。如何测试它?

  • 除了统一的HTML或XML报告之外,我如何获得Spock测试执行控制台输出或类似于我的应用程序日志? 使用Gradle2.11和Log4j支持的Slf4j。

  • 由于一个我不知道的事件,调用在我的Ubuntu18.04中不再起作用。(自4月开始运行)。或I阶段尝试的任何其他生命周期都运行良好。 例如,如果我在克隆到中的Apache Commons IO的最新版本中运行,If显示 target/surefire-reports包含以下内容的许多重复 我尝试的任何其他项目都表现出类似的行为。 我尝试下载maven 3.5.4,解压缩它,通过将它添加到路径中,并

  • 我用Robolectric测试写了一个简单的hello-world。 我已经向build.gradle添加了正确的依赖项: 下面是我要测试的简单: 我运行的是Android Studio2.0预览版3B。 问题是:如何避免失败?

  • 我正在为一个处理订单和设置完成的订单准备发货的类编写一些测试。为客户检索订单,并使用已完成订单的订单状态来完成处理。然而,当我运行测试时,它们失败了,说“不能在空对象上获得completedLines...”从错误中可以清楚地看出OrderStatus对象为NULL。但是,我在创建测试时设置了它,如下所示

  • 非常感谢您的帮助:)我将Spock0.6与Groovy1.8一起使用。