2014-10-07 110 views
0

我分叉这个项目:RestTemplate测试失败,ResourceAccessException

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

原来,当我启用此虚拟机参数-Djava.net.preferIPv4Stack=true,我得到了“地址已被使用”。接下来我跑了sudo lsof -i -P | grep -i "listen"看看我有什么端口在听。你猜怎么了?我有一台虚拟机启动并运行暴露端口8080.杀死虚拟机。重新运行测试。 Et瞧。有效!