Using Karate, can the .feature Mock files intercept calls from back-end Java service class? - karate

Question about Karate: can the .feature Mock files intercept calls from back-end Java service class?
I think the mocking feature is pretty easy to understand. I can see that you can start the mock server in the background step and then your scenario HTTP calls will be intercepted BUT
My question is: is it possible to intercept HTTP calls coming from my back-end service class after I hit my API endpoint with a Karate step?
If I don't get an answer, I will put together a sample project and experiment. Just hoping someone knows off-hand. If not, I think I can probably run an instance of Mountebank.

It may be possible by telling the JVM to use an HTTP proxy, but it depends a lot on the system under test.
You can find more details here: https://stackoverflow.com/a/61414022/143475

Related

Mock Open API endpoints that haven‘t been implemented yet?

Mocking endpoints an Open API Specification file defines isn't all that difficult.
However I am trying to mock only the endpoints that don't have an implementation yet.
For example I have an OAS file that defines an GET /dogs and an GET /cats endpoint.
Now when I implement GET /dogs in a - for example - Nest.js application and that application is running, I would want to have a mock server that only mocks the GET /cats endpoint.
That would make it possible to bundle the mock server and the application and to deploy them together, so that some form of response (either the implementation or the mocked) is always returned for every endpoint.
Is there some sort of tool/mock-server that has this kind of ability? Would doing something like that even make sense?

Karate test framework: besides SOAP and REST also JMS calls? [duplicate]

This question already has an answer here:
How can I integrate socket.io on Karate
(1 answer)
Closed 1 year ago.
We are using ReadyAPI for API testing, Mocking and JMeter for performance testing and looking into the possibility to migrate everything to Karate framework. Would be handy to have all in one open source framework. But the main question is: can Karate framework handle JMS calls? Because 80% of our service testing is via JMS (with Hermes in ReadyAPI). I know SOAP and REST are supported but can't find anything about JMS.
Yes, you have to write a Java adapter (one-time work).
Please look at these 2 references:
https://twitter.com/getkarate/status/1128170638223364097
A great example that shows off Karate's
Java / JS interop - and built-in async support
first we call custom code to listen to an ApacheActiveMQ queue
an HTTP POST is made
we wait for the JMS message
and assert that the message is as expected
https://github.com/intuit/karate/tree/master/karate-netty#consumer-provider-example
https://twitter.com/getkarate/status/1417023536082812935
How Java interop and mocks can come together for advanced async / messaging flows such as JMS or apachekafka
link to full example: https://github.com/intuit/karate/tree/develop/karate-demo/src/test/java/mock/async

what does Swagger server stub mean?

What does the term Server Stub mean in the context of the Swagger ecosystem? How is it used?
From a swagger tutorial:
With SwaggerHub, you can easily generate a server stub (an API implementation stub) for Node.js, ASP.NET, JAX-RS, and other servers
and frameworks. The server stub is a good starting point for
implementing your API – you can run and test it locally, implement the
business logic for your API, and then deploy it to your server.
https://app.swaggerhub.com/help/apis/generating-code/server-stub
and a stub is:
method stub or simply stub in software development is a piece of code used to stand in for some other programming functionality. A stub may simulate the behavior of existing code (such as a procedure on a remote machine, such methods are often called mocks) or be a temporary substitute for yet-to-be-developed code. Stubs are therefore most useful in porting, distributed computing as well as general software development and testing.
https://en.wikipedia.org/wiki/Method_stub
Stub the API means : create à mock to serve examples described in swagger file.
This mock can be formatted in specific languages/ framework
Server stubbing can be quite powerful depending on the backend platform and framework you plan to use for your API.
For example, you may choose Apache (common in Linux environments) or ASP.NET (common for IIS). The server "stubs" being generated will typically be a deployable library to that specific platform. What you typically get is:
Routing to your business logic. The framework will handle the HTTP specification, but actually mapping from a "controller" to your service layer is being handled by the code generator, based on your API specification.
Serialization and Deserialization of your models (applies to strongly-typed languages like Java/C#).
AuthN/AuthZ may be handled, to some degree, based on the framework's support for your API's chosen auth scheme.
tl;dr: A server stub is intended to be a ready-to-deploy application that routes HTTP requests to your actual business logic on the backend.
From my experience and peers, I found stub to be a mock function or a placeholder function where you can fill in the proper implementation later.

Mocking a web-service

I have to integrate with a third-party web-service (behind firewall), and I do have their WSDL and proxy class.
I want to develop the client stuff outside the firewall.
What is the best approach to mock the web-service to ease integration with them?
Do I create a web-service project on my side? Somehow use their proxy classes ad mock the methods? This would create the service references so I can just change the target URL when the time comes.
Or do I create a service layer that returns mock classes in my dev. environment but would use real web-services at run-time?
The former approach would take a lot of work, I would think.
Any ideas?
With just the WSDL, you could host a mock service using soapUI.
I've used the latter approach to good effect in our projects. I've usually found that my apps use a subset of the functionality exposed by a given web service's API, to it's usually made good sense to expose a simpler API to my client code that's more streamlined and that reflects the workflow of my client better. So, since the way I typically use web services already involves writing an abstraction layer, replacing the endpoint on the other side of my adapter classes with a mock service is a very low-friction way to test interaction with the service.

How to setup spring.net dependency injection for a web service?

I have been handed a wsdl file + a number of xsd type definition files - the service I need to code against is not ready yet and I need to put together a fake service (so called a stub or mock) in order to be ready when the real thing comes along.
My question is - once I get the interface I need to implement from the wsdl, how do I setup dependency injection so that whenever the new service comes along I can add a service reference or a web reference and just edit the spring.net config file to swap in the service I want in the consumer? Is it even possible?
I found this article, specific to WCF, It's pretty good but he seems to have access to the service code and he's doing dependency injection on the service side rather than on the consumer side - in my case I will most likely just get a url, I will have to swap it with the fake local one and go from there.
Also is there a way of doing this only dependent on the way I consume the service but not on the way the service was put together? I mean, I shouldn't care less which technology was used to develop the service as long as I get a url to the wsdl.
Any pointers appreciated!
Just create an interface which maps to the webservice methods and use that on the client side. It doesn't matter if it's a local service, webservice, or whatever.
I've done what you are trying to do before, you can read about it here.