Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions java/developing-applications/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,11 @@ For a more extensive version of the previous `CatalogServiceTest` snippets, have

Integration tests enable us to verify the behavior of a custom event handler execution doing a roundtrip starting at the protocol adapter layer and going through the whole CAP architecture until it reaches the service and event handler layer and then back again through the protocol adapter.

As the services defined in our `CDS model` are exposed as `OData` endpoints, by using [MockMvc](https://docs.spring.io/spring-framework/reference/testing/mockmvc.html) we can simply invoke a specific `OData` request and assert the response from the addressed service.
As the services defined in our `CDS model` are exposed as `OData` endpoints, we can simply invoke a specific `OData` request and assert the response from the addressed service. Spring provides two clients for this purpose: [MockMvc](https://docs.spring.io/spring-framework/reference/testing/mockmvc.html) and, since Spring Framework 7.0 (Spring Boot 4.x), the [RestTestClient](https://docs.spring.io/spring-framework/reference/testing/resttestclient.html). Both approaches perform an equivalent server-side roundtrip through the whole CAP architecture without starting a servlet container, so you can pick whichever fits your style — there's no preference. However, with RestTestClient you can also switch to real HTTP communication for either testing your complete HTTP layer or remote services.

The following demonstrates this by invoking a `GET` request to the `OData` endpoint of our `Books` entity, which triggers the execution of the `discountBooks` method of the `CatalogServiceHandler` in our example:
The following demonstrates this by invoking a `GET` request to the `OData` endpoint of our `Books` entity, which triggers the execution of the `discountBooks` method of the `CatalogServiceHandler` in our example.

#### Using MockMvc

```java
@SpringBootTest
Expand Down Expand Up @@ -247,6 +249,42 @@ public class CatalogServiceITest {
}
```

#### Using RestTestClient

The same test can be written with `RestTestClient`, which offers a fluent, [WebTestClient](https://docs.spring.io/spring-framework/reference/testing/webtestclient.html)-style API:

```java
@SpringBootTest
@AutoConfigureRestTestClient
public class CatalogServiceITest {

private static final String booksURI = "/api/browse/Books";

@Autowired
private RestTestClient client;

@Test
public void discountApplied() {
client.get().uri(booksURI + "?$filter=stock gt 200&top=1")
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.value[0].title")
.value(String.class, title -> assertThat(title).contains("11% discount"));
}

@Test
public void discountNotApplied() {
client.get().uri(booksURI + "?$filter=stock lt 100&top=1")
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.value[0].title")
.value(String.class, title -> assertThat(title).doesNotContain("11% discount"));
}
}
```

::: tip
Check out the version in our [CAP Java bookshop sample project](https://github.com/SAP-samples/cloud-cap-samples-java/blob/main/srv/src/test/java/my/bookshop/CatalogServiceITest.java) for additional examples of integration testing.
:::
Expand Down
Loading