SWS-544 - Add test framework for Spring WS client

This commit is contained in:
Arjen Poutsma
2010-07-14 11:50:22 +00:00
parent 18025fd558
commit 61af77d06e
3 changed files with 32 additions and 5 deletions

View File

@@ -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);

View File

@@ -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;
}
}

View File

@@ -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());
}
}