Я использую макетный сервер для проверки запросов 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 httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
String response = httpResponse.body();
assertEquals(responseBody, response);
assertEquals(200, httpResponse.statusCode());
}}
На порту 1080 ничего не работает, и я пробовал другие порты, и ни один из них не работает.
Я нашел эту тему на Github на другом сайте. люди столкнулись с одной и той же проблемой, но ни одно из предложенных решений мне не помогло.
ссылка на github
Я использую следующие зависимости:
org.mock-server
mockserver-netty
5.11.2
org.mock-server
mockserver-client-java
5.11.2
test
Это часть трассировки стека
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
добавлен импорт плагинов для макетного сервера
org.mock-server
mockserver-maven-plugin
3.10.8
1080
1090
DEBUG
org.mockserver.maven.ExampleInitializationClass
process-test-classes
process-test-classes
start
verify
verify
stop
Подробнее здесь: https://stackoverflow.com/questions/759 ... o-any-port
Mockserver не может подключиться ни к одному порту ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Невозможно подключиться к порту Arduino с помощью Visual Studio (Доступ к порту COM4 запрещен)
Anonymous » » в форуме C# - 0 Ответы
- 104 Просмотры
-
Последнее сообщение Anonymous
-