From 8a5a14e88a07665c97638dd81a31fb4e6c336194 Mon Sep 17 00:00:00 2001 From: jcw1031 Date: Sat, 22 Apr 2023 18:52:55 +0900 Subject: [PATCH 1/4] Owner class addVisit() method return type void --- .../org/springframework/samples/petclinic/owner/Owner.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/owner/Owner.java b/src/main/java/org/springframework/samples/petclinic/owner/Owner.java index a5234942b..ac556459d 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/Owner.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/Owner.java @@ -160,7 +160,7 @@ public class Owner extends Person { * @param petId the identifier of the {@link Pet}, must not be {@literal null}. * @param visit the visit to add, must not be {@literal null}. */ - public Owner addVisit(Integer petId, Visit visit) { + public void addVisit(Integer petId, Visit visit) { Assert.notNull(petId, "Pet identifier must not be null!"); Assert.notNull(visit, "Visit must not be null!"); @@ -170,8 +170,6 @@ public class Owner extends Person { Assert.notNull(pet, "Invalid Pet identifier!"); pet.addVisit(visit); - - return this; } } From b2ec2204c28bf582d06f1e4d70aaae144c5d84b8 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 9 May 2023 10:34:06 +0100 Subject: [PATCH 2/4] Latest Java version --- .devcontainer/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 9782cf3de..804a6d446 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -8,6 +8,6 @@ ARG USER=vscode VOLUME /home/$USER/.m2 VOLUME /home/$USER/.gradle -ARG JAVA_VERSION=17.0.4.1-ms +ARG JAVA_VERSION=17.0.7-ms RUN sudo mkdir /home/$USER/.m2 /home/$USER/.gradle && sudo chown $USER:$USER /home/$USER/.m2 /home/$USER/.gradle RUN bash -lc '. /usr/local/sdkman/bin/sdkman-init.sh && sdk install java $JAVA_VERSION && sdk use java $JAVA_VERSION' From 0787ad6a22359df32c5dd8fe6ec8bdee38d956c8 Mon Sep 17 00:00:00 2001 From: xerx593 Date: Tue, 25 Apr 2023 22:56:01 +0200 Subject: [PATCH 3/4] improve jmeter test --------------------- - jmeter update - reduced owner + pet count (99% fixes "(Post) New Visit" issues) - fixed JSS: jquery->bootstrap ;) - changed post params from "body" to "form value" (better!) - added "test" for GET and POST new pet --------------------------------2023-04-25 22:47:37,313 INFO o.a.j.r.Summariser: summary = 65000 in 00:08:42 = 124.4/s Avg: 3526 Min: 4 Max: 20838 Err: 4 (0.01%) --- src/test/jmeter/petclinic_test_plan.jmx | 116 ++++++++++++++++++++---- 1 file changed, 96 insertions(+), 20 deletions(-) diff --git a/src/test/jmeter/petclinic_test_plan.jmx b/src/test/jmeter/petclinic_test_plan.jmx index 0a9a3a5f9..8014ffbec 100644 --- a/src/test/jmeter/petclinic_test_plan.jmx +++ b/src/test/jmeter/petclinic_test_plan.jmx @@ -1,5 +1,5 @@ - + @@ -69,7 +69,7 @@ 1 - 10 + 3 1 count @@ -78,7 +78,7 @@ 1 - 13 + 3 1 petCount @@ -135,7 +135,7 @@ - ${CONTEXT_WEB}/webjars/jquery/jquery.min.js + ${CONTEXT_WEB}/webjars/bootstrap/5.2.3/dist/js/bootstrap.bundle.min.js GET true false @@ -246,13 +246,42 @@ - true - + - + false - firstName=Test&lastName=${count}&address=1234+Test+St.&city=TestCity&telephone=612345678 + Test = + true + firstName + + + false + ${count} + = + true + lastName + + + false + 1234+Test+St. + = + true + address + + + false + TestCity + = + true + city + + + false + 612345678 + = + true + telephone @@ -272,7 +301,7 @@ - + @@ -282,7 +311,7 @@ - ${CONTEXT_WEB}/owners/${count}/pets/${petCount}/visits/new + ${CONTEXT_WEB}/owners/${count}/pets/new GET true false @@ -292,14 +321,29 @@ - - true - + + - + false - date=2013%2F02%2F22&description=visit + Test+Fluffy+${petCount} = + true + name + + + false + 2020-12-20 + = + true + birthDate + + + false + cat + = + true + type @@ -309,7 +353,7 @@ - ${CONTEXT_WEB}/owners/${count}/pets/${petCount}/visits/new + ${CONTEXT_WEB}/owners/${count}/pets/new POST true false @@ -319,22 +363,54 @@ - + - - - ${CONTEXT_WEB}/owners/${count} + ${CONTEXT_WEB}/owners/${count}/pets/${petCount}/visits/new GET true false true false + + + + + + + + + + false + 2013-02-22 + = + true + date + + + false + visit + = + true + description + + + + + + + + ${CONTEXT_WEB}/owners/${count}/pets/${petCount}/visits/new + POST + true + false + true + false false From 2249cbcb6f5da03a041ef1a6a20987889d6b8771 Mon Sep 17 00:00:00 2001 From: xerx593 Date: Tue, 25 Apr 2023 14:50:47 +0000 Subject: [PATCH 4/4] Update CrashControllerTests.java --- .../CrashControllerIntegrationTests.java | 98 +++++++++++++++++++ .../system/CrashControllerTests.java | 30 +++--- 2 files changed, 111 insertions(+), 17 deletions(-) create mode 100644 src/test/java/org/springframework/samples/petclinic/system/CrashControllerIntegrationTests.java diff --git a/src/test/java/org/springframework/samples/petclinic/system/CrashControllerIntegrationTests.java b/src/test/java/org/springframework/samples/petclinic/system/CrashControllerIntegrationTests.java new file mode 100644 index 000000000..4edf2af92 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/system/CrashControllerIntegrationTests.java @@ -0,0 +1,98 @@ +/* + * Copyright 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.samples.petclinic.system; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +import java.util.List; +import java.util.Map; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; +import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.RequestEntity; +import org.springframework.http.ResponseEntity; + +/** + * Integration Test for {@link CrashController}. + * + * @author Alex Lutz + */ +// NOT Waiting https://github.com/spring-projects/spring-boot/issues/5574 +@SpringBootTest(webEnvironment = RANDOM_PORT, + properties = { "server.error.include-message=ALWAYS", "management.endpoints.enabled-by-default=false" }) +class CrashControllerIntegrationTests { + + @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, + DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class }) + static class TestConfiguration { + + } + + @Value(value = "${local.server.port}") + private int port; + + @Autowired + private TestRestTemplate rest; + + @Test + void testTriggerExceptionJson() { + ResponseEntity> resp = rest.exchange( + RequestEntity.get("http://localhost:" + port + "/oups").build(), + new ParameterizedTypeReference>() { + }); + assertThat(resp).isNotNull(); + assertThat(resp.getStatusCode().is5xxServerError()); + assertThat(resp.getBody().containsKey("timestamp")); + assertThat(resp.getBody().containsKey("status")); + assertThat(resp.getBody().containsKey("error")); + assertThat(resp.getBody()).containsEntry("message", + "Expected: controller used to showcase what happens when an exception is thrown"); + assertThat(resp.getBody()).containsEntry("path", "/oups"); + } + + @Test + void testTriggerExceptionHtml() { + HttpHeaders headers = new HttpHeaders(); + headers.setAccept(List.of(MediaType.TEXT_HTML)); + ResponseEntity resp = rest.exchange("http://localhost:" + port + "/oups", HttpMethod.GET, + new HttpEntity<>(headers), String.class); + assertThat(resp).isNotNull(); + assertThat(resp.getStatusCode().is5xxServerError()); + assertThat(resp.getBody()).isNotNull(); + // html: + assertThat(resp.getBody()).containsSubsequence("", "

", "Something happened...", "

", "

", + "Expected:", "controller", "used", "to", "showcase", "what", "happens", "when", "an", "exception", "is", + "thrown", "

", ""); + // Not the whitelabel error page: + assertThat(resp.getBody()).doesNotContain("Whitelabel Error Page", + "This application has no explicit mapping for"); + } + +} diff --git a/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java b/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java index 142a136b0..65d3b8921 100644 --- a/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java @@ -16,35 +16,31 @@ package org.springframework.samples.petclinic.system; -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.test.web.servlet.MockMvc; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import org.junit.jupiter.api.Test; /** * Test class for {@link CrashController} * * @author Colin But + * @author Alex Lutz */ -// Waiting https://github.com/spring-projects/spring-boot/issues/5574 -@Disabled -@WebMvcTest(controllers = CrashController.class) +// Waiting https://github.com/spring-projects/spring-boot/issues/5574 ..good +// luck ((plain(st) UNIT test)! :) class CrashControllerTests { - @Autowired - private MockMvc mockMvc; + CrashController testee = new CrashController(); @Test void testTriggerException() throws Exception { - mockMvc.perform(get("/oups")) - .andExpect(view().name("exception")) - .andExpect(model().attributeExists("exception")) - .andExpect(forwardedUrl("exception")) - .andExpect(status().isOk()); + RuntimeException thrown = assertThrows(RuntimeException.class, () -> { + testee.triggerException(); + }); + + assertEquals("Expected: controller used to showcase what happens when an exception is thrown", + thrown.getMessage()); } }