Bael 6131 (#15967)
* chore: Simple test setup * chore: Authorization error * feat: Removed using page parameter * feat: Added MockMvc test * feat: Added MockUser * feat: Removed unused import * feat: WebClientTest documentation * feat: Working tests for WebMvc and WebTestClient * feat: Working tests with RestAssured * feat: Cleanup * feat: Added a BookService * feat: Suppressed a warning * feat: Fixed typo
This commit is contained in:
parent
e6be71cd84
commit
c1fe1fd285
@ -37,6 +37,10 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-hateoas</artifactId>
|
<artifactId>spring-boot-starter-hateoas</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.json.bind</groupId>
|
<groupId>javax.json.bind</groupId>
|
||||||
<artifactId>javax.json.bind-api</artifactId>
|
<artifactId>javax.json.bind-api</artifactId>
|
||||||
@ -96,6 +100,12 @@
|
|||||||
<artifactId>spring-restdocs-restassured</artifactId>
|
<artifactId>spring-restdocs-restassured</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.restdocs</groupId>
|
||||||
|
<artifactId>spring-restdocs-webtestclient</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.zaxxer</groupId>
|
<groupId>com.zaxxer</groupId>
|
||||||
<artifactId>HikariCP</artifactId>
|
<artifactId>HikariCP</artifactId>
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.baeldung.queryparamdoc;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||||
|
|
||||||
|
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
|
||||||
|
public class Application {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Application.class, args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.baeldung.queryparamdoc;
|
||||||
|
|
||||||
|
public class Book {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.baeldung.queryparamdoc;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/books")
|
||||||
|
public class BookController {
|
||||||
|
|
||||||
|
private final BookService service;
|
||||||
|
|
||||||
|
public BookController(BookService service) {
|
||||||
|
this.service = service;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public List<Book> getBooks(@RequestParam(name = "page") Integer page) {
|
||||||
|
return service.getBooks(page);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.queryparamdoc;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class BookService {
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public List<Book> getBooks(Integer page) {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
package com.baeldung.queryparamdoc;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
|
||||||
|
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
|
||||||
|
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
|
||||||
|
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
|
||||||
|
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
|
||||||
|
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
|
||||||
|
import static org.springframework.restdocs.request.RequestDocumentation.requestParameters;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||||
|
import org.springframework.restdocs.RestDocumentationContextProvider;
|
||||||
|
import org.springframework.restdocs.RestDocumentationExtension;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
|
@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
|
||||||
|
@WebMvcTest(controllers = {BookController.class, BookService.class},
|
||||||
|
excludeAutoConfiguration = SecurityAutoConfiguration.class)
|
||||||
|
class BookControllerMvcIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp(WebApplicationContext webApplicationContext, RestDocumentationContextProvider restDocumentation) {
|
||||||
|
this.mockMvc = webAppContextSetup(webApplicationContext)
|
||||||
|
.apply(documentationConfiguration(restDocumentation))
|
||||||
|
.alwaysDo(document("{method-name}", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint())))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void smokeTest() {
|
||||||
|
assertThat(mockMvc).isNotNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenEndpoint_whenSendGetRequest_thenSuccessfulResponse() throws Exception {
|
||||||
|
mockMvc.perform(get("/books?page=2"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andDo(document("books",
|
||||||
|
requestParameters(parameterWithName("page").description("The page to retrieve"))));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
package com.baeldung.queryparamdoc;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
|
||||||
|
import static org.springframework.restdocs.request.RequestDocumentation.requestParameters;
|
||||||
|
import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.document;
|
||||||
|
import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.documentationConfiguration;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
|
||||||
|
import org.springframework.boot.test.context.TestConfiguration;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.restdocs.RestDocumentationContextProvider;
|
||||||
|
import org.springframework.restdocs.RestDocumentationExtension;
|
||||||
|
import org.springframework.security.test.context.support.WithMockUser;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||||
|
|
||||||
|
@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
|
||||||
|
@WebFluxTest
|
||||||
|
class BookControllerReactiveIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private WebTestClient webTestClient;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp(ApplicationContext webApplicationContext, RestDocumentationContextProvider restDocumentation) {
|
||||||
|
this.webTestClient = WebTestClient.bindToApplicationContext(webApplicationContext)
|
||||||
|
.configureClient()
|
||||||
|
.filter(documentationConfiguration(restDocumentation))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void smokeTest() {
|
||||||
|
assertThat(webTestClient).isNotNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithMockUser
|
||||||
|
void givenEndpoint_whenSendGetRequest_thenSuccessfulResponse() {
|
||||||
|
webTestClient.get().uri("/books?page=2")
|
||||||
|
.exchange().expectStatus().isOk().expectBody()
|
||||||
|
.consumeWith(document("books",
|
||||||
|
requestParameters(parameterWithName("page").description("The page to retrieve"))));
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestConfiguration
|
||||||
|
public static class TestConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
BookService bookService() {
|
||||||
|
return new BookService();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
package com.baeldung.queryparamdoc;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.hamcrest.core.Is.is;
|
||||||
|
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
|
||||||
|
import static org.springframework.restdocs.request.RequestDocumentation.requestParameters;
|
||||||
|
import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.document;
|
||||||
|
import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.documentationConfiguration;
|
||||||
|
|
||||||
|
import io.restassured.RestAssured;
|
||||||
|
import io.restassured.builder.RequestSpecBuilder;
|
||||||
|
import io.restassured.specification.RequestSpecification;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||||
|
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||||
|
import org.springframework.restdocs.RestDocumentationContextProvider;
|
||||||
|
import org.springframework.restdocs.RestDocumentationExtension;
|
||||||
|
import org.springframework.security.test.context.support.WithMockUser;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
|
||||||
|
@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
|
||||||
|
@AutoConfigureWebMvc
|
||||||
|
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||||
|
class BookControllerRestAssuredIntegrationTest {
|
||||||
|
|
||||||
|
private RequestSpecification spec;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp(RestDocumentationContextProvider restDocumentation, @LocalServerPort int port) {
|
||||||
|
this.spec = new RequestSpecBuilder().addFilter(documentationConfiguration(restDocumentation))
|
||||||
|
.setPort(port)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void smokeTest() {
|
||||||
|
assertThat(spec).isNotNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithMockUser
|
||||||
|
void givenEndpoint_whenSendGetRequest_thenSuccessfulResponse() {
|
||||||
|
RestAssured.given(this.spec).filter(document("users", requestParameters(
|
||||||
|
parameterWithName("page").description("The page to retrieve"))))
|
||||||
|
.when().get("/books?page=2")
|
||||||
|
.then().assertThat().statusCode(is(200));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user