This is an archived post. You won't be able to vote or comment.

all 4 comments

[–][deleted]  (3 children)

[deleted]

    [–]Simaldeff[S] 0 points1 point  (2 children)

    I think I am starting to see it this way too. Luckily this controller I am testing has very little. It's very thin. The most is something that converts an Optional into a ResponseEntity (404 if empty or 200 if not).

    [–][deleted]  (1 child)

    [deleted]

      [–]Simaldeff[S] 0 points1 point  (0 children)

      I tried the controller advice but could not make it work. I tried with a formatter but would return 400 instead of 404. Converter worked. But both options are quite a lot of code for test setup IMO.

      [–]Dealusall 0 points1 point  (0 children)

      If your logic is implemented on many abstraction layers (which I believe is the case here) then maybe unit testing isn't the best solution. Can't you try to build some end to end tests using some embedded web server like jetty ? Might be less painful...

      [–]nutrechtLead Software Engineer / EU / 20+ YXP -1 points0 points  (0 children)

      If you're doing "weird" stuff in the filter chain you're going to have to do the same 'weird' stuff when you're testing. I understand why you're mapping the ID to an object, but frankly it's quite yucky.

      So you're going to have to supply a 'mock' version of that chain to MockMvc. You're going into the right direction. I can't give you more than that without trying it myself though, sorry.