I'm creating restful api with springboot, hibernate crudRepository, spring security.
We plan to make :
ui test in java&selenium,
rest test Rest assured
Then what test should i make in backend? I'm plan to unit test but which? Services, controllers? Should use mockito to test service when mocking repository/dao or use datajpatest to memory db?
Both should be tested:
Integration test of controller using
MockMvc(https://howtodoinjava.com/spring-boot2/testing/spring-boot-mockmvc-example/)
Integration test of Service. Don't mock repository but use test db(h2). Mock only external services(for example client for payment connection).
Unit test could be for some other service that used in your service.
The main picture:Should be done end to end test integration test(main service that calls other services, controller) and during chain to the bottom of calls you could make unit tests
Related
I have a unit test annotated with #QuarkusTestResource that setups a Wiremock for an internal rest client. Can I inject the actual resource (the class implementing QuarkusTestResourceLifecycleManager) into the test somehow so I can access the Wiremock server to perform verifications on it?
I have an api(API 1) which is being stubbed through MockMvc.When I post on this API through this mock object, a request goes out to external system which in turn invokes api (API 2) of my system. Since this API 2 is invoked through http channel (host:port) and the container is not running, this breaks. How do I handle this scenario since I would not prefer to change the way external system invokes my API. Hope I have clarified.
If you're using MockMvc, you cannot test calls over the network.
So in that case, you would need to mock or stub the components that perform external network calls.
On the other hand, if you are using Spring Boot... you can then have Spring Boot's testing support launch the embedded Servlet container for the test, and externals calls can connect to the running Servlet container over HTTP. For that however, you would typically use something like Spring Boot's TestRestTemplate or core Spring's WebTestClient (available since Spring Framework 5.0) instead of MockMvc.
Currently, I am working on a project on Spring Boot where we are integrating with external REST API. As part of our integration suite test, we are doing the mock test of the actual external API which executes as part of the CI/CD.
My question is in production it calls the actual API so, how we can do that in the test environment. I don't think we need to make the actual external provider call during multiple integration test which will load the external API, also at the same time would like to test with actual REST response from the service.
Any suggestions?
If the public API has a swagger description, you could use the Atlassian Pact Swagger Validator. I describe the workflow in this talk: https://www.youtube.com/watch?v=79GKBYSqMIo#t=39m10s
Another alternative would be to create a mock API for the external service. There are some free services like https://mockfirst.com, https://www.mockable.io/, etc. where you can do that.
Currently I have one of the spring boot apps which is using wiremock for mocking the external services dependency in integration testing. However I have experienced that those tests are very fragile and unpredictable and takes lot of time to execute.
I want to know if there are better ways to mock external services dependency for integration testing purposes in spring boot application ?
TDD veterans seem to suggest that we must avoid mocking 3rd party code such as any framework code. Any non-trivial Spring based project will have dozens of Spring provided Beans injected and used. If Mocking 3rd party code is bad, what is the best way to write Unit Tests when the class depends on Spring provided Beans?
Don't mock then, use the real classes! Just as you wouldn't mock the String class.
That said, if you are developing a web application or a REST client, you should be aware that Spring provides classes that mock the web application server, for testing web applications, and the HTTP client, for testing REST clients.