Skip to content

Commit 2b45a1f

Browse files
committed
test: Add test that annotation on exception handler overwrites the annotation on the controller method if there is an exception
Fixes #150
1 parent ee74bcd commit 2b45a1f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

htmx-spring-boot/src/test/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxHandlerMethodTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ private static HttpHeaders htmxRequest() {
3333
return headers;
3434
}
3535

36+
@Test
37+
public void testExceptionHandlerWithOverride() throws Exception {
38+
39+
mockMvc.perform(get("/throw-exception-annotation-override").headers(htmxRequest()))
40+
.andExpect(status().isOk())
41+
.andExpect(header().string("HX-Retarget", "#container"))
42+
.andExpect(content().string("View1\n"));
43+
}
44+
3645
@Test
3746
public void testExceptionHandler() throws Exception {
3847

@@ -253,6 +262,12 @@ public HtmxView handleError(TestExceptionForHtmxViewHandler ex, HtmxRequest htmx
253262
return new HtmxView("view1");
254263
}
255264

265+
@ExceptionHandler(TestExceptionForAnnotatedHandlerWithAnnotationOverride.class)
266+
@HxRetarget("#container")
267+
public String handleError(TestExceptionForAnnotatedHandlerWithAnnotationOverride ex, HtmxRequest htmxRequest, HtmxResponse htmxResponse) {
268+
return "view1";
269+
}
270+
256271
@ExceptionHandler(TestExceptionForAnnotatedHandler.class)
257272
@HxRetarget("#container")
258273
public String handleError(TestExceptionForAnnotatedHandler ex, HtmxRequest htmxRequest, HtmxResponse htmxResponse) {
@@ -433,6 +448,13 @@ public void throwExceptionHtmxView() {
433448
throw new TestExceptionForHtmxViewHandler();
434449
}
435450

451+
@HxRequest
452+
@GetMapping("/throw-exception-annotation-override")
453+
@HxRetarget("controller-method-target")
454+
public void throwExceptionAnnotationOverride() {
455+
throw new TestExceptionForAnnotatedHandlerWithAnnotationOverride();
456+
}
457+
436458
@HxRequest
437459
@GetMapping("/throw-exception-annotated")
438460
public void throwExceptionForAnnotatedHandler() {
@@ -458,4 +480,9 @@ static class TestExceptionForAnnotatedHandler extends RuntimeException {
458480
static class TestExceptionForAnnotatedHandlerWithHtmxView extends RuntimeException {
459481

460482
}
483+
484+
static class TestExceptionForAnnotatedHandlerWithAnnotationOverride extends RuntimeException {
485+
486+
}
487+
461488
}

0 commit comments

Comments
 (0)