Groovy测试中的动态WireMock服务器-Junit 5

js81xvg6  于 2022-12-03  发布在  其他
关注(0)|答案(1)|浏览(160)

关于文章:https://wiremock.org/docs/junit-jupiter/
我试着做我自己的服务器:

@RegisterExtension
    static WireMockExtension wm1 = WireMockExtension.newInstance()
            .options(wireMockConfig().dynamicPort().dynamicHttpsPort())
            .build();

但我将未知方法wireMockConfig()更改为new WireMockConfiguration()
groovy测试中,我得到一个错误Method threw 'groovy.lang.MissingPropertyException' exception.属性为getWm1
此问题是否与spockwiremock集成有关?
钢丝钻2.33.0
斯波克-核心-2.2-凹槽-3.0
Spring引导版本=2.7.4
spring云版本=2021.0.3
也许这段代码在集成测试中不起作用?
编辑:我丢失了import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; IntelliJ没有自动找到这个导入。但是这个代码做的完全一样-new WireMockConfiguration()

stszievb

stszievb1#

除了我的评论之外,我还看到了您的@RegisterExtension。请注意,Spock 2.x测试引擎是JUnit 5平台上的一个单独的测试引擎,它运行在Jupiter引擎之上。Spock是Jupiter的兄弟而不是子引擎。因此,您不能简单地使用Jupiter特定的注解或扩展。您更应该这样做。因为WireMock does not provide its own Spock 2.x extension

package de.scrum_master.stackoverflow.q74613761

import com.github.tomakehurst.wiremock.WireMockServer
import spock.lang.Shared
import spock.lang.Specification

import static com.github.tomakehurst.wiremock.client.WireMock.*
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options

class WireMockTest extends Specification {
  @Shared
  WireMockServer wiremockServer

  def setupSpec() {
    wiremockServer = new WireMockServer(options().dynamicHttpsPort())
    wiremockServer.start()
  }

  def cleanupSpec() {
    wiremockServer.stop()
  }

  def cleanup() {
    wiremockServer.resetAll()
  }

  def test() {
    stubFor(get("/api-1-thing").willReturn(ok()))
    stubFor(get("/api-2-stuff").willReturn(ok()))

    expect:
    wiremockServer.httpsPort()
  }
}

但是,有一个第三方Spock扩展,它使您能够使用Jupiter注解和Jupiter扩展。我没有任何使用它的经验,但它似乎适用于WireMock:

package de.scrum_master.stackoverflow.q74613761

import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer
import com.github.tomakehurst.wiremock.junit5.WireMockExtension
import org.junit.jupiter.api.extension.RegisterExtension
import spock.lang.Specification

import static com.github.tomakehurst.wiremock.client.WireMock.get
import static com.github.tomakehurst.wiremock.client.WireMock.ok
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig

/**
 * Needs <ul>
 *   <li><tt>com.github.tomakehurst:wiremock-jre8</tt>,</li>
 *   <li><tt>org.junit.jupiter:junit-jupiter-api</tt> in version fitting with Spock and WireMock,</li>
 *   <li><a href="https://github.com/xvik/spock-junit5"><tt>ru.vyarus:spock-junit5</tt></a> for being able to use Jupiter extensions in Spock 2.x.</li>
 * <ul>
 */
class WireMockJupiterExtensionTest extends Specification {
  @RegisterExtension
  static WireMockExtension wm1 = WireMockExtension.newInstance()
    .options(wireMockConfig().dynamicPort().dynamicHttpsPort())
    .build()
  @RegisterExtension
  static WireMockExtension wm2 = WireMockExtension.newInstance()
    .options(wireMockConfig().dynamicPort().extensions(new ResponseTemplateTransformer(true)))
    .build()

  def test() {
    wm1.stubFor(get("/api-1-thing").willReturn(ok()))
    wm2.stubFor(get("/api-2-stuff").willReturn(ok()))

    expect:
    wm1.runtimeInfo.httpsPort
    wm1.port
  }
}

我建议不要依赖于Jupiter特定的东西,将其拉入Spock,只要有好的替代方案,就像在这种情况下,只需自己设置WireMock。您也可以将配置放入一个基础规范中,并为您的WireMock规范扩展该基础规范。

相关问题