2017-06-29 60 views
0

我刚开始使用Wiremock,我对stubbing有个疑问。Wiremock Stubbing不工作

从文档看来,您可以使用映射下的JSON文件或Java代码中的代码stubFor(get(urlEqualTo(...。但是,我发现使用stubFor(get(urlEqualTo(结果显示在Wiremock控制台中的“请求未匹配”消息。

这是正确的行为?存根是否需要代码和json文件?

谢谢。

回答

0

WireMock只能在映射中使用JSON有效载荷。听起来你的配置还有其他的东西,但我需要更多的细节来诊断。

1

不,wiremock只能使用.json文件或只能使用java代码。 如果需要,您可以将其组合。

当请求不匹配时,那么该URL不正确桩。 如果您使用的是独立流程,您可以使用--verbose来启动它,以查找请求未匹配的详细信息。

0

没有必要。我已经尝试了下面的代码,它对我有用:

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; 
import static com.github.tomakehurst.wiremock.client.WireMock.get; 
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; 
import com.github.tomakehurst.wiremock.WireMockServer; 

public class WireMockTest { 
    public static void main(String[] args) throws InterruptedException { 
     WireMockServer wireMockServer1 = new WireMockServer(); 
     wireMockServer1.start(); 
     wireMockServer1.stubFor(get(urlEqualTo("/testWireMock")) 
         .willReturn(aResponse().withHeader("Content-Type", "text/plain") 
         .withStatus(200).withBody("Welcome to WireMock!"))); 
     System.out.println("Server started"); 
     Thread.sleep(1000); 
     wireMockServer1.stop(); 
    } 
}