SWS-544 - Add test framework for Spring WS client
This commit is contained in:
@@ -25,8 +25,14 @@ import java.util.List;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.ws.transport.WebServiceMessageSender;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Mock implementation of {@link WebServiceMessageSender}. Contains a list of expected {@link MockSenderConnection}s,
|
||||
* and iterates over those.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Lukas Krecan
|
||||
* @since 2.0
|
||||
*/
|
||||
class MockWebServiceMessageSender implements WebServiceMessageSender {
|
||||
|
||||
private final List<MockSenderConnection> expectedConnections = new LinkedList<MockSenderConnection>();
|
||||
@@ -38,7 +44,9 @@ class MockWebServiceMessageSender implements WebServiceMessageSender {
|
||||
if (connectionIterator == null) {
|
||||
connectionIterator = expectedConnections.iterator();
|
||||
}
|
||||
assertTrue("No further connections expected", connectionIterator.hasNext());
|
||||
if (!connectionIterator.hasNext()) {
|
||||
throw new AssertionError("No further connections expected");
|
||||
}
|
||||
|
||||
MockSenderConnection currentConnection = connectionIterator.next();
|
||||
currentConnection.setUri(uri);
|
||||
|
||||
@@ -32,10 +32,18 @@ import org.springframework.ws.transport.WebServiceMessageSender;
|
||||
class ThreadLocalMockWebServiceMessageSender implements WebServiceMessageSender {
|
||||
|
||||
public WebServiceConnection createConnection(URI uri) throws IOException {
|
||||
return MockWebServiceMessageSenderHolder.get().createConnection(uri);
|
||||
return getMock().createConnection(uri);
|
||||
}
|
||||
|
||||
public boolean supports(URI uri) {
|
||||
return MockWebServiceMessageSenderHolder.get().supports(uri);
|
||||
return getMock().supports(uri);
|
||||
}
|
||||
|
||||
private MockWebServiceMessageSender getMock() {
|
||||
MockWebServiceMessageSender mock = MockWebServiceMessageSenderHolder.get();
|
||||
if (mock == null) {
|
||||
throw new AssertionError("No further connections expected");
|
||||
}
|
||||
return mock;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,6 +153,17 @@ public class WebServiceMockTest {
|
||||
template.sendSourceAndReceiveToResult(new StringSource(request), new StringResult());
|
||||
assertNull(MockWebServiceMessageSenderHolder.get());
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void unexpectedConnection() throws Exception {
|
||||
String request = "<request xmlns='http://example.com'/>";
|
||||
String response = "<response xmlns='http://example.com'/>";
|
||||
|
||||
expect(payload(request)).andRespond(withPayload(response));
|
||||
|
||||
template.sendSourceAndReceiveToResult(new StringSource(request), new StringResult());
|
||||
template.sendSourceAndReceiveToResult(new StringSource(request), new StringResult());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user