Accept external REST calls from SpringBootTest context and verify the call - java

I am writing Spring Boot Integration test. For this, we boot up the empty application (context) and call multiple services as part of test.
One of the requirements is to call the external REST endpoint with payload that contains an URL to notify (call back) and that service, after some business logic, calls the URL received in the payload.
From the test, I can call the REST endpoint, which is external service. But I want to be able to test the call back from that service for the given URL. URL can be random.
Can I do with MockClientServer? OR MockRestServiceServer Or some other day to test this?
Essentially, I want the test to be able to get an external call and verify it.

After trying out a couple things, WireMock actually does what I wanted. It takes the rest call from outside application(s). Very useful for testing.

Related

How to Mock an external API that an internal API calls?

I am having trouble on how to Mock an external API that an internal API calls.
What I want is basically to test the call on the internal API, to see if it reaches the point of the external API path. I am using Mockito with java and can't seem to get it working. I want to intercept the External API call and return a mock response so the external api isn't hit.
Here is what I have tried:
I am using Micronaut using HttpClients in my tests to do something like:
HttpResponse<blah> response = client.toBlocking().exchange(request, blah.class);
, which is the internal API call. The external api call is in the form of:
HttpResponse<blah> response = client.toBlocking().exchange(request, blah.class, error.class);
I am trying to catch the external API call in my Integration test class by:
when(client.toBlocking().exchange(any(MutableHttpRequest.class), any(Argument.class), any(Argument.class))).thenReturn(resp);
But it seems like I cannot even reach the internal api, the call never seems to be made. I believe that it is being intercepted earlier.
Any help or suggestions are appreciated - I am not sure if this is the right approach or if there's some easier way, but thank you in advance.

How karate mocks the external integrations URLS [duplicate]

This question already has an answer here:
In Karate API mocking not working as expected for me
(1 answer)
Closed 1 year ago.
I have a question about external mocking server. My set up is:
I have an API which I want to test
The service internally calls Database , Gateways , payment aggregators whichs have their own URLS
I control the Mock URLS which I can call. But if it is internally initiated how can i mock it without changing my code?
For example I call service
I call Controller of paymentservice which I can mock
What about my controller call java module which makes a call to gateway
I want to mock that gateway not controller. I see all the examples of karate-netty and Proxy . Proxy tracks all the request after host:port but in my case the host will be real host and how proxy will track it ?
Looks like I tried so much but did not get any perfect solution
This is not a question regarding karate, because you want to make your code more testable.
Your controller that call other services have to know how to request service. I would expect that you have at leas some sort of configuration file where all the urls and other application properties are specified.
In more complex environment, I would expect some sort of service discovery with consul for instance.
The simplest thing you can do is to read a system or environment property in your controller to make the service url configurable.

How to verify the token on every endpoint call except one using javax.ws.rs running on Tomcat?

I'm making a RESTful backend using javax.ws.rs running on Tomcat, and the next step is to add the validation of tokens and every call will validate the token except for the call to create a user. I already have a code that runs for every call to take care of the CORS but I don't know what is the best way to exclude only the call to create users (and maybe another call), I will like to avoid the same line on every endpoint (to validate the token only requires two lines, it's not much but I don't like repetition).
You can write a filter which will validate every incoming request to you service. In that filter, you can check if the token is valid or not.

How to set wiremock to mock specified url

I have microservice based app, and I need to mock one of the service.
So when Im requesting one microservice http://service1/src it triggers another one by link lets say
http://service2/src
I cannot change this link to the mocked one but I want to start wiremock somehow and fake this link, so when I will send request to http://service1/src it will trigger http://service2/src but mocked one
Im able to setup wiremock but I cannot get it directly by link http://service2/src I need to add http://localhost:8080/http://service2/src
Or maybe you can advice some another framework for such purpose
thanks for helping in advance

Using RestTemplate with local json file

Background
I'm writing a web service that makes calls to an external api. This api has not yet been put into production. As such, when I make a call to my web service in my dev environment, I want to stub out the responses that it returns. Note: this is not a unit testing question.
My Solution So Far
The api calls are made using RestTemplate from the lovely Spring people, the url of which is held in application.properties. This has allowed me to set different urls for different environments using Active Profiles. So, for example, application-dev.properties holds a different url.
The dev url is ideally a pointer to a json file under resources/.
My Issue
I can't seem to get RestTemplate to pick up the local json file. The url I'm using is:
url = "file://staticJson.json"
However that comes up with a
Object of class [sun.net.www.protocol.ftp.FtpURLConnection] must be an instance of class java.net.HttpURLConnection
And now I'm unsure of how to proceed, or if this is even possible without extending RestTemplate.
Any directions to try or solutions would be fantastic.
If any more information is required I'll do my best to provide it asap.

Categories

Resources