JAVA-3538: Move spring-rest-testing into spring-web-modules

This commit is contained in:
Krzysztof Woyke 2021-01-04 16:27:21 +01:00
parent f63058acb6
commit 7dc8fdba7e
51 changed files with 161 additions and 162 deletions

View File

@ -664,7 +664,6 @@
<module>spring-reactor</module> <module>spring-reactor</module>
<module>spring-remoting</module> <module>spring-remoting</module>
<module>spring-resttemplate</module> <module>spring-resttemplate</module>
<module>spring-rest-testing</module>
<module>spring-roo</module> <module>spring-roo</module>
<module>spring-scheduling</module> <module>spring-scheduling</module>
@ -1119,7 +1118,6 @@
<module>spring-reactor</module> <module>spring-reactor</module>
<module>spring-remoting</module> <module>spring-remoting</module>
<module>spring-resttemplate</module> <module>spring-resttemplate</module>
<module>spring-rest-testing</module>
<module>spring-roo</module> <module>spring-roo</module>
<module>spring-scheduling</module> <module>spring-scheduling</module>

View File

@ -33,6 +33,7 @@
<module>spring-rest-query-language</module> <module>spring-rest-query-language</module>
<module>spring-rest-shell</module> <module>spring-rest-shell</module>
<module>spring-rest-simple</module> <module>spring-rest-simple</module>
<module>spring-rest-testing</module>
<module>spring-resttemplate-2</module> <module>spring-resttemplate-2</module>
<module>spring-thymeleaf</module> <module>spring-thymeleaf</module>
<module>spring-thymeleaf-2</module> <module>spring-thymeleaf-2</module>

View File

@ -11,7 +11,7 @@
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId> <artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath> <relativePath>../../parent-boot-2</relativePath>
</parent> </parent>
<dependencies> <dependencies>

View File

@ -1,25 +1,25 @@
package com.baeldung.exceptiontesting; package com.baeldung.exceptiontesting;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
/** /**
* Main Application Class - uses Spring Boot. Just run this as a normal Java * 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) * class to run up a Jetty Server (on http://localhost:8082/spring-rest-full)
* *
*/ */
@EnableScheduling @EnableScheduling
@EnableAutoConfiguration @EnableAutoConfiguration
@ComponentScan("com.baeldung.exceptiontesting") @ComponentScan("com.baeldung.exceptiontesting")
@SpringBootApplication @SpringBootApplication
public class ExceptionTestingApplication extends SpringBootServletInitializer { public class ExceptionTestingApplication extends SpringBootServletInitializer {
public static void main(final String[] args) { public static void main(final String[] args) {
SpringApplication.run(ExceptionTestingApplication.class, args); SpringApplication.run(ExceptionTestingApplication.class, args);
} }
} }

View File

@ -1,31 +1,31 @@
package com.baeldung.exceptiontesting.controller; package com.baeldung.exceptiontesting.controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.baeldung.exceptiontesting.exception.BadArgumentsException; import com.baeldung.exceptiontesting.exception.BadArgumentsException;
import com.baeldung.exceptiontesting.exception.InternalException; import com.baeldung.exceptiontesting.exception.InternalException;
import com.baeldung.exceptiontesting.exception.ResourceNotFoundException; import com.baeldung.exceptiontesting.exception.ResourceNotFoundException;
@RestController @RestController
public class ExceptionController { public class ExceptionController {
@GetMapping("/exception/{exception_id}") @GetMapping("/exception/{exception_id}")
public void getSpecificException(@PathVariable("exception_id") String pException) { public void getSpecificException(@PathVariable("exception_id") String pException) {
if("not_found".equals(pException)) { if("not_found".equals(pException)) {
throw new ResourceNotFoundException("resource not found"); throw new ResourceNotFoundException("resource not found");
} }
else if("bad_arguments".equals(pException)) { else if("bad_arguments".equals(pException)) {
throw new BadArgumentsException("bad arguments"); throw new BadArgumentsException("bad arguments");
} }
else { else {
throw new InternalException("internal error"); throw new InternalException("internal error");
} }
} }
@GetMapping("/exception/throw") @GetMapping("/exception/throw")
public void getException() throws Exception { public void getException() throws Exception {
throw new Exception("error"); throw new Exception("error");
} }
} }

View File

@ -1,13 +1,13 @@
package com.baeldung.exceptiontesting.exception; package com.baeldung.exceptiontesting.exception;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.ResponseStatus;
@SuppressWarnings("serial") @SuppressWarnings("serial")
@ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseStatus(HttpStatus.BAD_REQUEST)
public class BadArgumentsException extends RuntimeException { public class BadArgumentsException extends RuntimeException {
public BadArgumentsException(String message) { public BadArgumentsException(String message) {
super(message); super(message);
} }
} }

View File

@ -1,13 +1,13 @@
package com.baeldung.exceptiontesting.exception; package com.baeldung.exceptiontesting.exception;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.ResponseStatus;
@SuppressWarnings("serial") @SuppressWarnings("serial")
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public class InternalException extends RuntimeException { public class InternalException extends RuntimeException {
public InternalException(String message) { public InternalException(String message) {
super(message); super(message);
} }
} }

View File

@ -1,13 +1,13 @@
package com.baeldung.exceptiontesting.exception; package com.baeldung.exceptiontesting.exception;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.ResponseStatus;
@SuppressWarnings("serial") @SuppressWarnings("serial")
@ResponseStatus(HttpStatus.NOT_FOUND) @ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException { public class ResourceNotFoundException extends RuntimeException {
public ResourceNotFoundException(String message) { public ResourceNotFoundException(String message) {
super(message); super(message);
} }
} }

View File

@ -1,65 +1,65 @@
package com.baeldung.exceptiontesting.controller; package com.baeldung.exceptiontesting.controller;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import com.baeldung.exceptiontesting.controller.ExceptionController; import com.baeldung.exceptiontesting.controller.ExceptionController;
import com.baeldung.exceptiontesting.exception.BadArgumentsException; import com.baeldung.exceptiontesting.exception.BadArgumentsException;
import com.baeldung.exceptiontesting.exception.InternalException; import com.baeldung.exceptiontesting.exception.InternalException;
import com.baeldung.exceptiontesting.exception.ResourceNotFoundException; import com.baeldung.exceptiontesting.exception.ResourceNotFoundException;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@WebMvcTest(ExceptionController.class) @WebMvcTest(ExceptionController.class)
public class ExceptionControllerUnitTest{ public class ExceptionControllerUnitTest{
@Autowired @Autowired
private MockMvc mvc; private MockMvc mvc;
@Test @Test
public void givenNotFound_whenGetSpecificException_thenNotFoundCode() throws Exception { public void givenNotFound_whenGetSpecificException_thenNotFoundCode() throws Exception {
String exceptionParam = "not_found"; String exceptionParam = "not_found";
mvc.perform(get("/exception/{exception_id}", exceptionParam) mvc.perform(get("/exception/{exception_id}", exceptionParam)
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound()) .andExpect(status().isNotFound())
.andExpect(result -> assertTrue(result.getResolvedException() instanceof ResourceNotFoundException)) .andExpect(result -> assertTrue(result.getResolvedException() instanceof ResourceNotFoundException))
.andExpect(result -> assertEquals("resource not found", result.getResolvedException().getMessage())); .andExpect(result -> assertEquals("resource not found", result.getResolvedException().getMessage()));
} }
@Test @Test
public void givenBadArguments_whenGetSpecificException_thenBadRequest() throws Exception { public void givenBadArguments_whenGetSpecificException_thenBadRequest() throws Exception {
String exceptionParam = "bad_arguments"; String exceptionParam = "bad_arguments";
mvc.perform(get("/exception/{exception_id}", exceptionParam) mvc.perform(get("/exception/{exception_id}", exceptionParam)
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest()) .andExpect(status().isBadRequest())
.andExpect(result -> assertTrue(result.getResolvedException() instanceof BadArgumentsException)) .andExpect(result -> assertTrue(result.getResolvedException() instanceof BadArgumentsException))
.andExpect(result -> assertEquals("bad arguments", result.getResolvedException().getMessage())); .andExpect(result -> assertEquals("bad arguments", result.getResolvedException().getMessage()));
} }
@Test @Test
public void givenOther_whenGetSpecificException_thenInternalServerError() throws Exception { public void givenOther_whenGetSpecificException_thenInternalServerError() throws Exception {
String exceptionParam = "dummy"; String exceptionParam = "dummy";
mvc.perform(get("/exception/{exception_id}", exceptionParam) mvc.perform(get("/exception/{exception_id}", exceptionParam)
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isInternalServerError()) .andExpect(status().isInternalServerError())
.andExpect(result -> assertTrue(result.getResolvedException() instanceof InternalException)) .andExpect(result -> assertTrue(result.getResolvedException() instanceof InternalException))
.andExpect(result -> assertEquals("internal error", result.getResolvedException().getMessage())); .andExpect(result -> assertEquals("internal error", result.getResolvedException().getMessage()));
} }
@Test(expected = Exception.class) @Test(expected = Exception.class)
public void whenGetException_thenInternalServerError() throws Exception { public void whenGetException_thenInternalServerError() throws Exception {
mvc.perform(get("/exception/throw") mvc.perform(get("/exception/throw")
.contentType(MediaType.APPLICATION_JSON)); .contentType(MediaType.APPLICATION_JSON));
} }
} }