Skip to content

Commit e9c2d78

Browse files
committed
Move controllers and service into associated test classes
1 parent 489d84f commit e9c2d78

File tree

7 files changed

+321
-328
lines changed

7 files changed

+321
-328
lines changed

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

Lines changed: 179 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@
44
import org.springframework.beans.factory.annotation.Autowired;
55
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
66
import org.springframework.security.test.context.support.WithMockUser;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.test.context.ContextConfiguration;
79
import org.springframework.test.web.servlet.MockMvc;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.ResponseBody;
813

914
import static org.hamcrest.Matchers.not;
1015
import static org.hamcrest.collection.IsIterableContainingInRelativeOrder.containsInRelativeOrder;
1116
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
1217
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
1318
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
1419

15-
@WebMvcTest(TestController.class)
20+
@WebMvcTest(HtmxHandlerInterceptorTest.TestController.class)
21+
@ContextConfiguration(classes = HtmxHandlerInterceptorTest.TestController.class)
1622
@WithMockUser
1723
class HtmxHandlerInterceptorTest {
1824

@@ -231,4 +237,176 @@ public void testHxReselect() throws Exception {
231237
.andExpect(header().string("HX-Reselect", "#target"));
232238
}
233239

240+
@Controller
241+
@RequestMapping("/")
242+
static class TestController {
243+
244+
@GetMapping("/without-trigger")
245+
@ResponseBody
246+
public String methodWithoutAnnotations() {
247+
return "";
248+
}
249+
250+
@GetMapping("/with-trigger")
251+
@HxTrigger("eventTriggered")
252+
@ResponseBody
253+
public String methodWithHxTrigger() {
254+
return "";
255+
}
256+
257+
@GetMapping("/with-trigger-multiple-events")
258+
@HxTrigger({ "event1", "event2" })
259+
@ResponseBody
260+
public String methodWithHxTriggerAndMultipleEvents() {
261+
return "";
262+
}
263+
264+
@GetMapping("/with-trigger-after-settle")
265+
@HxTriggerAfterSettle("eventTriggered")
266+
@ResponseBody
267+
public String methodWithHxTriggerAfterSettle() {
268+
return "";
269+
}
270+
271+
@GetMapping("/with-trigger-after-settle-multiple-events")
272+
@HxTriggerAfterSettle({ "event1", "event2" })
273+
@ResponseBody
274+
public String methodWithHxTriggerAfterSettleAndMultipleEvents() {
275+
return "";
276+
}
277+
278+
@GetMapping("/with-trigger-after-swap")
279+
@HxTriggerAfterSwap("eventTriggered")
280+
@ResponseBody
281+
public String methodWithHxTriggerAfterSwap() {
282+
return "";
283+
}
284+
285+
@GetMapping("/with-trigger-after-swap-multiple-events")
286+
@HxTriggerAfterSwap({ "event1", "event2" })
287+
@ResponseBody
288+
public String methodWithHxTriggerAfterSwapAndMultipleEvents() {
289+
return "";
290+
}
291+
292+
@GetMapping("/with-triggers")
293+
@HxTrigger({ "event1", "event2" })
294+
@HxTriggerAfterSettle({ "event1", "event2" })
295+
@HxTriggerAfterSwap({ "event1", "event2" })
296+
@ResponseBody
297+
public String methodWithHxTriggers() {
298+
return "";
299+
}
300+
301+
@GetMapping("/updates-sidebar")
302+
@HxUpdatesSidebar
303+
@ResponseBody
304+
public String updatesSidebar() {
305+
return "";
306+
}
307+
308+
@GetMapping("/hx-trigger-alias-for")
309+
@HxTriggerWithAliasFor(event = "updateTrigger")
310+
@ResponseBody
311+
public String hxTriggerWithAliasForOverride() {
312+
return "";
313+
}
314+
315+
@GetMapping("/hx-refresh")
316+
@HxRefresh
317+
@ResponseBody
318+
public String hxRefresh() {
319+
return "";
320+
}
321+
322+
@GetMapping("/hx-vary")
323+
@ResponseBody
324+
public String hxVary() {
325+
return "";
326+
}
327+
328+
@GetMapping("/hx-location-without-context-data")
329+
@HxLocation("/path")
330+
@ResponseBody
331+
public String hxLocationWithoutContextData() {
332+
return "";
333+
}
334+
335+
@GetMapping("/hx-location-with-context-data")
336+
@HxLocation(path = "/path", source = "source", event = "event", handler = "handler", target = "target", swap = "swap", select = "select")
337+
@ResponseBody
338+
public String hxLocationWithContextData() {
339+
return "";
340+
}
341+
342+
@GetMapping("/hx-push-url-path")
343+
@HxPushUrl("/path")
344+
@ResponseBody
345+
public String hxPushUrlPath() {
346+
return "";
347+
}
348+
349+
@GetMapping("/hx-push-url")
350+
@HxPushUrl
351+
@ResponseBody
352+
public String hxPushUrl() {
353+
return "";
354+
}
355+
356+
@GetMapping("/hx-push-url-false")
357+
@HxPushUrl(HtmxValue.FALSE)
358+
@ResponseBody
359+
public String hxPushUrlFalse() {
360+
return "";
361+
}
362+
363+
@GetMapping("/hx-redirect")
364+
@HxRedirect("/path")
365+
@ResponseBody
366+
public String hxRedirect() {
367+
return "";
368+
}
369+
370+
@GetMapping("/hx-replace-url-path")
371+
@HxReplaceUrl("/path")
372+
@ResponseBody
373+
public String hxReplaceUrlPath() {
374+
return "";
375+
}
376+
@GetMapping("/hx-replace-url")
377+
@HxReplaceUrl
378+
@ResponseBody
379+
public String hxReplaceUrl() {
380+
return "";
381+
}
382+
@GetMapping("/hx-replace-url-false")
383+
@HxReplaceUrl(HtmxValue.FALSE)
384+
@ResponseBody
385+
public String hxReplaceUrlFalse() {
386+
return "";
387+
}
388+
389+
@GetMapping("/hx-reswap")
390+
@HxReswap(value = HxSwapType.INNER_HTML, swap = 300)
391+
@ResponseBody
392+
public String hxReswap() {
393+
return "";
394+
}
395+
396+
@GetMapping("/hx-retarget")
397+
@HxRetarget("#target")
398+
@ResponseBody
399+
public String hxRetarget() {
400+
return "";
401+
}
402+
403+
@GetMapping("/hx-reselect")
404+
@HxReselect("#target")
405+
@ResponseBody
406+
public String hxReselect() {
407+
return "";
408+
}
409+
410+
}
411+
234412
}

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

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
77
import org.springframework.security.test.context.support.WithMockUser;
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.stereotype.Service;
10+
import org.springframework.test.context.ContextConfiguration;
811
import org.springframework.test.context.bean.override.mockito.MockitoBean;
912
import org.springframework.test.web.servlet.MockMvc;
13+
import org.springframework.web.bind.annotation.GetMapping;
14+
import org.springframework.web.bind.annotation.RequestMapping;
15+
import org.springframework.web.bind.annotation.ResponseBody;
1016

1117
import static org.assertj.core.api.Assertions.assertThat;
1218
import static org.mockito.Mockito.verify;
1319
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
1420
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
1521

1622

17-
@WebMvcTest(HtmxHandlerMethodArgumentResolverTestController.class)
23+
@WebMvcTest(HtmxHandlerMethodArgumentResolverTest.TestController.class)
24+
@ContextConfiguration(classes = HtmxHandlerMethodArgumentResolverTest.TestController.class)
1825
@WithMockUser
1926
class HtmxHandlerMethodArgumentResolverTest {
2027
@Autowired
@@ -165,21 +172,70 @@ void testHxRequestAnnotation() throws Exception {
165172
mockMvc.perform(get("/method-arg-resolver/users")
166173
.header("HX-Request", "true"))
167174
.andExpect(view().name("users :: list"));
168-
;
169175
}
170176

171177
@Test
172178
void testHxRequestAnnotationInheritance() throws Exception {
173179
mockMvc.perform(get("/method-arg-resolver/users/inherited")
174180
.header("HX-Request", "true"))
175181
.andExpect(view().name("users :: list"));
176-
;
177182
}
178183

179184
@Test
180185
void testHxRequestSameUrlNoAnnotation() throws Exception {
181186
mockMvc.perform(get("/method-arg-resolver/users"))
182187
.andExpect(view().name("users"));
183-
;
184188
}
189+
190+
@Controller
191+
@RequestMapping("/method-arg-resolver")
192+
static class TestController {
193+
194+
@Autowired
195+
private TestService service;
196+
197+
@GetMapping
198+
@ResponseBody
199+
public String htmxRequestDetails(HtmxRequest details) {
200+
service.doSomething(details);
201+
202+
return "";
203+
}
204+
205+
@GetMapping("/users")
206+
@HxRequest
207+
public String htmxRequest(HtmxRequest details) {
208+
service.doSomething(details);
209+
210+
return "users :: list";
211+
}
212+
213+
@GetMapping("/users")
214+
public String normalRequest(HtmxRequest details) {
215+
service.doSomething(details);
216+
217+
return "users";
218+
}
219+
220+
@HxGetMapping("/users/inherited")
221+
public String htmxRequestInheritance(HtmxRequest details) {
222+
service.doSomething(details);
223+
224+
return "users :: list";
225+
}
226+
227+
@GetMapping("/users/inherited")
228+
public String normalRequestInheritance(HtmxRequest details) {
229+
service.doSomething(details);
230+
231+
return "users";
232+
}
233+
}
234+
235+
@Service
236+
public class TestService {
237+
void doSomething(HtmxRequest details) {
238+
}
239+
}
240+
185241
}

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

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)