简体   繁体   English

在不运行服务器的情况下测试WebService的响应代码

[英]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) 我想用JUnit(EasyMock和Hamcrest)测试我的WebService,而不必启动服务器(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: 我收到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. 据我了解,找不到javax.ws.rs.core.Response的实现,默认的实现似乎是我没有的玻璃鱼,因此是ClassNotFoundException。

How could I tell my test to use the Response from Wildfly ? 我怎样才能告诉我的测试使用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; 这意味着,您可以使用JUnit来测试您的类,以“准备”应该放在某个地方的输入。 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. 在这两种情况下,您都可以使用模拟框架(例如EasyMock或Mokito)来伪造为您的类提供输入的外部部分。

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. 或者您专注于在此处编写功能测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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