IKo*_*IKo 0 java testing junit localhost spring-boot
我正在测试 spring-boot 应用程序并想检查简单的获取请求。问题是,如果我用我们的开发服务器替换 localhost 一切正常。但不是本地主机。
这是父测试类:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyServiceStarter.class)
public abstract class AbstractModulIntegrationTest { ... }
Run Code Online (Sandbox Code Playgroud)
问题:
@Test
public void testGetRequest() {
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials
= new UsernamePasswordCredentials("user", "pass");
provider.setCredentials(AuthScope.ANY, credentials);
HttpClient httpClient = HttpClientBuilder.create()
.setDefaultCredentialsProvider(provider)
.build();
HttpGet request = new HttpGet("http://localhost:9000/abc/v1/users/001");
HttpResponse response = null;
try {
response = httpClient.execute(request); //HERE IT FAILS
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("RETURN CODE:" + response.getStatusLine().getStatusCode());
}
Run Code Online (Sandbox Code Playgroud)
跟踪:
org.apache.http.conn.HttpHostConnectException: Connect to localhost:9000 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:159)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
...
Caused by: java.net.ConnectException: Connection refused: connect
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏。
@SpringBootTest默认情况下不启动 Web 服务器。您可以使用webEnvironment属性要求它在定义的端口或随机端口上启动
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyServiceStarter.class, webEnvironment = RANDOM_PORT)
public abstract class AbstractModulIntegrationTest { ... }
Run Code Online (Sandbox Code Playgroud)
您可以在测试中注入服务器的实际端口,如下所示:
@LocalServerPort
private int port;
public void testGetRequest() { ... }
Run Code Online (Sandbox Code Playgroud)
如果您想在定义的端口上启动,您可能已将应用程序配置为在端口 9000 ( server.port=9000)上启动。那么你应该这样做:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyServiceStarter.class, webEnvironment = DEFINED_PORT)
public abstract class AbstractModulIntegrationTest { ... }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4942 次 |
| 最近记录: |