Alex
Alex

Reputation: 315

Testing web services with JUnit

I'm developing an application to perform a series of tests on various web services. These web services consume and produce json, and for each of them we have a class to model the json request and response. For example:

If the json request for serviceX is something like this:

{
  "name":"Alex",
  "id":"123"
}

We have a class serviceXrequest like this:

public class serviceXrequest {

   String name;
   String id;

   //Constructor, getters/setters, etc
   ...
}

With an object of that class as the starting point, we can perform a series of test on the web service. The idea is to make those test as generic as possible so they can be used with any web service by just writing a class that models it's request and a class to model the response.

For that reason, all of the test methods developed so far work with plain java objects. This is an example of what I want to have:

public class WebServiceTest {

    String serviceURL;
    String requestJson;
    String requestClass;
    String responseClass;

    public WebServiceTest() {}

    @Test
    public static void Test1() { ... }

    @Test
    public static void Test2() { ... }

    ....

    @Test
    public static void TestN() { ... }
}

And then, from another class, invoke those tests with doing something like this:

public class LoginTest { //To test the login web service, for example

    public static void main(String[] args) {

        WebServiceTest loginTest = New WebServiceTest();

        loginTest.setServiceURL("172.0.0.1/services/login");
        loginTest.setRequestJson("{"user":"ale","pass":"1234"}");
        ...
        loginTest.runTests();
    }
}

I know it's not that simple, but any ideas on how to get there? Thanks in advance!!

Upvotes: 3

Views: 12873

Answers (3)

keyoxy
keyoxy

Reputation: 4591

You should consider using http-matchers (https://github.com/valid4j/http-matchers) which let's you write JUnit-tests, using regular hamcrest-matchers (bundled with JUnit) to test your web-service via standard JAX-RS interface.

Upvotes: 0

Kevin Welker
Kevin Welker

Reputation: 7947

You might also look into REST-assured

Upvotes: 2

Cris
Cris

Reputation: 5007

One of the best tools for testing your webservices is SOAP UI, but this is more for functional testing

As well I integrated very well FitNesse tests

JMeter goes hand in hand with LoadUI ..kind of same things in terms of stress and load tests for webservices.

Junit...i never used directly applied to the webservice itself.

Most of the times I had a Spring service called by the implemetation of the WebService interface (Port) and I unit tested that one.

Upvotes: 0

Related Questions