简体   繁体   English

Spring / JUnit-在进行任何测试之前先运行

[英]Spring/JUnit - Run something before ANY tests

Does Spring or the JUnit runner have something that I can use to run code before running ANY tests? 在运行任何测试之前,Spring或JUnit运行器是否具有可用于运行代码的东西?

The closest I've found is the @Rule and @ClassRule , which work on a class level. 我找到的最接近的是@Rule@ClassRule ,它们在类级别上起作用。

I have a Docker @ClassRule which hosts an empty database for empty integration testing. 我有一个Docker @ClassRule ,它托管一个空数据库以进行空集成测试。 With the @ClassRule , it restarts the container every time. 使用@ClassRule ,它每次都会重新启动容器。

I'd prefer instead to just start it once when starting the tests (regardless if it's all tests or just a single one), runs the tests, then kill the container. 相反,我宁愿在启动测试时仅启动一次(无论是所有测试还是仅一个测试),然后运行测试,然后终止容器。

I've searched around, but I haven't found anything other than the class rule. 我到处搜寻,但是除了上课规则外,我什么也没找到。 Apologizes if I'm missing something obvious. 如果我遗漏了一些明显的东西,表示歉意。

It appears that Spring and JUnit don't directly have anything to do this. 看来Spring和JUnit没有直接做任何事情。 After some more googling, I found a few bits that lead to some inspiration. 经过更多的谷歌搜索后,我发现了一些启发灵感的方法。

Making use of a custom rule extending ExternalResource (from JUnit), I'm kind of bastardizing it, but it does what I want: 通过使用扩展ExternalResource (来自JUnit)的自定义规则,我有点混蛋了,但是它确实满足了我的要求:

public class MyRule extends ExternalResource {
  static private MyRule instance;

  static public MyRule get() {
    if (instance == null) {
      instance = new MyRule();
    }

    return instance;
  }

  private MyRule() {
    // do init stuff

    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
      // do shutdown stuff
    });
  }
}

The basic idea is that the rule is a singleton. 基本思想是规则是单例。 In each class that might need it, I'd put an @ClassRule : 在每个可能需要的类中,我将放置一个@ClassRule

 public class MyTest {
   @ClassRule
   private MyRule myRule = MyRule.get();
 }

It'll lazy-initialize itself, which will do all of the setup needed. 它将自行延迟初始化,这将完成所有需要的设置。 It'll also register a shutdown hook, which will then handle any after stuff. 它还将注册一个关闭钩子,该钩子将处理所有后续操作。

With this pattern, it'll run code exactly once before any tests (that need this rule) run, and it'll perform shutdown code only at the very end after all tests have finished. 使用这种模式,它将在任何测试(需要此规则)运行之前仅运行一次代码,并且仅在所有测试完成后才执行关闭代码。

Note: It purposely doesn't override the before() and after() functions, because those are before and after each class. 注意:故意不要覆盖before()after()函数,因为它们在每个类的前面和后面。 You could add things there if you wanted to do something in between classes as well. 如果您还想在课程之间进行操作,则可以在此处添加内容。

Maybe what you are looking for are those 2 annotations : 也许您正在寻找的是这两个注解:

@BeforeClass

@Before

@Before runs before each test while @BeforeClass runs only once @Before在每个测试之前运行,而@BeforeClass仅运行一次

You can use it like this : 您可以像这样使用它:

@Before
public void setUp(){
// start container here
}

You also have equivalent for after test : @After @AfterClass 您也有等效的事后测试: @After @AfterClass

You will find a great explanation here 您会在这里找到很好的解释

Thanks 谢谢

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

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