我试图让wiremock在我的单元测试中通过一个简单的请求返回一个200状态,但是,这个单元测试总是返回一个404错误。
如何解决?
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.Assert.assertTrue;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.junit.Rule;
import org.junit.Test;
import java.net.HttpURLConnection;
import java.net.URL;
public class WiremockTest {
@Rule
public WireMockRule wireMockRule = new WireMockRule(8089); // No-args constructor defaults to port 8080
@Test
public void exampleTest() throws Exception {
stubFor(get(urlPathMatching("/my/resource[0-9]+"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/xml")
.withBody("<response>Some content</response>")));
int result = sendGet("http://localhost/my/resource/121");
assertTrue(200 == result);
//verify(getRequestedFor(urlMatching("/my/resource/[a-z0-9]+")));
}
private int sendGet(String url) throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
return responseCode;
}
}
}