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.
Related
I have a REST client in Java that is ready to connect to a REST server, send a specific request and get a response back. However, the actual REST server is not available during development time (it is hosted by a 3rd party and only available in the isolated local net of the target machine) and we still want to test connectivity and interaction with the server.
Can you point me to a product or technology that in the first place lets me quickly create a fake REST server (or at least mock it) according to the specification of the REST call parameters? I did some research on the web but haven't had a "yes, this is it!" moment yet.
You can use any rest mocking framework to achieve this. As per my experience
Wiremock is the best framework for RES API Mocking.
http://wiremock.org/
You can use mockwebserver library by square. It's pretty simple to use and do exactly what you attempt to do.
Little usage sample:
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().setBody("hello world"));
server.start();
For client testing, I would recommend a tool like SOAP UI or Postman. But for mocking the REST service itself, I would actually recommend just creating some dummy endpoints in your Java web application and using that. At each endpoint you could do something hard code a simple response, in JSON, XML, or whatever would make sense with your service. Then, when you finally come to actual development, you will already have a functioning framework for your REST service. You would just need to fill in the missing pieces.
This makes sense because you are going to need to setup REST support in your Java code anyway, and the amount of effort to just add stubs should be minimal. This approach is also attractive from the point of view that it avoids any potential surprises from switching from a mock REST service to the actual one.
I am just started to write JUnit test cases.Now I am writing a test method to test RESTful web service in java using the IntelliJ IDEA. My directory structure as this.
I am calling the web service from my test case as:
Response response = target.path("groups").path("registergroup").request().accept(MediaType.APPLICATION_JSON).post(Entity.json(stringEmp.toString()));
String output = response.readEntity(String.class);
I have added the multiple breakpoint in this test method and source classes.
Is it possible to jump Webservice classes from above request point?
If possible then how can I do that?
I am using the embedded jetty server to test which is also running from this module.
Testing REST services using JUnit only is in my opinion not worth the effort, because you usually have to mock a lot of the REST library internals in order to make it work, and it's very hard to test some of the service behavior anyway (e.g. what happens when the client specifies the wrong Content-Type or Accept headers).
Assuming you are using Jersey, you have two options :
use JerseyTest
use Arquillian
My personal preference goes to Arquillian because (among other things) the resulting tests are completely independent from what is being tested (i.e. you can change the implementation of the service and the REST library without changing the tests).
I have a service that calls out to a third-party endpoint using java.net.URLConnection. As part of an integration test that uses this service I would like to use a fake endpoint of my own construction.
I have made a Spring MVC Controller that simulates that behaviour of the endpoint I require. (I know this endpoint works as expected as I included it in my web app's servlet config and hit it from a browser once started).
I am having trouble figuring out how I can get this fake endpoint available for my integration test.
Is there some feature of Spring-Test that would help me here?
Do I somehow need to start up a servlet at the beginning of my test?
Are there any other solutions entirely?
It's a bad idea to use a Spring MVC controller as a fake endpoint. There is no way to simply have the controller available for the integration test and starting a servlet with just that controller alongside whatever you are testing requires a lot of configuration.
It is much better to use a mocking framework like MockServer (http://www.mock-server.com/) to create your fake endpoint. MockServer should be powerful enough to cover even complex responses from the fake endpoint, with relatively little setup.
Check out Spring MVC Test that was added to Spring in version 3.2.
Here are some tutorials: 1, 2, 3
First I think we should get the terminology right. There are two general groups of "fake" objects in testing (simplified): a mock, which returns predefined answers on predefined input and stubs which are a simplified version of the object the SUT (system under test) communicates with. While a mock basically does nothing than to provide a response, a stub might use a live algorithm, but not store it's results in a database or send them to customers via eMail for example. I am no expert in testing, but those two fake objects are rather to be used in unit and depending on their scope in acceptance tests.
So your sut communicates with a remote system during integration test. In my book this is the perfect time to actually test how your software integrates with other systems, so your software should be tested against a test version of the remote system. In case this is not possible (they might not have a test system) you are conceptually in some sort of trouble. You can shape your stub or mock only in a way you expect it to work, very much like the part of the software you have written to communicate with that remote service. This leaves out some important things you want to test with integration tests: Was the client side implemented correctly so that it will work with the live server. Do we have to develop work around as there are implementation errors on the server side? In which scale will the communication with the remote system affect our software's performance? Do our authentication credentials work? Does the authentication mechanism work? What are the technical and conceptual implications of this communication relationship no one has thought of so far? (Believe me, the latter will happen more often than you might expect!)
Generally speaking: What will happen if you do integration tests against a mock or a stub is that you test against your own understanding of how to implement the client and the server side of communication, and you do not test how your client works with the actual remote server or at least the best thing next to that, a test system. I can tell you from experience: never make assumptions on how a remote system should behave - test it. Even when talking of a JMS server: test it!
In case you are working for a company, testing against a provided test system is even more important: if you software works against a test system and you can prove it (selenium is a good helper here, as well as good logging, believe it or not) and your software does not work with a live version, you have a situation which I call "instablame": it is immediately obvious that it is not your fault the software isn't working. I myself hate fingerpointing to the bone, but most suits tend to ask "Who's fault was it?" even before "Can we fix that immediately?" and way before "How can we solve that problem?". And there is a special group of suits called lawyers, you know ... ;)
That being said: if you absolutely have to use those stubs during your integration tests, I would create an own project for them (let's say "MyProject-IT-Stubs" and build and run the latest version of MyProject-IT-Stubs before I run the IT of my main project. When using maven, you could create MyProject-IT-Stubs with war packaging, call it as a dependency during the pre-integration-test phase and fire up a jetty for this war in the same phase. Then your integration tests run, either successful or not and you can tear down the jetty in the post-integration-test phase.
The IMHO best way to organize your project with maven would be to have a project with three modules: MyProject,MyProject-IT-Stubs and MyProject-IT(declaring dependencies on MyProject and MyProject-IT-Stubs. This keeps your projects nice and tidy and the stubs do not pollute your project. You might want to think about organizing MyProject-IT-Stubs into modules as well, one for each remote system you have to talk to. As soon as you have test access, you can simply deactivate the according module in MyProject-IT-Stubs.
I am sure according options exist for InsertYourBuildToolHere.
I am implementing an application that tests web services, and I am wondering if it is worthy to use spring testing.
In my application I call web services and test if the results returned match with the ones expected, so I don't need to inject any DAO.
Did I miss any other benefits ?
If you test an external webservice, you don't need spring. You even don't need java as you might as well write good tests with soapUI.
Anyhow, if you're testing your own webservice classes, you probably have to mock some services at testing time, and therefore spring testing might help alot.
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 ;)