diff --git a/spring-rest-testing/pom.xml b/spring-rest-testing/pom.xml index d807459cad..9bfe9d83a4 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..d624efcdd0 --- /dev/null +++ b/spring-rest-testing/src/test/java/com/baeldung/exceptiontesting/controller/ExceptionControllerUnitTest.java @@ -0,0 +1,65 @@ +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) +@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)); + } +}