BAEL-6093 add spring boot 3 url matching code sample (#13744)
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.sample.resources;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
@WebMvcTest(controllers = GreetingsController.class)
|
||||
class GreetingsControllerApiIntegrationTest {
|
||||
|
||||
private static final String BASEURL = "/some";
|
||||
private static final String DEFAULT_MEDIA_TYPE = MediaType.APPLICATION_JSON_VALUE;
|
||||
|
||||
@Autowired
|
||||
MockMvc mvc;
|
||||
|
||||
@Test
|
||||
public void testGreeting() throws Exception {
|
||||
mvc.perform(get(BASEURL + "/greeting").accept(DEFAULT_MEDIA_TYPE))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string("Hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled("Disabled while TrailingSlashRedirectFilter is in use.")
|
||||
public void testGreetingTrailingSlash() throws Exception {
|
||||
mvc.perform(get(BASEURL + "/greeting/").accept(DEFAULT_MEDIA_TYPE))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string("Hello with slash"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGreetingTrailingSlashWithFilter() throws Exception {
|
||||
mvc.perform(get(BASEURL + "/greeting/").accept(DEFAULT_MEDIA_TYPE))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string("Hello"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.sample.resources;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@WebFluxTest
|
||||
public class GreetingsControllerReactiveApiIntegrationTest {
|
||||
private static final String BASEURL = "/some/reactive";
|
||||
@Autowired
|
||||
private WebTestClient webClient;
|
||||
@Test
|
||||
public void testGreeting() {
|
||||
webClient.get().uri( BASEURL + "/greeting")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody().consumeWith(result -> {
|
||||
String responseBody = new String(result.getResponseBody());
|
||||
assertTrue(responseBody.contains("Hello reactive"));
|
||||
});
|
||||
}
|
||||
@Test
|
||||
@Disabled("Disabled while TrailingSlashRedirectFilter is in use.")
|
||||
public void testGreetingTrailingSlash() {
|
||||
webClient.get().uri(BASEURL + "/greeting/")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody().consumeWith(result -> {
|
||||
String responseBody = new String(result.getResponseBody());
|
||||
assertTrue(responseBody.contains("Hello with slash reactive"));
|
||||
});
|
||||
}
|
||||
@Test
|
||||
public void testGreetingTrailingSlashWithFilter() {
|
||||
webClient.get().uri(BASEURL + "/greeting/")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody().consumeWith(result -> {
|
||||
String responseBody = new String(result.getResponseBody());
|
||||
assertTrue(responseBody.contains("Hello reactive"));
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user