From 10bde9d0af4fe39a18cbc2f90a7caf9f7e4b04ae Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Sat, 19 Mar 2022 18:27:30 +0000 Subject: [PATCH] [JAVA-10500] Investigate and reduce springdoc build time --- .../spring-boot-springdoc/pom.xml | 55 ----------- .../restdocopenapi/FooController.java | 5 +- .../src/main/resources/logback.xml | 5 +- .../restdoc/SpringRestDocsUnitTest.java | 96 +++++++++++-------- .../baeldung/springdoc/SpringContextTest.java | 14 +-- .../{logback.xml => logback-test.xml} | 0 6 files changed, 66 insertions(+), 109 deletions(-) rename spring-boot-modules/spring-boot-springdoc/src/test/resources/{logback.xml => logback-test.xml} (100%) diff --git a/spring-boot-modules/spring-boot-springdoc/pom.xml b/spring-boot-modules/spring-boot-springdoc/pom.xml index d13efba450..e7d4a35d97 100644 --- a/spring-boot-modules/spring-boot-springdoc/pom.xml +++ b/spring-boot-modules/spring-boot-springdoc/pom.xml @@ -37,10 +37,6 @@ spring-boot-starter-test test - - org.hibernate - hibernate-core - org.springdoc @@ -63,21 +59,6 @@ spring-restdocs-restassured test - - - org.springdoc - springdoc-openapi-kotlin - ${springdoc.version} - - - org.jetbrains.kotlin - kotlin-stdlib-jdk8 - ${kotlin.version} - - - org.jetbrains.kotlin - kotlin-reflect - @@ -109,41 +90,6 @@ - - - kotlin-maven-plugin - org.jetbrains.kotlin - ${kotlin.version} - - - spring - - ${java.version} - - - - compile - compile - - compile - - - - test-compile - test-compile - - test-compile - - - - - - org.jetbrains.kotlin - kotlin-maven-allopen - ${kotlin.version} - - - @@ -208,7 +154,6 @@ 1.6.4 1.5.6 - 1.6.0 ${project.build.directory}/generated-snippets diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/restdocopenapi/FooController.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/restdocopenapi/FooController.java index 55c2cccb3c..892eb05f8d 100644 --- a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/restdocopenapi/FooController.java +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/restdocopenapi/FooController.java @@ -40,7 +40,8 @@ public class FooController { public ResponseEntity getFooById(@PathVariable("id") Long id) { Optional foo = repository.findById(id); - return foo.isPresent() ? new ResponseEntity<>(foo.get(), HttpStatus.OK) : new ResponseEntity<>(HttpStatus.NOT_FOUND); + return foo.map(value -> new ResponseEntity<>(value, HttpStatus.OK)) + .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND)); } @PostMapping @@ -70,7 +71,7 @@ public class FooController { @PutMapping("/{id}") public ResponseEntity updateFoo(@PathVariable("id") long id, @RequestBody Foo foo) { - boolean isFooPresent = repository.existsById(Long.valueOf(id)); + boolean isFooPresent = repository.existsById(id); if (!isFooPresent) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/resources/logback.xml b/spring-boot-modules/spring-boot-springdoc/src/main/resources/logback.xml index 6a07b178e9..73dd672c1a 100644 --- a/spring-boot-modules/spring-boot-springdoc/src/main/resources/logback.xml +++ b/spring-boot-modules/spring-boot-springdoc/src/main/resources/logback.xml @@ -7,10 +7,7 @@ - - - - + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-springdoc/src/test/java/com/baeldung/restdocopenapi/restdoc/SpringRestDocsUnitTest.java b/spring-boot-modules/spring-boot-springdoc/src/test/java/com/baeldung/restdocopenapi/restdoc/SpringRestDocsUnitTest.java index 4d37abf78a..41e870e2e8 100644 --- a/spring-boot-modules/spring-boot-springdoc/src/test/java/com/baeldung/restdocopenapi/restdoc/SpringRestDocsUnitTest.java +++ b/spring-boot-modules/spring-boot-springdoc/src/test/java/com/baeldung/restdocopenapi/restdoc/SpringRestDocsUnitTest.java @@ -1,6 +1,32 @@ package com.baeldung.restdocopenapi.restdoc; +import com.baeldung.restdocopenapi.Foo; +import com.baeldung.restdocopenapi.FooController; +import com.baeldung.restdocopenapi.FooRepository; +import com.fasterxml.jackson.databind.ObjectMapper; +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.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.hateoas.MediaTypes; +import org.springframework.restdocs.RestDocumentationContextProvider; +import org.springframework.restdocs.RestDocumentationExtension; +import org.springframework.restdocs.constraints.ConstraintDescriptions; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import static java.util.Collections.singletonList; import static org.hamcrest.Matchers.containsString; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete; @@ -15,99 +41,89 @@ import static org.springframework.restdocs.payload.PayloadDocumentation.requestF import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields; import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName; import static org.springframework.restdocs.request.RequestDocumentation.pathParameters; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.util.StringUtils.collectionToDelimitedString; -import java.util.HashMap; -import java.util.Map; - -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.context.SpringBootTest; -import org.springframework.hateoas.MediaTypes; -import org.springframework.restdocs.RestDocumentationContextProvider; -import org.springframework.restdocs.RestDocumentationExtension; -import org.springframework.restdocs.constraints.ConstraintDescriptions; -import org.springframework.test.context.junit.jupiter.SpringExtension; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.web.context.WebApplicationContext; - -import com.baeldung.restdocopenapi.Application; -import com.baeldung.restdocopenapi.Foo; -import com.fasterxml.jackson.databind.ObjectMapper; - @ExtendWith({ RestDocumentationExtension.class, SpringExtension.class }) -@SpringBootTest(classes = Application.class) -public class SpringRestDocsUnitTest { +@WebMvcTest(FooController.class) +class SpringRestDocsUnitTest { private MockMvc mockMvc; - + + @MockBean + private FooRepository fooRepository; + @Autowired private ObjectMapper objectMapper; @BeforeEach - public void setup(WebApplicationContext webApplicationContext, RestDocumentationContextProvider restDocumentation) { + void setup(WebApplicationContext webApplicationContext, RestDocumentationContextProvider restDocumentation) { this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) .apply(documentationConfiguration(restDocumentation)) .build(); } @Test - public void whenGetFoo_thenSuccessful() throws Exception { + void whenGetAllFoo_thenSuccessful() throws Exception { + when(fooRepository.findAll()) + .thenReturn(singletonList(new Foo(1, "Foo 1", "Foo 1"))); + this.mockMvc.perform(get("/foo")) - .andDo(print()) .andExpect(status().isOk()) .andExpect(content().string(containsString("Foo 1"))) .andDo(document("getAllFoos")); } @Test - public void whenGetFooById_thenSuccessful() throws Exception { + void whenGetFooById_thenSuccessful() throws Exception { ConstraintDescriptions desc = new ConstraintDescriptions(Foo.class); + when(fooRepository.findById(1L)) + .thenReturn(Optional.of(new Foo(1, "title", "body"))); + this.mockMvc.perform(get("/foo/{id}", 1)) .andExpect(status().isOk()) - .andDo(document("getAFoo", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()), + .andDo(document("getAFoo", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()), pathParameters(parameterWithName("id").description("id of foo to be searched")), responseFields(fieldWithPath("id").description("The id of the foo" + collectionToDelimitedString(desc.descriptionsForProperty("id"), ". ")), fieldWithPath("title").description("The title of the foo"), fieldWithPath("body").description("The body of the foo")))); } @Test - public void whenPostFoo_thenSuccessful() throws Exception { + void whenPostFoo_thenSuccessful() throws Exception { Map foo = new HashMap<>(); foo.put("id", 4L); foo.put("title", "New Foo"); foo.put("body", "Body of New Foo"); - + this.mockMvc.perform(post("/foo").contentType(MediaTypes.HAL_JSON) .content(this.objectMapper.writeValueAsString(foo))) .andExpect(status().isCreated()) .andDo(document("createFoo", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()), requestFields(fieldWithPath("id").description("The id of the foo"), fieldWithPath("title").description("The title of the foo"), fieldWithPath("body").description("The body of the foo")))); } - + @Test - public void whenDeleteFoo_thenSuccessful() throws Exception { + void whenDeleteFoo_thenSuccessful() throws Exception { this.mockMvc.perform(delete("/foo/{id}", 2)) .andExpect(status().isNoContent()) .andDo(document("deleteFoo", pathParameters(parameterWithName("id").description("The id of the foo to delete")))); } - + @Test - public void whenUpdateFoo_thenSuccessful() throws Exception { - + void whenUpdateFoo_thenSuccessful() throws Exception { + + when(fooRepository.existsById(3L)).thenReturn(true); + when(fooRepository.save(any(Foo.class))) + .thenReturn(new Foo(3, "Updated Foo", "Body of updated Foo")); + ConstraintDescriptions desc = new ConstraintDescriptions(Foo.class); - + Map foo = new HashMap<>(); foo.put("title", "Updated Foo"); foo.put("body", "Body of Updated Foo"); - + this.mockMvc.perform(put("/foo/{id}", 3).contentType(MediaTypes.HAL_JSON) .content(this.objectMapper.writeValueAsString(foo))) .andExpect(status().isOk()) @@ -115,6 +131,4 @@ public class SpringRestDocsUnitTest { responseFields(fieldWithPath("id").description("The id of the updated foo" + collectionToDelimitedString(desc.descriptionsForProperty("id"), ". ")), fieldWithPath("title").description("The title of the updated foo"), fieldWithPath("body").description("The body of the updated foo")))); } - - } diff --git a/spring-boot-modules/spring-boot-springdoc/src/test/java/com/baeldung/springdoc/SpringContextTest.java b/spring-boot-modules/spring-boot-springdoc/src/test/java/com/baeldung/springdoc/SpringContextTest.java index 4cd84477b9..d7cd6bc30b 100644 --- a/spring-boot-modules/spring-boot-springdoc/src/test/java/com/baeldung/springdoc/SpringContextTest.java +++ b/spring-boot-modules/spring-boot-springdoc/src/test/java/com/baeldung/springdoc/SpringContextTest.java @@ -1,17 +1,17 @@ package com.baeldung.springdoc; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) @SpringBootTest -public class SpringContextTest { +class SpringContextTest { @Test - public void whenSpringContextIsBootstrapped_thenNoExceptions() { - + void whenSpringContextIsBootstrapped_thenNoExceptions() { + } } diff --git a/spring-boot-modules/spring-boot-springdoc/src/test/resources/logback.xml b/spring-boot-modules/spring-boot-springdoc/src/test/resources/logback-test.xml similarity index 100% rename from spring-boot-modules/spring-boot-springdoc/src/test/resources/logback.xml rename to spring-boot-modules/spring-boot-springdoc/src/test/resources/logback-test.xml