How to test a restful web service in java

russellhoff

I've written a web service at work and now I want to write some tests. I'm doubtful as how to test them, but I know which tools I need: JUnit 4 and perhaps an embedded application container, for instance Tomcat, plus Jersey Client. Nevertheless, here you are my approaches:

  1. Create a test class where an embedded Application Container (Tomcat) is started. Then, deploy the web service to it and make test calls using Jersey Client. This is the way I've done it so far. The problem comes here: to deploy the web service, I must have the WAR created previously. This is a problem, as when building the application with Maven firstly tests are executed and then WAR is built; I think this is actually a loop. Would it be possible to deploy my webapp without the need of the war? Here you are my test class:

    public class TestAutenticacionService {
    
        private final String mWorkingDir = "C://Users//Asasa//workspace//myWebApp//";
        private Tomcat tomcat = null;
        private WebTarget webTarget = null;
        private ObjectMapper mapper = new ObjectMapper();
        private Long idClienteCorrecto = 787538L;   
    
        @Before
        public void setUp() throws Exception {  
            tomcat = new Tomcat();
            tomcat.setPort(9090);
            tomcat.setBaseDir(mWorkingDir);
            tomcat.getHost().setAppBase(mWorkingDir);
            tomcat.getHost().setAutoDeploy(true);
            tomcat.getHost().setDeployOnStartup(true);
            tomcat.getServer().addLifecycleListener(new VersionLoggerListener());
            tomcat.getHost().addLifecycleListener(new HostConfig());
    
            try {
                tomcat.addWebapp("/bidegiWsCli", "C:/Users/Asasa/workspace/myWebApp/myWebApp.war");
                tomcat.start();
                System.out.println("Tomcat iniciado en " + tomcat.getHost());
    
                webTarget = buildClientWs();
    
            } catch (LifecycleException e) {
                System.err.println("Tomcat no se pudo iniciar.");
                e.printStackTrace();
            }
        }
    
        @After
        public void tearDown() throws Exception {
            if(tomcat != null){
                try {
                    tomcat.stop();
                    System.out.println("Tomcat parado.");
                } catch (LifecycleException e) {
                    System.err.println("Error al intentar parar Tomcat.");
                    e.printStackTrace();
                }   
            }
        }
    
        @Test
        public void test() {
    
            WebTarget loginTgt = webTarget.path("login");
    
            // Probamos un login correcto:
            WebTarget loginTgtOk = loginTgt.queryParam("Aus", "764577676t").queryParam("Pass", "****");
            Response respOk = loginTgtOk.request(MediaType.APPLICATION_JSON).get();
            String strLrOk = (String) respOk.readEntity(String.class);
            try {
                LoginResponse lrOk = mapper.readValue(strLrOk, LoginResponse.class);
                Long idClienteOk = lrOk.getIdCliente();
                assertEquals(idClienteOk, idClienteCorrecto);
            } catch (Exception e) {
                try {
                    mapper.readValue(strLrOk, ExceptionResponse.class);
                    assertTrue(true);
                } catch (JsonParseException e1) {
                    System.err.println("Error JsonParseException: " + e1.getMessage());
                    e1.printStackTrace();
                } catch (JsonMappingException e1) {
                    System.err.println("Error JsonMappingException: " + e1.getMessage());
                    e1.printStackTrace();
                } catch (IOException e1) {
                    System.err.println("Error IOException: " + e1.getMessage());    
                    e1.printStackTrace();
                }
            }
    
            // Probamos un login incorrecto:
            WebTarget loginTgtWrong = loginTgt.queryParam("Aus", "764577676t").queryParam("Password", "***");
            Response respWrong = loginTgtWrong.request(MediaType.APPLICATION_JSON).get();
            String strLrWrong = (String) respWrong.readEntity(String.class);
            try {
                LoginResponse lrWrong = mapper.readValue(strLrWrong, LoginResponse.class);
                Long idClienteWrong = lrWrong.getIdCliente();
                assertNotEquals(idClienteWrong, idClienteCorrecto);
            } catch (Exception e) {
                try {
                    mapper.readValue(strLrWrong, ExceptionResponse.class);
                    assertTrue(true);
                } catch (JsonParseException e1) {
                    System.err.println("Error JsonParseException: " + e1.getMessage());
                    e1.printStackTrace();
                } catch (JsonMappingException e1) {
                    System.err.println("Error JsonMappingException: " + e1.getMessage());
                    e1.printStackTrace();
                } catch (IOException e1) {
                    System.err.println("Error IOException: " + e1.getMessage());
                    e1.printStackTrace();
                }
            }
    
        }
    
        private WebTarget buildClientWs(){
            Client client = ClientBuilder.newClient();
            return client.target("http://localhost:9090").path("myWebApp").path("resources").path("Auth");
        }
    
    }
    
  2. The other approach would be executing directly web service methods directly, without the need of an application container. I'm not sure about this, as I wouldn't be testing the Web Service itself.

Any thoughts about this?

Thank you in advance

crazyGuy

If you are using Jersey, then you can use the built-in test frameworks.

See https://jersey.java.net/documentation/latest/test-framework.html

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to expose a cxf restful web service in java?

From Dev

How to expose a cxf restful web service in java?

From Dev

how to consume a Restful Web Service (Restful API) in Java

From Dev

How to test RESTful web service automatically with random data

From Dev

How to test RESTful web service automatically with random data

From Dev

How to enable CORS with a java restful web service on JBoss AS

From Dev

How to enable CORS with a java restful web service on JBoss AS

From Dev

Java , how can i get JSON from restful(?) web service

From Dev

java - how to send images from a RESTful web service?

From Dev

java - How to receive a Json object on a Restful web service?

From Dev

How to assert the request sent to restful web service from a BDD test (specflow with nunit)

From Dev

Use threads on a RESTful Web Service in Java

From Dev

Java Restful Web Service - Static Member Variables

From Dev

Deploy Java restful web service to live server

From Dev

How to call RESTful web service remotely

From Dev

How to expose a C library to a RESTful web service

From Dev

how to call RESTful web service in phonegap application?

From Dev

how to Call Restful web service using javaScript?

From Dev

How to call RESTful web service remotely

From Dev

how to call RESTful web service in phonegap application?

From Dev

Hosting restful web service in java on a real web server?

From Dev

How to create a Restful web-service of JSON data in java,tomcat,eclipse

From Dev

How to create a Restful web-service of JSON data in java,tomcat,eclipse

From Dev

java restful web service, can't figure how to execute a specific Query

From Dev

How to test web service with fitnesse

From Dev

Restful Web Service Error

From Dev

RESTful web service - HATEOAS

From Dev

State in Restful web service

From Dev

SQLite for a RESTful web service

Related Related

  1. 1

    How to expose a cxf restful web service in java?

  2. 2

    How to expose a cxf restful web service in java?

  3. 3

    how to consume a Restful Web Service (Restful API) in Java

  4. 4

    How to test RESTful web service automatically with random data

  5. 5

    How to test RESTful web service automatically with random data

  6. 6

    How to enable CORS with a java restful web service on JBoss AS

  7. 7

    How to enable CORS with a java restful web service on JBoss AS

  8. 8

    Java , how can i get JSON from restful(?) web service

  9. 9

    java - how to send images from a RESTful web service?

  10. 10

    java - How to receive a Json object on a Restful web service?

  11. 11

    How to assert the request sent to restful web service from a BDD test (specflow with nunit)

  12. 12

    Use threads on a RESTful Web Service in Java

  13. 13

    Java Restful Web Service - Static Member Variables

  14. 14

    Deploy Java restful web service to live server

  15. 15

    How to call RESTful web service remotely

  16. 16

    How to expose a C library to a RESTful web service

  17. 17

    how to call RESTful web service in phonegap application?

  18. 18

    how to Call Restful web service using javaScript?

  19. 19

    How to call RESTful web service remotely

  20. 20

    how to call RESTful web service in phonegap application?

  21. 21

    Hosting restful web service in java on a real web server?

  22. 22

    How to create a Restful web-service of JSON data in java,tomcat,eclipse

  23. 23

    How to create a Restful web-service of JSON data in java,tomcat,eclipse

  24. 24

    java restful web service, can't figure how to execute a specific Query

  25. 25

    How to test web service with fitnesse

  26. 26

    Restful Web Service Error

  27. 27

    RESTful web service - HATEOAS

  28. 28

    State in Restful web service

  29. 29

    SQLite for a RESTful web service

HotTag

Archive