I have written a Restful Web service and have to test it using JUnit4. I have already written a Client using Jersey Client. But want to know if I can test my service only with junit4. Can someone help me with sample at least.
My rest service has authenticate method that takes user name, password and returns a token.
I have written test case for authenticate method. But I am not sure how to test using url.
public class TestAuthenticate {
Service service = new Service();
String username = "user";
String password = "password";
String token;
@Test(expected = Exception.class)
public final void testAuthenticateInputs() {
password = "pass";
service.authenticate(username, password);
}
@Test(expected = Exception.class)
public final void testAuthenticateException(){
username = null;
String token = service.authenticate(username, password);
assertNotNull(token);
}
@Test
public final void testAuthenticateResult() {
String token = service.authenticate(username, password);
assertNotNull(token);
}
}
I think @peeskillet has given you the needed prerequisites, i.e you need to run your web-service in an embedded web server. You could also look into dropwizard or spring-boot support for doing this conveniently.
As for actually verifying the response I would keep it simple and go with JUnit & http-matchers (see https://github.com/valid4j/http-matchers)
If you want to test using the URL, then you will need to start a server from your test. You can explicitly start an embedded server, which is pretty common for tests. Something like
It's basically an integration test. You're starting the Grizzly container and loading a
ResourceConfig
to the server with only theService
class. Of course you could add more classes to the configuration. You can use "real" resource config if you wanted.The above test uses this dependency
Another option, which is the one I prefer, is to make use of the Jersey Test Framework, which will start an embedded container for you. A test might look something more like
Using this dependency
And embedded Grizzly container will get started under the hood, with your
ResourceConfig
configuration. In both examples above it is assumed the@Path
value for theService
class isservice
, as you can see in the test URLs.Some Resources
Some Examples
UPDATE
If you're not using Maven, here are the jars you will need to run an embedded Grizzly container for the Jersey Test Fraemwork
I usually search for all my jars here. You can select the version and there should be a link in the next page, to download. You can use the search bar to search for the others.
Here's a simple running example, once you have all the jars
You're most likely not going to have the resources and
ResourceConfig
in the same class as the test, but I just want to keep it simple and all visible in one class.Whether you are using a web.xml or a
ResourceConfig
subclass (as shown above), you can cut down what you test by using a separateResourceConfig
, built in the test class, as I have done. Otherwise, if you are using your normalResourceConfig
class, you can just replace it in theconfigure
method.The
configure
method, is pretty much just building a web.xml file, just in Java code. You can see different methods in theWebAppDescriptor.Builder
, likeinitParam
, which is the same as an<init-param>
in your web xml. You can simply use the string in the arguments, but there are some constants, as I used above.The
@Test
is you usual JUnit test that will run. It is using the Jersey Client. But instead of creating theClient
, you can simply just use the preconfiguredClient
by just accessing theresource()
method, which returns aWebResource
. If you are familiar with the Jersey Client, then this class should not be new to you.Take a look at Alchemy rest client generator. This can generate a proxy implementation for your JAX-RS webservice class using jersey client behind the scene. Effectively you will call you webservice methods as simple java methods from your unit tests. Handles http authentication as well.
There is no code generation involved if you need to simply run tests so it is convenient.
The demo here setup up grizzly and uses the generator above to run junit tests.
Disclaimer: I am the author of this library.