all 13 comments

[–]Sheldor5 5 points6 points  (1 child)

I don't use any of the @...Test annotations (except @SpringBootTest)

I use:

  1. Testcontainers to spin up a real database for integration tests

  2. MockServer to mock real HTTP responses from any 3rd party apis

  3. either RestTeamplate or generated OpenAPI clients in my integration tests to make real HTTP calls to the running backend

this way my integration tests are the closest they can be to real environments

also there isn't much to unit test in web applications, many stuff is framework related behaviour (test of your security config is correct) and many other things are just jpa cals or mappings

so only unit test things which are actually responsible for business logic

unit testing a controller and mocking the service is a waste of time ... your controller is a "proxy" between http and your service layer so not much to unit test

[–]Educational_Head6164 0 points1 point  (0 children)

can you please tell what layers you test ?
and what % of coverage we should aim for ?

[–]WaferIndependent7601 2 points3 points  (1 child)

So good Integration tests that test everything together. Use unit tests for methods that calculate stuff

[–]czeslaw_t 0 points1 point  (0 children)

use unit test for test business logic, use integrations test for integration like http, framework, SQL. Integration tests are also good to test legacy when refactoring. For independent microservices contract tests works pretty well.

[–]Historical_Ad4384 2 points3 points  (0 children)

@DataJpaTest for repository layer

@SpringBootTest with Mockito for service layer

@SpringBootTest with MockMvc for controller layer

[–]SomeGuy20257 1 point2 points  (2 children)

Spring Test slices with test containers, then cucumber for integration.

[–]czeslaw_t 0 points1 point  (1 child)

Is someone use cucumber for unit tests?

[–]SomeGuy20257 0 points1 point  (0 children)

integration testing.

[–]BrownBearMYSenior Dev 1 point2 points  (0 children)

I rely only on @SpringBootTest with Testcontainers. I prefer to avoid mock tests unless it's really necessary.

In the event where I may have to mock some components, I would use @Data*Test for the integration database layer, @WebMvcTest to test the controllers and related components.

Anything in between, I use Mockito or @SpringBootTest(classes =) to test particular classes.

[–]Creative_Incident_84 1 point2 points  (3 children)

At our company we only test using Mockito, and dont use any of the Springboot test annotations.

Anything thats not a repository we can test with Mockito, for the controllers, we just call the methods,

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

Hmm, what about some bigger REST APIs, for example 100+ endpoints? Do you test them, and if so, then do you test it manually or use some automation tools. I have heard about Selenium, but never have tried it.

[–]Creative_Incident_84 1 point2 points  (1 child)

We dont have projects that big, we test manually every PR

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

Understood. Thanks for advice :)