|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2016 the original author or authors. |
|
|
|
* Copyright 2002-2017 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
@ -16,25 +16,29 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.test.web.client; |
|
|
|
package org.springframework.test.web.client; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.net.SocketException; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.test.web.client.MockRestServiceServer.MockRestServiceServerBuilder; |
|
|
|
import org.springframework.test.web.client.MockRestServiceServer.MockRestServiceServerBuilder; |
|
|
|
import org.springframework.web.client.RestTemplate; |
|
|
|
import org.springframework.web.client.RestTemplate; |
|
|
|
|
|
|
|
|
|
|
|
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; |
|
|
|
import static org.springframework.http.HttpMethod.*; |
|
|
|
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; |
|
|
|
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*; |
|
|
|
|
|
|
|
import static org.springframework.test.web.client.response.MockRestResponseCreators.*; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Unit tests for {@link MockRestServiceServer}. |
|
|
|
* Unit tests for {@link MockRestServiceServer}. |
|
|
|
|
|
|
|
* |
|
|
|
* @author Rossen Stoyanchev |
|
|
|
* @author Rossen Stoyanchev |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class MockRestServiceServerTests { |
|
|
|
public class MockRestServiceServerTests { |
|
|
|
|
|
|
|
|
|
|
|
private RestTemplate restTemplate = new RestTemplate(); |
|
|
|
private final RestTemplate restTemplate = new RestTemplate(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void buildMultipleTimes() throws Exception { |
|
|
|
public void buildMultipleTimes() { |
|
|
|
MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(this.restTemplate); |
|
|
|
MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(this.restTemplate); |
|
|
|
|
|
|
|
|
|
|
|
MockRestServiceServer server = builder.build(); |
|
|
|
MockRestServiceServer server = builder.build(); |
|
|
@ -56,7 +60,7 @@ public class MockRestServiceServerTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = AssertionError.class) |
|
|
|
@Test(expected = AssertionError.class) |
|
|
|
public void exactExpectOrder() throws Exception { |
|
|
|
public void exactExpectOrder() { |
|
|
|
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate) |
|
|
|
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate) |
|
|
|
.ignoreExpectOrder(false).build(); |
|
|
|
.ignoreExpectOrder(false).build(); |
|
|
|
|
|
|
|
|
|
|
@ -66,7 +70,7 @@ public class MockRestServiceServerTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void ignoreExpectOrder() throws Exception { |
|
|
|
public void ignoreExpectOrder() { |
|
|
|
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate) |
|
|
|
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate) |
|
|
|
.ignoreExpectOrder(true).build(); |
|
|
|
.ignoreExpectOrder(true).build(); |
|
|
|
|
|
|
|
|
|
|
@ -78,7 +82,7 @@ public class MockRestServiceServerTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void resetAndReuseServer() throws Exception { |
|
|
|
public void resetAndReuseServer() { |
|
|
|
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate).build(); |
|
|
|
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate).build(); |
|
|
|
|
|
|
|
|
|
|
|
server.expect(requestTo("/foo")).andRespond(withSuccess()); |
|
|
|
server.expect(requestTo("/foo")).andRespond(withSuccess()); |
|
|
@ -92,7 +96,7 @@ public class MockRestServiceServerTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void resetAndReuseServerWithUnorderedExpectationManager() throws Exception { |
|
|
|
public void resetAndReuseServerWithUnorderedExpectationManager() { |
|
|
|
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate) |
|
|
|
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate) |
|
|
|
.ignoreExpectOrder(true).build(); |
|
|
|
.ignoreExpectOrder(true).build(); |
|
|
|
|
|
|
|
|
|
|
@ -108,4 +112,24 @@ public class MockRestServiceServerTests { |
|
|
|
server.verify(); |
|
|
|
server.verify(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test // SPR-16132
|
|
|
|
|
|
|
|
public void followUpRequestAfterFailure() { |
|
|
|
|
|
|
|
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate).build(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
server.expect(requestTo("/some-service/some-endpoint")) |
|
|
|
|
|
|
|
.andRespond(request -> { throw new SocketException("pseudo network error"); }); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
server.expect(requestTo("/reporting-service/report-error")) |
|
|
|
|
|
|
|
.andExpect(method(POST)).andRespond(withSuccess()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
this.restTemplate.getForEntity("/some-service/some-endpoint", String.class); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (Exception ex) { |
|
|
|
|
|
|
|
this.restTemplate.postForEntity("/reporting-service/report-error", ex.toString(), String.class); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
server.verify(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|