I'm using apache http client version 5.x, and I'm struglling to implement mocked unit tests for the CloseableHttpResponse execute(ClassicHttpRequest request) method from CloseableHttpClient class, since the response type is final and there is no public way to instantiate it.
The following question provided some ways to do it, but the discussion seems to be around the older versions of the apache http client, and although I was able to enable mockito to mock final types, it seems to be risky, as documentation itself warns that this is a completely different mocking mechanism that requires more feedback from the community.
Is there an easier/proper way to achieve this?
Related
Basically as the title says. Apache HttpClient and Spring RestTemplate allow for defining custom interceptors which wrap around requests/responses and allow for additional (global) modification of request parameters, logging, etc...
I do not see such a feature available in standard Java implementation of java.net.http.HttpClient (as of Java 11). Am I missing something or is there no way to intercept all requests/responses on a single HttpClient?
There isn't a built-in solution, but you can write your own code as in this answer or use this interceptable-http-client library.
Is there a way to enforce usage of HTTPS protocol, i.e. disable usage of plain HTTP protocol within the RestTemplate client in Spring?
I have this client as an abstract class and there are numerous implementations using it. So filtering input parameters for URI protocol http is not such a good idea.
Is there a way to achieve this using ClientHttpRequestFactory or similar?
Thank you,
Josef
It's not about Spring. You just should configure your container or server(Tomcat, JBoss, i.e.)
So, here's the thing.
There's a REST service I'm using that is supposed to be tested and I can't access the code.
I've made some kind of library in java so I can interact with that service, but I need to unit test my library (actually it's still not implemented, I'm using TDD) so I can know for sure it works.
How can I do it so I don't mess up with the service (I don't want to create nor delete anything)?
Should I use some kind of mock or stub? If so, how can it be done?
Thx!
I use the Jersey test framework to test - if you have written your code with Jersey. It runs grizzly in the background.
You could use HttpUnit's PseudoServer to create fake responses.
Or you could use HttpClient to generate post and get requests to your running service.
You can use a mocking framework (I like Mockito) to mock your API endpoint library. Then you can use traditional junit tests to ensure that your library is making the expected API calls.
If you want to actually make the HTTP calls, there are few other libraries you can use (Jersey client was already suggested).
Is rest assured a thing you wanted to find?
My organization uses spring to generate our mock rest services. Spring MVC test framework (formally spring social test) works very well. However if you aren't already using spring i'd suggest Jmock, mockito, or easy mock to just fake a response.
I dislike the idea to use a mock since it leaves out the HTTP stuff. Best is using a mock server like enter link description here. It is a bit tricky first but once you have your test case up and running it is a breeze. I use random ports to start the mock server so one can run tests in parallel.
Somewhere, I don't remember where, I spotted information, that starting from GWT 2.1.1 it is possible to test ReqeustFactory services without GWTTestCase. If this is true, please show me how.
The RequestFactorySource type can be used to instantiate RequestFactory instances in non-GWT runtimes. The previously-mentioned InProcessRequestTransport is used by GWT's own RequestFactoryJreSuite tests to avoid the need to fire up an entire GWT runtime environment.
The RequestFactorySource type isn't limited to just testing. If you implement your own RequestTransport (perhaps based on a java.net.HttpUrlConnection or Apache HttpClient library), you can write console apps, bulk-query apps, or health probers using your production RequestFactory endpoints. This is a huge improvement over GWT`s old RPC system, which only supports GWT-based clients.
I found it myself browsing GWT's source code. The answer is hidden in InProcessRequestTransport javadoc:
https://gwt.googlesource.com/gwt/+/master/user/src/com/google/web/bindery/requestfactory/server/testing/InProcessRequestTransport.java
I'm getting ready to dive into testing of a RESTful service. The majority of our systems are built in Java and Eclipse, so I'm hoping to stay there.
I've already found rest-client (http://code.google.com/p/rest-client/) for doing manual and exploratory testing, but is there a stack of java classes that may make my life easier? I'm using testNG for the test platform, but would love helper libraries that can save me time.
I've found http4e (http://www.ywebb.com/) but I'd really like something FOSS.
You can use REST Assured which makes it very easy to test and validate REST services in Java from JUnit or TestNG. E.g. let's say that a GET request to a service called "/lotto" returns the following JSON
{
"lotto":{
"lottoId":5,
"winning-numbers":[2,45,34,23,7,5,3],
"winners":[{
"winnerId":23,
"numbers":[2,45,34,23,3,5]
},{
"winnerId":54,
"numbers":[52,3,12,11,18,22]
}]
}
}
then you can make the request and validate the response (in this case that lotto id equal to 5) with REST Assured like this:
expect().body("lotto.lottoId", equalTo(5)).when().get("/lotto");
Would JMeter be an option? It has HTTP Request samplers that support all of the HTTP methods and assertions to validate the response.
Another alternative might be something like soapUI which has an API that could be integrated with your test cases, though I haven't tried it.
CXF apparently has support for REST. I haven't tried the REST support yet myself, but I think CXF is a superb, flexible, standards-based webservice implementation.
Another informal option to test your REST services is by using the Firefox RESTClient Add-on.
Postman REST Client for Google Chrome is another option.
Apparently rest-client has a library built in. I'm using that with testNG and XStream and it seems to be exactly what the doctor ordered.
I was working today around the unit testing of a rest service, I needed to test the deployed server in order to check some concurrency requeriments, so I needed to test the deployes rest service.
At first I tried the solution sugested by Johan, and started with REST Assured with some succes, but look for more documentation, I found this:
http://uttesh.blogspot.com/2012/09/spring-rest-web-service-test.html
its based on a spring library, so if you are using spring, you'll keep using the stuff from the same vendor, which is always nice.
Also I found the libs from spring easier to understand than REST Assured, but it's just me ;)