From cb26154fe0554b6bc8c17548090a33d82c74b774 Mon Sep 17 00:00:00 2001 From: sergio41 Date: Sat, 20 Jun 2020 13:09:32 +0200 Subject: [PATCH 1/4] [BAEL-4084] Code Upload --- spring-rest-testing/pom.xml | 4 ++ .../ExceptionTestingApplication.java | 25 +++++++ .../controller/ExceptionController.java | 31 +++++++++ .../exception/BadArgumentsException.java | 13 ++++ .../exception/InternalException.java | 13 ++++ .../exception/ResourceNotFoundException.java | 13 ++++ .../ExceptionControllerUnitTest.java | 66 +++++++++++++++++++ 7 files changed, 165 insertions(+) create mode 100644 spring-rest-testing/src/main/java/com/baeldung/exceptiontesting/ExceptionTestingApplication.java create mode 100644 spring-rest-testing/src/main/java/com/baeldung/exceptiontesting/controller/ExceptionController.java create mode 100644 spring-rest-testing/src/main/java/com/baeldung/exceptiontesting/exception/BadArgumentsException.java create mode 100644 spring-rest-testing/src/main/java/com/baeldung/exceptiontesting/exception/InternalException.java create mode 100644 spring-rest-testing/src/main/java/com/baeldung/exceptiontesting/exception/ResourceNotFoundException.java create mode 100644 spring-rest-testing/src/test/java/com/baeldung/exceptiontesting/controller/ExceptionControllerUnitTest.java diff --git a/spring-rest-testing/pom.xml b/spring-rest-testing/pom.xml index d807459cad..4947acd265 100644 --- a/spring-rest-testing/pom.xml +++ b/spring-rest-testing/pom.xml @@ -141,6 +141,10 @@ com.h2database h2 + + net.bytebuddy + byte-buddy + diff --git a/spring-rest-testing/src/main/java/com/baeldung/exceptiontesting/ExceptionTestingApplication.java b/spring-rest-testing/src/main/java/com/baeldung/exceptiontesting/ExceptionTestingApplication.java new file mode 100644 index 0000000000..facc300dfa --- /dev/null +++ b/spring-rest-testing/src/main/java/com/baeldung/exceptiontesting/ExceptionTestingApplication.java @@ -0,0 +1,25 @@ +package com.baeldung.exceptiontesting; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.scheduling.annotation.EnableScheduling; + +/** + * Main Application Class - uses Spring Boot. Just run this as a normal Java + * class to run up a Jetty Server (on http://localhost:8082/spring-rest-full) + * + */ +@EnableScheduling +@EnableAutoConfiguration +@ComponentScan("com.baeldung.exceptiontesting") +@SpringBootApplication +public class ExceptionTestingApplication extends SpringBootServletInitializer { + + public static void main(final String[] args) { + SpringApplication.run(ExceptionTestingApplication.class, args); + } + +} \ No newline at end of file diff --git a/spring-rest-testing/src/main/java/com/baeldung/exceptiontesting/controller/ExceptionController.java b/spring-rest-testing/src/main/java/com/baeldung/exceptiontesting/controller/ExceptionController.java new file mode 100644 index 0000000000..0f458b5f10 --- /dev/null +++ b/spring-rest-testing/src/main/java/com/baeldung/exceptiontesting/controller/ExceptionController.java @@ -0,0 +1,31 @@ +package com.baeldung.exceptiontesting.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.exceptiontesting.exception.BadArgumentsException; +import com.baeldung.exceptiontesting.exception.InternalException; +import com.baeldung.exceptiontesting.exception.ResourceNotFoundException; + +@RestController +public class ExceptionController { + + @GetMapping("/exception/{exception_id}") + public void getSpecificException(@PathVariable("exception_id") String pException) { + if("not_found".equals(pException)) { + throw new ResourceNotFoundException("resource not found"); + } + else if("bad_arguments".equals(pException)) { + throw new BadArgumentsException("bad arguments"); + } + else { + throw new InternalException("internal error"); + } + } + + @GetMapping("/exception/throw") + public void getException() throws Exception { + throw new Exception("error"); + } +} diff --git a/spring-rest-testing/src/main/java/com/baeldung/exceptiontesting/exception/BadArgumentsException.java b/spring-rest-testing/src/main/java/com/baeldung/exceptiontesting/exception/BadArgumentsException.java new file mode 100644 index 0000000000..1eb1e6a3c9 --- /dev/null +++ b/spring-rest-testing/src/main/java/com/baeldung/exceptiontesting/exception/BadArgumentsException.java @@ -0,0 +1,13 @@ +package com.baeldung.exceptiontesting.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@SuppressWarnings("serial") +@ResponseStatus(HttpStatus.BAD_REQUEST) +public class BadArgumentsException extends RuntimeException { + + public BadArgumentsException(String message) { + super(message); + } +} diff --git a/spring-rest-testing/src/main/java/com/baeldung/exceptiontesting/exception/InternalException.java b/spring-rest-testing/src/main/java/com/baeldung/exceptiontesting/exception/InternalException.java new file mode 100644 index 0000000000..8e9f0f60f3 --- /dev/null +++ b/spring-rest-testing/src/main/java/com/baeldung/exceptiontesting/exception/InternalException.java @@ -0,0 +1,13 @@ +package com.baeldung.exceptiontesting.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@SuppressWarnings("serial") +@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) +public class InternalException extends RuntimeException { + + public InternalException(String message) { + super(message); + } +} diff --git a/spring-rest-testing/src/main/java/com/baeldung/exceptiontesting/exception/ResourceNotFoundException.java b/spring-rest-testing/src/main/java/com/baeldung/exceptiontesting/exception/ResourceNotFoundException.java new file mode 100644 index 0000000000..469d5af96f --- /dev/null +++ b/spring-rest-testing/src/main/java/com/baeldung/exceptiontesting/exception/ResourceNotFoundException.java @@ -0,0 +1,13 @@ +package com.baeldung.exceptiontesting.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@SuppressWarnings("serial") +@ResponseStatus(HttpStatus.NOT_FOUND) +public class ResourceNotFoundException extends RuntimeException { + + public ResourceNotFoundException(String message) { + super(message); + } +} diff --git a/spring-rest-testing/src/test/java/com/baeldung/exceptiontesting/controller/ExceptionControllerUnitTest.java b/spring-rest-testing/src/test/java/com/baeldung/exceptiontesting/controller/ExceptionControllerUnitTest.java new file mode 100644 index 0000000000..b08e1cc09d --- /dev/null +++ b/spring-rest-testing/src/test/java/com/baeldung/exceptiontesting/controller/ExceptionControllerUnitTest.java @@ -0,0 +1,66 @@ +package com.baeldung.exceptiontesting.controller; +import org.junit.Test; +import org.junit.runner.RunWith; +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.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; + +import com.baeldung.exceptiontesting.controller.ExceptionController; +import com.baeldung.exceptiontesting.exception.BadArgumentsException; +import com.baeldung.exceptiontesting.exception.InternalException; +import com.baeldung.exceptiontesting.exception.ResourceNotFoundException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +@RunWith(SpringRunner.class) +//@SpringBootTest(classes = Application.class) +@WebMvcTest(ExceptionController.class) +public class ExceptionControllerUnitTest{ + + @Autowired + private MockMvc mvc; + + @Test + public void givenNotFound_whenGetSpecificException_thenNotFoundCode() throws Exception { + String exceptionParam = "not_found"; + + mvc.perform(get("/exception/{exception_id}", exceptionParam) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isNotFound()) + .andExpect(result -> assertTrue(result.getResolvedException() instanceof ResourceNotFoundException)) + .andExpect(result -> assertEquals("resource not found", result.getResolvedException().getMessage())); + } + + @Test + public void givenBadArguments_whenGetSpecificException_thenBadRequest() throws Exception { + String exceptionParam = "bad_arguments"; + + mvc.perform(get("/exception/{exception_id}", exceptionParam) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isBadRequest()) + .andExpect(result -> assertTrue(result.getResolvedException() instanceof BadArgumentsException)) + .andExpect(result -> assertEquals("bad arguments", result.getResolvedException().getMessage())); + } + + @Test + public void givenOther_whenGetSpecificException_thenInternalServerError() throws Exception { + String exceptionParam = "dummy"; + + mvc.perform(get("/exception/{exception_id}", exceptionParam) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isInternalServerError()) + .andExpect(result -> assertTrue(result.getResolvedException() instanceof InternalException)) + .andExpect(result -> assertEquals("internal error", result.getResolvedException().getMessage())); + } + + @Test(expected = Exception.class) + public void whenGetException_thenInternalServerError() throws Exception { + mvc.perform(get("/exception/throw") + .contentType(MediaType.APPLICATION_JSON)); + } +} From 8e55aec36237d8c2e4ad2446f75c8fe2d803b10d Mon Sep 17 00:00:00 2001 From: sergio41 Date: Sat, 20 Jun 2020 20:00:19 +0200 Subject: [PATCH 2/4] [BAEL-4084] Correcto identation --- spring-rest-testing/pom.xml | 553 ++++++++++++++++++------------------ 1 file changed, 277 insertions(+), 276 deletions(-) diff --git a/spring-rest-testing/pom.xml b/spring-rest-testing/pom.xml index 4947acd265..ebe03428fe 100644 --- a/spring-rest-testing/pom.xml +++ b/spring-rest-testing/pom.xml @@ -1,298 +1,299 @@ - - 4.0.0 - spring-rest-testing - 0.1-SNAPSHOT - spring-rest-testing - war + + 4.0.0 + spring-rest-testing + 0.1-SNAPSHOT + spring-rest-testing + war - - com.baeldung - parent-boot-2 - 0.0.1-SNAPSHOT - ../parent-boot-2 - + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../parent-boot-2 + - + - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-actuator - - - org.aspectj - aspectjweaver - - - org.apache.tomcat.embed - tomcat-embed-jasper - provided - - - - - - org.springframework - spring-core - - - commons-logging - commons-logging - - - - - org.springframework - spring-context - - - org.springframework - spring-jdbc - - - org.springframework - spring-beans - - - org.springframework - spring-aop - - - org.springframework - spring-tx - - - org.springframework - spring-expression - - - org.springframework - spring-web - - - org.springframework - spring-webmvc - - - org.springframework.data - spring-data-commons - - - - - - org.springframework.boot - spring-boot-starter-tomcat - - - - - - org.apache.httpcomponents - httpclient - - - commons-logging - commons-logging - - - - - org.apache.httpcomponents - httpcore - - - - - - org.springframework - spring-orm - - - org.springframework.data - spring-data-jpa - - - org.hibernate - hibernate-entitymanager - - - xml-apis - xml-apis - ${xml-apis.version} - - - org.javassist - javassist - ${javassist.version} - - - mysql - mysql-connector-java - runtime - - - com.h2database - h2 - - - net.bytebuddy - byte-buddy + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-actuator + + + org.aspectj + aspectjweaver + + + org.apache.tomcat.embed + tomcat-embed-jasper + provided - + - - javax.servlet - javax.servlet-api - provided - - - javax.servlet - jstl - runtime - + + org.springframework + spring-core + + + commons-logging + commons-logging + + + + + org.springframework + spring-context + + + org.springframework + spring-jdbc + + + org.springframework + spring-beans + + + org.springframework + spring-aop + + + org.springframework + spring-tx + + + org.springframework + spring-expression + + + org.springframework + spring-web + + + org.springframework + spring-webmvc + + + org.springframework.data + spring-data-commons + - - - com.fasterxml.jackson.core - jackson-databind - + - + + org.springframework.boot + spring-boot-starter-tomcat + - - com.google.guava - guava - ${guava.version} - + - + + org.apache.httpcomponents + httpclient + + + commons-logging + commons-logging + + + + + org.apache.httpcomponents + httpcore + - - org.springframework - spring-test - test - + - - org.hamcrest - hamcrest - test - + + org.springframework + spring-orm + + + org.springframework.data + spring-data-jpa + + + org.hibernate + hibernate-entitymanager + + + xml-apis + xml-apis + ${xml-apis.version} + + + org.javassist + javassist + ${javassist.version} + + + mysql + mysql-connector-java + runtime + + + com.h2database + h2 + + + net.bytebuddy + byte-buddy + - - org.mockito - mockito-core - test - + - + + javax.servlet + javax.servlet-api + provided + + + javax.servlet + jstl + runtime + - - spring-rest-testing - - - src/main/resources - true - - - - - org.apache.maven.plugins - maven-war-plugin - - - org.codehaus.cargo - cargo-maven2-plugin - ${cargo-maven2-plugin.version} - - true - - jetty8x - embedded - - - - - - - 8082 - - - - - - - com.mysema.maven - apt-maven-plugin - ${apt-maven-plugin.version} - - - - process - - - target/generated-sources/java - com.querydsl.apt.jpa.JPAAnnotationProcessor - - - - - - + + + com.fasterxml.jackson.core + jackson-databind + - - - live - - - - org.codehaus.cargo - cargo-maven2-plugin - - false - - - - start-server - pre-integration-test - - start - - - - stop-server - post-integration-test - - stop - - - - - - - - + - - - 1.4.9 - 1.4.01 + + com.google.guava + guava + ${guava.version} + - - 19.0 - 3.25.0-GA + - - 1.6.1 - 1.1.3 - + + org.springframework + spring-test + test + + + + org.hamcrest + hamcrest + test + + + + org.mockito + mockito-core + test + + + + + + spring-rest-testing + + + src/main/resources + true + + + + + org.apache.maven.plugins + maven-war-plugin + + + org.codehaus.cargo + cargo-maven2-plugin + ${cargo-maven2-plugin.version} + + true + + jetty8x + embedded + + + + + + + 8082 + + + + + + + com.mysema.maven + apt-maven-plugin + ${apt-maven-plugin.version} + + + + process + + + target/generated-sources/java + com.querydsl.apt.jpa.JPAAnnotationProcessor + + + + + + + + + + live + + + + org.codehaus.cargo + cargo-maven2-plugin + + false + + + + start-server + pre-integration-test + + start + + + + stop-server + post-integration-test + + stop + + + + + + + + + + + + 1.4.9 + 1.4.01 + + + 19.0 + 3.25.0-GA + + + 1.6.1 + 1.1.3 + From ab70edcd354aacba1dd91effcdc99cf678681892 Mon Sep 17 00:00:00 2001 From: sergio41 Date: Sun, 21 Jun 2020 10:23:59 +0200 Subject: [PATCH 3/4] [BAEL-4084] Correct indentation II --- spring-rest-testing/pom.xml | 539 ++++++++++++++++++------------------ 1 file changed, 269 insertions(+), 270 deletions(-) diff --git a/spring-rest-testing/pom.xml b/spring-rest-testing/pom.xml index ebe03428fe..9bfe9d83a4 100644 --- a/spring-rest-testing/pom.xml +++ b/spring-rest-testing/pom.xml @@ -1,299 +1,298 @@ - - 4.0.0 - spring-rest-testing - 0.1-SNAPSHOT - spring-rest-testing - war + + 4.0.0 + spring-rest-testing + 0.1-SNAPSHOT + spring-rest-testing + war - - com.baeldung - parent-boot-2 - 0.0.1-SNAPSHOT - ../parent-boot-2 - + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../parent-boot-2 + - + - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-actuator - - - org.aspectj - aspectjweaver - - - org.apache.tomcat.embed - tomcat-embed-jasper - provided - + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-actuator + + + org.aspectj + aspectjweaver + + + org.apache.tomcat.embed + tomcat-embed-jasper + provided + - + - - org.springframework - spring-core - - - commons-logging - commons-logging - - - - - org.springframework - spring-context - - - org.springframework - spring-jdbc - - - org.springframework - spring-beans - - - org.springframework - spring-aop - - - org.springframework - spring-tx - - - org.springframework - spring-expression - - - org.springframework - spring-web - - - org.springframework - spring-webmvc - - - org.springframework.data - spring-data-commons - + + org.springframework + spring-core + + + commons-logging + commons-logging + + + + + org.springframework + spring-context + + + org.springframework + spring-jdbc + + + org.springframework + spring-beans + + + org.springframework + spring-aop + + + org.springframework + spring-tx + + + org.springframework + spring-expression + + + org.springframework + spring-web + + + org.springframework + spring-webmvc + + + org.springframework.data + spring-data-commons + - + - - org.springframework.boot - spring-boot-starter-tomcat - + + org.springframework.boot + spring-boot-starter-tomcat + - + - - org.apache.httpcomponents - httpclient - - - commons-logging - commons-logging - - - - - org.apache.httpcomponents - httpcore - + + org.apache.httpcomponents + httpclient + + + commons-logging + commons-logging + + + + + org.apache.httpcomponents + httpcore + - + - - org.springframework - spring-orm - - - org.springframework.data - spring-data-jpa - - - org.hibernate - hibernate-entitymanager - - - xml-apis - xml-apis - ${xml-apis.version} - - - org.javassist - javassist - ${javassist.version} - - - mysql - mysql-connector-java - runtime - - - com.h2database - h2 - - - net.bytebuddy - byte-buddy - + + org.springframework + spring-orm + + + org.springframework.data + spring-data-jpa + + + org.hibernate + hibernate-entitymanager + + + xml-apis + xml-apis + ${xml-apis.version} + + + org.javassist + javassist + ${javassist.version} + + + mysql + mysql-connector-java + runtime + + + com.h2database + h2 + + + net.bytebuddy + byte-buddy + - + - - javax.servlet - javax.servlet-api - provided - - - javax.servlet - jstl - runtime - + + javax.servlet + javax.servlet-api + provided + + + javax.servlet + jstl + runtime + - - - com.fasterxml.jackson.core - jackson-databind - + + + com.fasterxml.jackson.core + jackson-databind + - + - - com.google.guava - guava - ${guava.version} - + + com.google.guava + guava + ${guava.version} + - + - - org.springframework - spring-test - test - + + org.springframework + spring-test + test + - - org.hamcrest - hamcrest - test - + + org.hamcrest + hamcrest + test + - - org.mockito - mockito-core - test - + + org.mockito + mockito-core + test + - + - - spring-rest-testing - - - src/main/resources - true - - - - - org.apache.maven.plugins - maven-war-plugin - - - org.codehaus.cargo - cargo-maven2-plugin - ${cargo-maven2-plugin.version} - - true - - jetty8x - embedded - - - - - - - 8082 - - - - - - - com.mysema.maven - apt-maven-plugin - ${apt-maven-plugin.version} - - - - process - - - target/generated-sources/java - com.querydsl.apt.jpa.JPAAnnotationProcessor - - - - - - + + spring-rest-testing + + + src/main/resources + true + + + + + org.apache.maven.plugins + maven-war-plugin + + + org.codehaus.cargo + cargo-maven2-plugin + ${cargo-maven2-plugin.version} + + true + + jetty8x + embedded + + + + + + + 8082 + + + + + + + com.mysema.maven + apt-maven-plugin + ${apt-maven-plugin.version} + + + + process + + + target/generated-sources/java + com.querydsl.apt.jpa.JPAAnnotationProcessor + + + + + + - - - live - - - - org.codehaus.cargo - cargo-maven2-plugin - - false - - - - start-server - pre-integration-test - - start - - - - stop-server - post-integration-test - - stop - - - - - - - - + + + live + + + + org.codehaus.cargo + cargo-maven2-plugin + + false + + + + start-server + pre-integration-test + + start + + + + stop-server + post-integration-test + + stop + + + + + + + + - - - 1.4.9 - 1.4.01 + + + 1.4.9 + 1.4.01 - - 19.0 - 3.25.0-GA + + 19.0 + 3.25.0-GA - - 1.6.1 - 1.1.3 - + + 1.6.1 + 1.1.3 + From c7aee7a68029c5113d674c859a95e1191ad6e1cd Mon Sep 17 00:00:00 2001 From: sergio41 Date: Sun, 21 Jun 2020 22:52:49 +0200 Subject: [PATCH 4/4] [BAEL-4084] Useless line removed --- .../exceptiontesting/controller/ExceptionControllerUnitTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/spring-rest-testing/src/test/java/com/baeldung/exceptiontesting/controller/ExceptionControllerUnitTest.java b/spring-rest-testing/src/test/java/com/baeldung/exceptiontesting/controller/ExceptionControllerUnitTest.java index b08e1cc09d..d624efcdd0 100644 --- a/spring-rest-testing/src/test/java/com/baeldung/exceptiontesting/controller/ExceptionControllerUnitTest.java +++ b/spring-rest-testing/src/test/java/com/baeldung/exceptiontesting/controller/ExceptionControllerUnitTest.java @@ -18,7 +18,6 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @RunWith(SpringRunner.class) -//@SpringBootTest(classes = Application.class) @WebMvcTest(ExceptionController.class) public class ExceptionControllerUnitTest{