如何为一个测试课程中的所有测试一次启动Jersey测试容器(Grizzly)? [英] How to start a Jersey Test Container (Grizzly) once for all tests in a test class?

查看:96
本文介绍了如何为一个测试课程中的所有测试一次启动Jersey测试容器(Grizzly)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修复其中一个项目中的集成测试.当前,所有集成测试类都扩展了JerseyTest类.通过 JerseyTest类我意识到,它使用Junit的Before和After注释为每种测试方法启动和停止容器.

I am working on fixing the integration tests in one of the projects. Currently, all the integration test classes extend the JerseyTest class. Going through the JerseyTest class I realized that it starts and stops the container for every test method using Junit's Before and After annotations.

为什么这是必要的?如果我们只打开一次容器,运行测试并在结束时将其关闭,这还不够吗?

Why is this necessary? Isn't it enough if we bring up the container once, run the tests and shut it down at the end of it?

我们还使用Spring,需要花费一些时间来初始化上下文.

We also use Spring and it takes time for the context to get initialized.

在Junit4之前,我们通过使用布尔标志手动处理此限制来解决此限制.

Prior to Junit4 we worked around this limitation by handling it manually using boolean flags.

@Before
public void setup() {
  if(!containerStarted) {
   // start
   containerStarted = true;    
  }
 // else do nothing
}

推荐答案

您可以使用@BeforeClass和@AfterClass覆盖JerseyTest的@Before和@After生命周期.这是代码模板:

You can use @BeforeClass and @AfterClass to override JerseyTest's @Before and @After lifecycle. Here is the code template:

public abstract class MyJerseyTest {
  private JerseyTest jerseyTest;
  public MyJerseyTest(){

  }

  @BeforeClass
  public void setUp() throws Exception {
    initJerseyTest() 
    jerseyTest.setUp();
  }

  @AfterClass
  public void tearDown() throws Exception {
    jerseyTest.tearDown();
  }

  private void initJerseyTest() {
    jerseyTest = new JerseyTest() {
      @Override
      protected Application configure() {
        // do somthing like
        enable(...);
        set(...);

        // create ResourceConfig instance
        ResourceConfig rc = new ResourceConfig();

        // do somthing like
        rc.property(...);
        rc.register(...);

        return rc;
      }
    };
  }
}

希望这会有所帮助.

这篇关于如何为一个测试课程中的所有测试一次启动Jersey测试容器(Grizzly)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆