Axis2 Test - lukas-krecan/smock GitHub Wiki
Smock has first-class support for Axis2 testing. To be able to use it, you have to add corresponding dependency
<dependency>
<groupId>net.javacrumbs</groupId>
<artifactId>smock-axis2</artifactId>
<version>0.5</version>
<scope>test</scope>
</dependency>
To be able run the test you need to load the Axis 2 configuration and set-up mock service client.
import static net.javacrumbs.smock.axis2.server.SmockServer.*;
import static org.springframework.ws.test.server.ResponseMatchers.*;
...
public class EndpointTest {
private Axis2MockWebServiceClient client;
@Before
public void setUp() throws AxisFault
{
client = createClient(createConfigurationContextFromResource(resource("file:./src/main/webapp/WEB-INF")));
}
}
Once having mock client configured it's possible to call the service and verify the response.
private static final String ENDPOINT_URL = "/axis2/services/CalculatorService";
@Test
public void testSimple() throws Exception {
client.sendRequestTo(ENDPOINT_URL, withMessage("request1.xml")).andExpect(noFault());
}
@Test
public void testCompare() throws Exception {
client.sendRequestTo(ENDPOINT_URL,withMessage("request1.xml"))
.andExpect(message("response1.xml"));
}
To be able to test Axis2 client, we need to create a mock server and connect it to the Axis2. I was not able to find other way than setting ListenerManager.defaultConfigurationContext
with mock TransportSender
s. If you have your own ConfigurationContext
you have to somehow change the TransportSender
s yourself.
import static net.javacrumbs.smock.axis2.client.SmockClient.*;
import static org.springframework.ws.test.client.RequestMatchers.*;
...
public class CalcTest {
private CalcClient calc;
private MockWebServiceServer mockServer;
@Before
public void setUp(){
mockServer = createServer();
//client has to be created after createServer was called.
calc = new CalcClient();
}
}
Once the client is configured, you can use it
@Test
public void testVerifyRequest() throws RemoteException
{
mockServer.expect(message("request1.xml")).andRespond(withMessage("response1.xml"));
long result = calc.plus(1, 2);
assertEquals(3, result);
}