【问题标题】:Writing tests for grizzly sever with jersey with jar dependency使用带有 jar 依赖关系的球衣为 grizzly 服务器编写测试
【发布时间】:2016-06-24 05:38:04
【问题描述】:

我通过java -jar target/test-service-1.0-jar-with-dependencies.jar 命令从终端启动服务器

但是,当从 Intellij Idea 运行测试时,我就是不知道如何启动服务器..

这是当前代码,不起作用

private HttpServer server;
private WebTarget target;

@Before
public void setUp() throws Exception {
    // start the server
    server = Main.startServer();
    // create the client
    Client c = ClientBuilder.newBuilder().register(JacksonFeature.class).build();
    // uncomment the following line if you want to enable
    // support for JSON in the client (you also have to uncomment
    // dependency on jersey-media-json module in pom.xml and Main.startServer())
    // --
    // c.configuration().enable(new org.glassfish.jersey.media.json.JsonJaxbFeature());

    target = c.target(Main.BASE_URI);
}

这是我的 startServer 代码

public static HttpServer startServer() {
        // create a resource config that scans for JAX-RS resources and providers

    ResourceConfig rc = new ResourceConfig().packages("com.test.service").register(JacksonFeature.class);
    EncodingFilter.enableFor(rc, GZipEncoder.class);
    rc.register(LoggingFilter.class);
    rc.register(MultiPartFeature.class);
    rc.register(CORSResponseFilter.class);
   // rc.property("config", configParams);
    // create and start a new instance of grizzly http server
    // exposing the Jersey application at BASE_URI
    HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
    //httpServer.getServerConfiguration().addHttpHandler(shh);
    return httpServer;

}

【问题讨论】:

    标签: java intellij-idea server jersey grizzly


    【解决方案1】:

    它应该自己启动。但是对于测试,您可能希望控制它何时开始。您可以将false 作为third argument to the server factory method 传递。这样你就可以控制它什么时候开始。

    您可以在测试的 before 和 after 方法中的 HttpServer 实例上调用 startstop。您还需要更新 Main 类中的代码,以调用 start()

    您可能还想查看Jersey Test Framework。在这里,您不需要启动和停止任何服务器。该框架将为您处理它。它还使您的测试比您当前的设置更具可配置性。假设您只想注册一个资源,或者您想注入一些模拟服务。就个人而言,我会选择测试框架。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 2019-12-08
      • 2013-12-07
      相关资源
      最近更新 更多