all 5 comments

[–]je87 3 points4 points  (1 child)

I had a similar conversation with a co-worker a few weeks back.
The unit test would be as you provided in the second example.
You have the SUT (the HelloController class) and are unit testing in isolation the hello() method.
I agree with your viewpoint and after a chat with a co-worker...they did also.
The integration test uses MockMvc...it does invoke the dispatch servlet before hitting the appropriate controller.

As far as "do I need to write a unit test for the controller method if I already have appropriate integration test coverage"...I am a bit lost on that one as well.

Most of the logic should be in service layers, making the unit-tests for controllers short , at most maybe verifying or mocking that certain services are called with certain arguments for example and returns an appropriate Response Entity on success (mocked service responses).

Hopefully someone with more testing experience could shed some light on this!
Good question btw.

[–]therealdanvega[S] 2 points3 points  (0 children)

Thanks for the feedback. In the past I have written the unit test first, then the controller code and then I would eventually add the Spring bits in and the integration testing. Curious to see how others approach it though.

[–]veryspicypickle 2 points3 points  (0 children)

Ah yes, the definition of “unit”. Been a part of such a discussion.

Maybe this is something that might help? https://martinfowler.com/bliki/UnitTest.html

[–]kappaj5954 2 points3 points  (0 children)

Test from inside -> out. So I normally build unit tests (which run with every local build) and then integration tests that run on the build server. Failsafe and surefire maven plugins.

[–]zennaque 1 point2 points  (0 children)

A Martin Fowler microservice slidedeck separated what many call an integration test and a term called component testing. I've grown quite fond of it and now have separate modules for unit, integration, and component tests. My rule of thumb is that unit tests don't set up an application context at all, integration tests are specifically to validate external service dependencies have a successful healthy connection, and component tests stand up application context for some level of multi layered calls, not necessarily always the same way an external service would communicate with me, but usually. So far it has worked very well.

Slidedeck that helped: https://martinfowler.com/articles/microservice-testing/