我正在使用一个mockserver来测试API请求。
模拟服务器无法启动。
这是我的代码
@SpringBootTest
class RestApiMetaDataApplicationTests {
private ClientAndServer mockServer;
@BeforeClass
public void startServer() throws Exception {
mockServer = startClientAndServer(1080);
}
@AfterClass
public void stopServer() {
mockServer.stop();
}
@Test
public void shouldReturnTrack() throws Exception {
String isrc = "TEST1234560000";
String responseBody = "{\"id\": \"1234\",\"name\": \"Some new track!\",\"duration_ms\": 12000000}";
new MockServerClient("localhost", 1080)
.when(
request()
.withMethod("GET")
.withPath("/codechallenge/createTrack/" + isrc)
)
.respond(
response()
.withStatusCode(200)
.withBody(responseBody)
.withHeader("Content-Type", "application/json")
);
HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest httpRequest = HttpRequest.newBuilder()
.GET()
.uri(URI.create("http://localhost:1080/codechallenge/createTrack/" + isrc))
.header("Content-Type", "application/json")
.build();
HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
String response = httpResponse.body();
assertEquals(responseBody, response);
assertEquals(200, httpResponse.statusCode());
}}
端口1080上没有运行任何东西,我试过其他端口,没有一个工作。
我在Github上发现了其他人遇到同样问题的帖子,但没有一个解决方案对我有效
link to github
我使用的依赖项是
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
<version>5.11.2</version>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-client-java</artifactId>
<version>5.11.2</version>
<scope>test</scope>
</dependency>
这是一些stacktrace
org.mockserver.client.SocketConnectionException: Unable to connect to socket localhost/127.0.0.1:1080
at org.mockserver.client.NettyHttpClient.sendRequest(NettyHttpClient.java:180)
at org.mockserver.client.MockServerClient.sendRequest(MockServerClient.java:211)
at org.mockserver.client.MockServerClient.sendRequest(MockServerClient.java:244)
at org.mockserver.client.MockServerClient.upsert(MockServerClient.java:1109)
at org.mockserver.client.ForwardChainExpectation.respond(ForwardChainExpectation.java:50)
at net.codejava.song.RestApiMetaDataApplicationTests.shouldReturnTrack(RestApiMetaDataApplicationTests.java:64)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:727)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:156)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:147)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:86)
at
增加了mockserver的插件导入
<plugin>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-maven-plugin</artifactId>
<version>3.10.8</version>
<configuration>
<serverPort>1080</serverPort>
<proxyPort>1090</proxyPort>
<logLevel>DEBUG</logLevel>
<initializationClass>org.mockserver.maven.ExampleInitializationClass</initializationClass>
</configuration>
<executions>
<execution>
<id>process-test-classes</id>
<phase>process-test-classes</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<phase>verify</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
1条答案
按热度按时间2wnc66cl1#
它失败的原因是你没有启动模拟服务器,在测试时,它会失败,因为在那个端口没有启动任何东西,我假设你希望它在测试前自动启动服务器,因此有两个选项:
第一步:将测试任务配置为依赖于
moctserver:run
任务第二步:当运行测试任务时,在运行测试任务之前运行模拟服务器。查看official docs了解更多信息