For What
When we developing a project and serving for the user with a lot of function. And some of this function is called in multiple place. when a new requirement is come which need to modify the code of the old function. How can you ensure the ensure old function won’t be broken by the change you make. human test? too native. Human always make mistake. So we need the machine to check for us.
Layer Test (Deprecate)
we use mockito to mock all the dependencies injected (the database, the third party api) to the layer, in the Controller layer , we mock the Service function, in the Service layer, we mock the Dao function;
Controller:
1 |
|
Service:
1 |
|
Conclusion
Good: only test the necessary part.
Bad: it take a lot of effort for all the layer for same functionality.
Example:
https://howtodoinjava.com/spring-boot2/testing/rest-controller-unit-test-example/
https://howtodoinjava.com/spring-boot2/testing/spring-boot-mockito-junit-example/
Integration Test : TestContainer + SQL Initialization (Better)
Because the Layer Test is cause a lot of effort, so it is easy to give up. and then the Integration test came out
Integration test can test different modules are bounded correctly and if they work as expected in only one test case, no need to write for different layer , save a lot of effort.
**Integration test can more similar as the prod by using the testContainer or embedded db(H2).**
Integration test can test for certain behaviors by using the @sql to initial to test data to db.
1 |
|
Conclusion
integration test is a better choice
Example:
https://howtodoinjava.com/spring-boot2/testing/spring-integration-testing/