Access or inject QuarkusTestResource in the actual test? - quarkus

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?

Related

Springboot RESTful API | what should i test

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

spring mock mvc tests with api being invoked from external system

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.

Testing REST API provider response without mock

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.

Stub for Feign client for integration testing

I have a spring cloud project with the following packaging structure
Controller (publishes Rest Endpoint)-->flow (business logic)-->service (calls Feign client with hysterix fallback setup )--> Feign client.
Auto-wiring is done in respective classes e.g. flow is auto-wired in controller and service is auto-wired in flow and so on.
I want to perform integration test, by calling the endpoint published by the controller. The problem is I don't have endpoint accessed by the feign client at the moment (neither original nor spring cloud contract stub is available).
How do I stub the call made by feign client in this case.
You can use Spring Cloud WireMock support and set up an endpoint manually before the tests are called. In the feign configuration you can point manually to an IP and port. The problem is that this test is pretty much useless cause as a consumer you're mocking the producer.
UPDATE
You have a Feign client that will be used to call some external API. What you can do is you can use Spring Cloud WireMock (or just WireMock) to setup a mock of that API. Then you can teach WireMock to behave as you wish and assert whether your client works fine. The problem with such an approach is such that since you, as a client, are setting up the WireMock instance, you can teach it to behave in the way that has nothing to do with the real API. For example you state that if you send a request to endpoint /foo with a method GET then you should get back "BAR" in the response. Then you write a test where your client sends GET # /foo and assert that BAR got properly returned. However that doesn't mean that the other API indeed has that endpoint. So this approach can give you false-positives. You can however use WireMock to assert whether you can properly react to faulty responses like malformed response etc.
In such cases, if you really want to check if you can communicate properly with an API that you don't control, is that you can write tests that will call that real API via a WireMock proxy, you record that traffic and convert it into stubs. You can watch about this more in my presentation here https://www.youtube.com/watch?v=ZyHG-VOzPZg

How to inject hk2 servicelocator in a jersey jax-rs app

I am migrating a legacy service framework that uses java serialization/reflection to register services and call them as remote endpoints (javabin over http) or local calls seamlessly.
I have replaced the remote calls as jersey endpoints. Also used hk2 aop based interceptor to recreate some of the interception abilities of current framework. It is working fine when remote client makes a call to the endpoint.
Now I need to do the same for local calls to service methods and be able to use the same intercept logic as the remote service method calls.
(implemented org.glassfish.hk2.api.InterceptionService - based on this article - https://blog.dejavu.sk/2015/01/21/intercepting-jersey-resource-method-calls/)
I believe if I am able to get the instance of service(jax-rs resource) class from hk2 registry/locator, then method calls will invoke the same registered interceptors. How to achieve it?
As said by Paul in the comments you can always just #Inject or lookup the ServiceLocator. In fact, it's the service with the lowest ID (the first service added to the hk2 system).
In the case of children locators you will normally get the child ServiceLocator before the parent ServiceLocator unless you do something very specific with your query. This is because the rule for duplicate services is to pick the one with the highest locator id (which will be the child one)

Resources