如何在Junit5中替换DropwizardAppRule

Dav*_*ica 2 java dropwizard junit-rule junit5

在Junit 4我可以做类似的事情

@ClassRule
public DropwizardAppRule<Configuration> app = new DropwizardAppRule<>(MyApp.class);

...

app.getLocalPort()
Run Code Online (Sandbox Code Playgroud)

如何在Junit 5中复制此行为?从这个 github问题看起来我需要使用@ExtendWith(DropwizardExtensionsSupport.class),但它不清楚如何

Ale*_*rov 6

Dropwizard 1.3.0 通过引入该类添加了 JUnit5支持.DropwizardExtensionsSupport

具体来说,如果您需要在测试的开始/结束时启动/停止应用程序(这是什么DropwizardAppRule),那么就DropwizardAppExtension可以使用.

你的例子,为JUnit5重写:

@ExtendWith(DropwizardExtensionsSupport.class)
public class MyTest {

    public static final DropwizardAppExtension<Config> app = new DropwizardAppExtension<>(MyApp.class);

    ...

       // app.getLocalPort() is also available

}
Run Code Online (Sandbox Code Playgroud)

不幸的是,JUnit5支持似乎还没有记录.

链接: