RestTemplate测试因ResourceAccessException而失败

时间:2014-10-07 16:10:49

标签: unit-testing maven exception spring-boot resttemplate

我分叉了这个项目:

https://github.com/soudmaijer/drools

这里:

https://github.com/fastnsilver/drools

我将一些POM依赖项更新为更新,我使用RestTemplate添加了一个客户端测试,该测试应该用于FraudController

当我执行名为FraudControllerClientTest的测试时,我得到ResourceAccessException

查看此跟踪

Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 4.486 sec <<< FAILURE! - in com.oudmaijer.drools.fraud.FraudControllerClientTest
thatOrderSucceeds(com.oudmaijer.drools.fraud.FraudControllerClientTest)  Time elapsed: 0.251 sec  <<< ERROR!
org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:8080/fraud/check/order":Connection reset; nested exception is java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:136)
at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:152)
at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:270)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:140)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:260)
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:161)
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:153)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:271)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:254)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:91)
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:568)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:537)
at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:365)
at com.oudmaijer.drools.fraud.FraudControllerClientTest.thatOrderSucceeds(FraudControllerClientTest.java:41)

测试......

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = FraudApplication.class)
@WebAppConfiguration
@IntegrationTest
public class FraudControllerClientTest {

@Value("${env.baseUrl}")
private String baseUrl;

@Test
public void thatOrderSucceeds() {
    String uri = String.format("%s%s%s", baseUrl, Routes.BASE, Routes.ORDER);
    RestTemplate rest = new TestRestTemplate();
    ResponseEntity<Errors> response =
                    rest.postForEntity(uri, TestFixture.standardOrder(), Errors.class, Collections.EMPTY_MAP);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    Errors errors = response.getBody();
    assertFalse(errors.hasErrors());
}

@Test
public void thatOrderFails() {
    String uri = String.format("%s%s%s", baseUrl, Routes.BASE, Routes.ORDER);
    RestTemplate rest = new TestRestTemplate();
    ResponseEntity<Errors> response =
                    rest.postForEntity(uri, TestFixture.suspiciousOrder(), Errors.class, Collections.EMPTY_MAP);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    Errors errors = response.getBody();
    assertTrue(errors.hasErrors());
}

private static class TestFixture {

    static Order standardOrder() {
        return new Order("1", new Seller(5), new Address("456XYZ", 66));
    }

    static Order suspiciousOrder() {
        return new Order("1", new Seller(3), new Address("3452RK", 53));
    }
}
}

我有什么遗失的东西吗?我无法使用RestTemplate测试真正部署的Spring Boot应用程序吗?

1 个答案:

答案 0 :(得分:1)

当我启用此虚拟机-Djava.net.preferIPv4Stack=true时,我得到一个&#34;地址已在使用&#34;。接下来,我运行sudo lsof -i -P | grep -i "listen"以查看我收听的内容。你猜怎么着?我有一台虚拟机启动并运行暴露端口8080.杀死了虚拟机。重新进行测试。瞧瞧。它奏效了!