Skip to content

Commit

Permalink
Merge pull request quarkusio#16342 from geoand/quarkusio#16321
Browse files Browse the repository at this point in the history
Add test for case where @ResponseStatus is used along with @ExceptionHandler
  • Loading branch information
geoand authored Jun 22, 2021
2 parents b89c9db + 67ccb5c commit 4b3f6f9
Showing 1 changed file with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.quarkus.spring.web.test;

import static io.restassured.RestAssured.when;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import io.quarkus.test.QuarkusUnitTest;

public class ResponseStatusAndExceptionHandlerTest {

@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(ExceptionController.class, RestExceptionHandler.class));

@Test
public void testRootResource() {
when().get("/exception").then().statusCode(400);
}

@RestController
@RequestMapping("/exception")
public static class ExceptionController {

@GetMapping
@ResponseStatus(HttpStatus.OK)
public String throwException() {
throw new RuntimeException();
}
}

@RestControllerAdvice
public static class RestExceptionHandler {

@ExceptionHandler(RuntimeException.class)
public ResponseEntity<Object> handleException(Exception ex) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
}

0 comments on commit 4b3f6f9

Please sign in to comment.