Java WireMock:存根-无法实现;断言;方法正确
我正在努力创建一个简单的存根。我一直在关注wiremock在线教程,但在我面临的问题上没有成功。特别是,AssertMethod中的“Get”方法给了我很多问题。我不知道如何解决它Java WireMock:存根-无法实现;断言;方法正确,java,wiremock,Java,Wiremock,我正在努力创建一个简单的存根。我一直在关注wiremock在线教程,但在我面临的问题上没有成功。特别是,AssertMethod中的“Get”方法给了我很多问题。我不知道如何解决它 public class WeatherApplicationTest { @Rule public WireMockRule wireMockRule = new WireMockRule(); public WireMockServer wireMockServer = new Wi
public class WeatherApplicationTest {
@Rule
public WireMockRule wireMockRule = new WireMockRule();
public WireMockServer wireMockServer = new WireMockServer(); //No-args constructor will start on port 8080, no HTTPS
@BeforeClass
public void setUpClass() {
wireMockServer.start();
}
@AfterClass
public void tearDownClass() {
wireMockServer.stop();
}
@Test
public void statusMessage() throws IOException{
wireMockRule.stubFor(get(urlEqualTo("/some/thing"))
.willReturn(aResponse()
.withStatus(200)
.withStatusMessage("Everything is fine")
.withHeader("Content-Type", "Text/Plain")));
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://localhost:" + wireMockServer.port() + "/some/thing");
HttpResponse response = client.execute(request);
assertThat(response.GET("/some/thing").statusCode(), is(200));
}
}
我在以下行中遇到一个错误:
assertThat(response.GET("/some/thing").statusCode(), is(200));
GET方法用红色下划线表示
请帮忙 他们没有很好地勾勒出如何与之联系(但是,在他们的辩护中,这并不是他们真正的责任)
正如我在评论中避而不谈的那样,HttpResponse
不包含GET()
方法,这就是为什么会出现“红色下划线”(即:错误)
因此,我们知道我们正在寻找对状态代码的断言。如果我们查看HttpResponse
类的属性,就可以从HttpResponse
的getStatusLine()
中检索到StatusLine
类,该类的属性表明它包含getStatusCode()
方法。将此信息合并到您的答案中,需要将断言更新为:
assertThat(response.getStatusLine().getStatusCode(), is(200));
此外,正如Tom在评论中指出的那样,删除
WireMockServer
(以及start/stop
和getPort()
调用;您可以从您的规则中获取端口。stubFor(…)
方法应该静态调用(而不是作为规则的一部分).我对这个问题的贡献是:这个Java测试在IDE和Maven中都可以正常运行,而且在其他测试中也可以正常运行,因为最终它会关闭模拟服务器,并且不会与其他测试中运行的其他服务器发生任何冲突
import com.github.tomakehurst.wiremock.WireMockServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import wiremock.org.apache.http.HttpResponse;
import wiremock.org.apache.http.client.HttpClient;
import wiremock.org.apache.http.client.methods.HttpGet;
import wiremock.org.apache.http.impl.client.DefaultHttpClient;
import java.io.IOException;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class KatharsisControllerTest {
private static WireMockServer mockedServer;
private static HttpClient client;
@BeforeClass
public static void setUpClass() {
mockedServer = new WireMockServer();
mockedServer.start();
client = new DefaultHttpClient();
}
@AfterClass
public static void tearDown() {
mockedServer.stop();
}
@Test
public void test_get_all_mock() {
stubFor(get(urlEqualTo("/api/Payment"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/vnd.api+json;charset=UTF-8")
.withStatus(200)
.withBody("")
));
HttpGet request = new HttpGet("http://localhost:8080/api/Payment");
try {
HttpResponse response = client.execute(request);
assert response.getStatusLine().getStatusCode() == 200;
} catch (IOException e) {
e.printStackTrace();
}
}
}
红色表示存在编译时错误(或者,不太可能,您已经设置了正在破坏的编译时限制)。当您实际将鼠标移到红线上(或者检查错误)时,它会说什么?Hi@Ironcache,它基本上表示方法“GET”HTTP响应类中不存在。所以我听一下;
GET
方法是否存在于HttpResponse
中?我相信,您可能想检查,这说明了如何做您想做的事情。为@Ironcache干杯,但我看了一下,但我正在努力将其合并到我的断言方法中。hi@Ironcache,非常感谢但是,当我尝试运行这个JUnit文件时,通过右键单击该文件并选择“测试文件”,我得到了以下错误“预期的是:bur:was”我被定向到以下代码行作为错误源:assertThat(response.getStatusLine().getStatusCode(),is(200));所以您在这里取得了进展。我对该实用程序了解不多,所以我不能在这里提供太多具体的支持,但这意味着,您得到的不是“OK”HTTP响应(200),而是“not Found”HTTP响应(404)。基本上它找不到您的WireMock。需要尝试几件事:1)更改wireMockRule.stubFor(…)
到stubFor(…)(即:不要将其与规则关联);2) 删除未使用的WireMockServer
对象及其启动/停止调用(从WireMockRule
对象获取端口,该对象本身扩展了WireMockServer
)。作为第一点的扩展,请确保导入如下所示:import static com.github.tomakhurst.wiremock.client.wiremock.*代码>谢谢@Ironcahce,你的建议很有帮助!