Testing Web Services

Harry · 11 Sep 2026 · 13 views

Testing Web Services

Thorough testing is critical for web services. You need unit tests, integration tests, and contract tests. This post covers practical testing strategies for both REST and SOAP services.

Testing with MockMvc

MockMvc tests your controllers in isolation without starting a server. It is fast and catches most logic errors.

@SpringBootTest
@AutoConfigureMockMvc
public class ProductControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    void shouldReturnAllProducts() throws Exception {
        mockMvc.perform(get("/api/products")
                .contentType(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(jsonPath("$.content").isArray())
            .andExpect(jsonPath("$.content.length()").value(3));
    }

    @Test
    void shouldReturn404ForMissingProduct() throws Exception {
        mockMvc.perform(get("/api/products/9999"))
            .andExpect(status().isNotFound());
    }
}

Integration Tests with TestRestTemplate

Integration tests start the full application and make real HTTP requests. They verify the entire stack including filters, serialization, and error handling.

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ProductIntegrationTest {

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    void shouldCreateAndRetrieveProduct() {
        Product product = new Product(null, "Widget", 9.99, "Tools");

        ResponseEntity<Product> created = restTemplate.postForEntity(
            "/api/products", product, Product.class);
        assertThat(created.getStatusCode()).isEqualTo(HttpStatus.CREATED);
        assertThat(created.getBody().getId()).isNotNull();

        ResponseEntity<Product> retrieved = restTemplate.getForEntity(
            "/api/products/" + created.getBody().getId(), Product.class);
        assertThat(retrieved.getBody().getName()).isEqualTo("Widget");
    }
}

Testing SOAP with WebServiceTemplate

Spring-WS provides WebServiceTemplate for testing SOAP services:

@Autowired
private WebServiceTemplate webServiceTemplate;

@Test
void shouldCallOrderService() {
    CreateOrderRequest request = new CreateOrderRequest();
    request.setProductName("Widget");
    request.setQuantity(5);

    CreateOrderResponse response = (CreateOrderResponse)
        webServiceTemplate.marshalSendAndReceive(request);
    assertThat(response.getOrder().getStatus()).isEqualTo("PENDING");
}

Contract Testing

Use Pact or Spring Cloud Contract to verify that your API matches its documented contract. This catches breaking changes before they reach clients.

Key Points

  • MockMvc tests controllers in isolation without starting a server.
  • Integration tests start the full application and make real HTTP calls.
  • Test both success paths and error scenarios (404, 400, 500).
  • WebServiceTemplate tests SOAP services with Java objects.
  • Contract tests verify your API matches its documented specification.
Share this post:

Comments (0)

Please login or register to comment.