JUnit: Mocking a http server - unit-testing

I have a service that has a method that makes a rest call using apache httpclient. I want to test the call using junit but i do not want to mock out the call but mock out the server that it is making the call to
My question is, is it possible to mock out a server using something like mockwebserver so that if you make a request to a specific url that this will be picked up from the service (without mocking the service) rather then going off to the real server?

HttpClient is an interface so you can return a mock in your tests.

Try Jadler (http://jadler.net), an open source library for creating stub/mock http servers. It should provide you everything you need (I'm one of its developers).

Related

How to mock client inside paymentintent for Stripe?

Currently we are using paymentintent.New() method to create a payment intent for stripe but the issue we are facing is in writing unit tests (for integration test, we are already aware of stripe-mock)
Inside this method, a http client is created internally. Not sure how can we mock it.
https://github.com/stripe/stripe-go/blob/master/paymentintent/client.go#L24
We have tried generating the mock of stripe http client by ourselves but that won't help much since paymentintent internally creates a HTTP client and we don't see a way to pass our own http client inside the paymentintent method
stripe-go is using stripe-mock internally for unit testing. You can see this piece of code to learn how to set a mock server for you unit tests.

SoapUI: Create mock service with pass-through behavior for selected methods

While developing a web application I have the following use case:
a 3rd party Web Service with quite a lot of methods is deployed on a test server A (with a single endpoint, e.g. http://3rdPartyServer/3rdPartySvc?WSDL)
a new method is about to be implemented in the near future, but I need to use it now
the rest of the methods are used throughout my code extensively
So I would like to do the following:
Create a mock service in SoapUI locally, based on the new WSDL which includes the new WS method (i.e. a superset of the WS methods currently on server A)
point my local application server to use the SoapUI mock service endpoint
mock only the response of the new WS method (create a dummy response for it in SoapUI)
let the other WS method calls to reach server A and return whatever it returns normally (i.e. use SoapUI as a proxy for these calls)
I've gone through the SoapUI documentation regarding service mocking and have used it numerous times, but could not find an option for such "pass-through" behavior.
When you read in your WSDL, the endpoint will point to your server.
Open your service, and select the service endpoint.
Add a second endpoint, to point to your mock. SoapUI has little bit of documentation showing this here. Only step "2. Getting Started" applies, not step 3!
In each of your tests, where you are using the mocked method, you will need to select the mock endpoint. Further discussion is here.

Mock external http requests in grails

I am using RestBuilder (LINK) for making REST calls to outer services.
Now I want to mock that http requests for unit testing (or integration testing, whatever) service that handles all logic about communication with that outer services.
Does anyone know how to mock http requests or this RestBuilder maybe?
Thanks for any help!
Ivan
This kind of test is called «functional test». There are many plugins that can help you with this task:
http://www.grails.org/plugin/functional-test
http://www.grails.org/plugins/tag/functional

how to write unit tests for REST web services developed using apache-cxf with spring without using Maven

I have developed a REST web application using apache cxf library. I am able to access them using a browser and test. I want to write unit tests for the services.
I tried testing it using embedded Jetty server with cxf.
After starting the Jetty server, I am getting a proxy using WebClient API of cxf. The relevant code is
WebClient client = WebClient.create(ENDPOINT_ADDRESS);
client.accept("text/xml");
client.path("/studyservice/topimstudydetails");
client.replaceQuery("pnlId=3&lclId=30&indId=442&maxImStudies=99&rName=DEV");
TopIMStudyDetailsResponse resp = client.get(TopIMStudyDetailsResponse.class);
assertEquals(resp.getStatus().getReturnCode(),0);
The call to the service is successful as I can see the logs, but I'm get a null pointer where the service is trying to make a dao call. The dao is declared as a dependency for the service. How can we get a proxy with all the dependencies injected ?
May be you'll find the following posts helpful:
http://tarlogonjava.blogspot.co.il/2011/12/running-integration-test-using-with.html
http://tarlogonjava.blogspot.co.il/2011/12/running-integration-tests-using-with.html
The second post is about replacing the data source with a special in-memory data source for tests.

PHP library similar to REST-driver in Java

I found this little library to be perfect solution for my problem. It allows you to stub REST service responses easily.
Now i need to replicate the same set of test cases in PHP version of our library. Do you know any similar library/framework in PHP?
You could use Guzzle. Guzzle is a PHP HTTP client & framework for building RESTful web service clients. The thing is that you can create Response objects. In this library, you have a Response class. You could just create the Guzzle Response that you want (choosing the desired status code, content, etc), and mock your HTTP Client so it returns the Response object that you just created.
If you read the documentation, it seems that there is a plugin to mock responses, although I've never used it.