简体   繁体   中英

Testing the Response code of a WebService without a running server

I would like to test my WebService with JUnit (EasyMock and Hamcrest) without having to start my server (Wildfly)

private RegisterUserWebService testeee = new RegisterUserWebService();

@Test
  public void test() {

    // Set Mocks for testee
    // ...
    //

    Response response = testee.registerUser();
    verifyAll();
    assertThat(response.getStatusInfo().getStatusCode(), equalTo(Status.CONFLICT));

  }

I am getting a ClassNotFoundException:

java.lang.RuntimeException: java.lang.ClassNotFoundException: org.glassfish.jersey.internal.RuntimeDelegateImpl
            at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:152)
            at javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:120)
            at javax.ws.rs.core.Response$ResponseBuilder.newInstance(Response.java:848)
            at javax.ws.rs.core.Response.status(Response.java:590)
            at javax.ws.rs.core.Response.status(Response.java:601)
            at my.package.webservices.RegisterUserWebService.registerUser(RegisterUserWebService.java:50)

As I understand it, no implementation could be found for javax.ws.rs.core.Response, and the default one seems to be the glassfish one which I don't have, hence the ClassNotFoundException.

How could I tell my test to use the Response from Wildfly ? Or is there another way ?

It seems that you are a bit mistaken on what unit tests are: basically, unit tests are for testing the smallest "units" that exist (like classes, methods) at build time ; so, in other words: without any connection to real file systems, servers, databases, etc. pp.

This means, you can use JUnit to test your classes that "prepare" input that is supposed to go somewhere; or you can test classes that process some "result" coming from somewhere. In both cases, you might use a mocking framework (like EasyMock or Mokito) to fake that external part that provides the input to your classes.

So, some pseudo code to show you what could be working:

RegisterUserWebService mockedService = ... create mock ...
Response mockedResponse = ... create mock ...
expect(mockedService.getStatusInfo() ...)
expect(mockedService.registerUser( params to match on )).andReturn(mockedResponse)

YourComponent underTest = new YourComponent ( mockedService )
underTest.doSomething()

verify ( that your mocks saw the calls you expected to happen )

Anything else would not be a unit test, but a function/integration test (because they need other components to be up and running)

So, you decision: you can do unit tests, but then your scope will be different; or you focus on writing functional tests here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM