Skip to content

Commit 84165f6

Browse files
Update tests for annotation processing
1 parent 413e8a0 commit 84165f6

6 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2025-2026 The Problem4J Authors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all
11+
* copies or substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
* SOFTWARE.
20+
*/
21+
22+
package io.github.problem4j.spring.webflux.app.problem;
23+
24+
import io.github.problem4j.core.ProblemMapping;
25+
import org.springframework.http.HttpStatus;
26+
import org.springframework.web.bind.annotation.ResponseStatus;
27+
28+
@ResponseStatus(HttpStatus.FORBIDDEN)
29+
@ProblemMapping(status = 418, title = "Both Annotated Exception")
30+
public class BothAnnotatedException extends RuntimeException {}

problem4j-spring-webflux/src/test/java/io/github/problem4j/spring/webflux/app/rest/ResponseStatusAnnotatedController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
package io.github.problem4j.spring.webflux.app.rest;
2323

24+
import io.github.problem4j.spring.webflux.app.problem.BothAnnotatedException;
2425
import io.github.problem4j.spring.webflux.app.problem.ForbiddenAnnotatedException;
2526
import io.github.problem4j.spring.webflux.app.problem.ReasonAnnotatedException;
2627
import org.springframework.web.bind.annotation.GetMapping;
@@ -40,4 +41,9 @@ public String responseStatusAnnotated() {
4041
public String reasonAnnotated() {
4142
throw new ReasonAnnotatedException();
4243
}
44+
45+
@GetMapping("/both-annotated")
46+
public String bothAnnotated() {
47+
throw new BothAnnotatedException();
48+
}
4349
}

problem4j-spring-webflux/src/test/java/io/github/problem4j/spring/webflux/integration/ResponseStatusAnnotatedExceptionWebFluxTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,19 @@ void givenSpringNativeResponseStatusAnnotationWithReason_shouldReturnProblem() {
7070
.value(notNullValue())
7171
.isEqualTo(Problem.of(HttpStatus.FORBIDDEN.value(), "this is reason"));
7272
}
73+
74+
@Test
75+
void givenBothResponseStatusAndProblemMappingAnnotations_shouldPreferProblemMapping() {
76+
webTestClient
77+
.get()
78+
.uri("/response-status-annotated/both-annotated")
79+
.exchange()
80+
.expectStatus()
81+
.isEqualTo(HttpStatus.valueOf(418))
82+
.expectHeader()
83+
.contentType(Problem.CONTENT_TYPE)
84+
.expectBody(Problem.class)
85+
.value(notNullValue())
86+
.isEqualTo(Problem.builder().status(418).title("Both Annotated Exception").build());
87+
}
7388
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2025-2026 The Problem4J Authors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all
11+
* copies or substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
* SOFTWARE.
20+
*/
21+
22+
package io.github.problem4j.spring.webmvc.app.problem;
23+
24+
import io.github.problem4j.core.ProblemMapping;
25+
import org.springframework.http.HttpStatus;
26+
import org.springframework.web.bind.annotation.ResponseStatus;
27+
28+
@ResponseStatus(HttpStatus.FORBIDDEN)
29+
@ProblemMapping(status = 418, title = "Both Annotated Exception")
30+
public class BothAnnotatedException extends RuntimeException {}

problem4j-spring-webmvc/src/test/java/io/github/problem4j/spring/webmvc/app/rest/ResponseStatusAnnotatedController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
package io.github.problem4j.spring.webmvc.app.rest;
2323

24+
import io.github.problem4j.spring.webmvc.app.problem.BothAnnotatedException;
2425
import io.github.problem4j.spring.webmvc.app.problem.ForbiddenAnnotatedException;
2526
import io.github.problem4j.spring.webmvc.app.problem.ReasonAnnotatedException;
2627
import org.springframework.web.bind.annotation.GetMapping;
@@ -40,4 +41,9 @@ public String responseStatusAnnotated() {
4041
public String reasonAnnotated() {
4142
throw new ReasonAnnotatedException();
4243
}
44+
45+
@GetMapping("/both-annotated")
46+
public String bothAnnotated() {
47+
throw new BothAnnotatedException();
48+
}
4349
}

problem4j-spring-webmvc/src/test/java/io/github/problem4j/spring/webmvc/integration/ResponseStatusAnnotatedExceptionWebMvcTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,19 @@ void givenSpringNativeResponseStatusAnnotationWithReason_shouldReturnProblem() t
6868

6969
assertThat(problem).isEqualTo(Problem.of(HttpStatus.FORBIDDEN.value(), "this is reason"));
7070
}
71+
72+
@Test
73+
void givenBothResponseStatusAndProblemMappingAnnotations_shouldPreferProblemMapping()
74+
throws Exception {
75+
ResponseEntity<String> response =
76+
restTemplate.getForEntity("/response-status-annotated/both-annotated", String.class);
77+
78+
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.valueOf(418));
79+
assertThat(response.getHeaders().getContentType()).hasToString(Problem.CONTENT_TYPE);
80+
81+
Problem problem = objectMapper.readValue(response.getBody(), Problem.class);
82+
83+
assertThat(problem)
84+
.isEqualTo(Problem.builder().status(418).title("Both Annotated Exception").build());
85+
}
7186
}

0 commit comments

Comments
 (0)