MockRestServiceServer test for follow-up request after failure

Issue: SPR-16132
This commit is contained in:
Juergen Hoeller
2017-10-30 16:40:20 +01:00
parent 3c07afc5be
commit 295e3b6a99
4 changed files with 58 additions and 42 deletions

View File

@@ -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");
* you may not use this file except in compliance with the License.
@@ -16,25 +16,29 @@
package org.springframework.test.web.client;
import java.net.SocketException;
import org.junit.Test;
import org.springframework.test.web.client.MockRestServiceServer.MockRestServiceServerBuilder;
import org.springframework.web.client.RestTemplate;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
import static org.springframework.http.HttpMethod.*;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
import static org.springframework.test.web.client.response.MockRestResponseCreators.*;
/**
* Unit tests for {@link MockRestServiceServer}.
*
* @author Rossen Stoyanchev
*/
public class MockRestServiceServerTests {
private RestTemplate restTemplate = new RestTemplate();
private final RestTemplate restTemplate = new RestTemplate();
@Test
public void buildMultipleTimes() throws Exception {
public void buildMultipleTimes() {
MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(this.restTemplate);
MockRestServiceServer server = builder.build();
@@ -56,7 +60,7 @@ public class MockRestServiceServerTests {
}
@Test(expected = AssertionError.class)
public void exactExpectOrder() throws Exception {
public void exactExpectOrder() {
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate)
.ignoreExpectOrder(false).build();
@@ -66,7 +70,7 @@ public class MockRestServiceServerTests {
}
@Test
public void ignoreExpectOrder() throws Exception {
public void ignoreExpectOrder() {
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate)
.ignoreExpectOrder(true).build();
@@ -78,7 +82,7 @@ public class MockRestServiceServerTests {
}
@Test
public void resetAndReuseServer() throws Exception {
public void resetAndReuseServer() {
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate).build();
server.expect(requestTo("/foo")).andRespond(withSuccess());
@@ -92,7 +96,7 @@ public class MockRestServiceServerTests {
}
@Test
public void resetAndReuseServerWithUnorderedExpectationManager() throws Exception {
public void resetAndReuseServerWithUnorderedExpectationManager() {
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate)
.ignoreExpectOrder(true).build();
@@ -108,4 +112,24 @@ public class MockRestServiceServerTests {
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();
}
}

View File

@@ -28,21 +28,15 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.mock.http.client.MockClientHttpRequest;
import static org.junit.Assert.assertEquals;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.test.util.AssertionErrors.fail;
import static org.springframework.test.web.client.ExpectedCount.max;
import static org.springframework.test.web.client.ExpectedCount.min;
import static org.springframework.test.web.client.ExpectedCount.once;
import static org.springframework.test.web.client.ExpectedCount.times;
import static org.springframework.test.web.client.ExpectedCount.twice;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
import static org.junit.Assert.*;
import static org.springframework.http.HttpMethod.*;
import static org.springframework.test.web.client.ExpectedCount.*;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
import static org.springframework.test.web.client.response.MockRestResponseCreators.*;
/**
* Unit tests for {@link SimpleRequestExpectationManager}.
*
* @author Rossen Stoyanchev
*/
public class SimpleRequestExpectationManagerTests {
@@ -188,16 +182,17 @@ public class SimpleRequestExpectationManagerTests {
@Test // SPR-16132
public void sequentialRequestsWithFirstFailing() throws Exception {
this.manager.expectRequest(once(), requestTo("/foo")).andExpect(method(GET))
.andRespond(request -> { throw new SocketException("pseudo network error"); });
this.manager.expectRequest(once(), requestTo("/handle-error")).andExpect(method(POST)).andRespond(withSuccess());
this.manager.expectRequest(once(), requestTo("/foo")).
andExpect(method(GET)).andRespond(request -> { throw new SocketException("pseudo network error"); });
this.manager.expectRequest(once(), requestTo("/handle-error")).
andExpect(method(POST)).andRespond(withSuccess());
try {
this.manager.validateRequest(createRequest(GET, "/foo"));
fail("expected exception");
fail("Expected SocketException");
}
catch (SocketException e) {
//expected
catch (SocketException ex) {
// expected
}
this.manager.validateRequest(createRequest(POST, "/handle-error"));
this.manager.verify();

View File

@@ -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");
* you may not use this file except in compliance with the License.
@@ -26,18 +26,15 @@ import org.junit.rules.ExpectedException;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import static org.junit.Assert.assertEquals;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.test.web.client.ExpectedCount.max;
import static org.springframework.test.web.client.ExpectedCount.min;
import static org.springframework.test.web.client.ExpectedCount.once;
import static org.springframework.test.web.client.ExpectedCount.twice;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
import static org.junit.Assert.*;
import static org.springframework.http.HttpMethod.*;
import static org.springframework.test.web.client.ExpectedCount.*;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
import static org.springframework.test.web.client.response.MockRestResponseCreators.*;
/**
* Unit tests for {@link UnorderedRequestExpectationManager}.
*
* @author Rossen Stoyanchev
*/
public class UnorderedRequestExpectationManagerTests {
@@ -131,4 +128,5 @@ public class UnorderedRequestExpectationManagerTests {
throw new IllegalStateException(ex);
}
}
}