Arquillian 和@BeforeClass、@AfterClass 注解

Arquillian and @BeforeClass, @AfterClass annotations

看起来提到的注释是在部署内部执行的。我需要它们在 运行 外面,假设在启动时启动一些模拟器 class 并在结束时停止它。我该怎么做?模拟器使用套接字通信,不应在服务器内部启动。

如何将 arquillian 与 "plain" junit(不在容器中执行)混合使用。

您可以将 arquillian @RunAsClient 注释与 junit @BeforeClass 和 @AfterClass 结合使用:

@BeforeClass 
@RunAsClient // runs as client
public static void shouldBeAbleToRunOnClientSideBeforeRemoteTest() throws Exception {
    System.out.println("before!!");
}

@AfterClass 
@RunAsClient // runs as client
public static void shouldBeAbleToRunOnClientSideAfterRemoteTest() throws Exception {
    System.out.println("after!!");
}

Franck 给出的答案肯定有效,并且可能是大多数用户想要使用的答案。然而,如果您需要获得更多关于正在发生的事情的细节,或者需要更多的控制,您当然可以连接到 Arquillian 生命周期并为 Arquillian 发出的各种 events 注册观察者。不幸的是,它不像收听 CDI 事件那么容易。

您需要在 META-INF/services 中创建一个文件名为 org.jboss.arquillian.core.spi.LoadableExtension 的服务条目。该文件的内容将是实现 Arquillian 的 LoadableExtension 接口的 类 的完全限定名称 (FQN)。然后,您可以在 register(ExtensionBuilder) 方法中注册任何将观察事件的 类。那些 类 只需要一个 public void methodName(@Observes EventType) 方法来处理他们想要监听的所有事件。 @Observes 注释在 org.jboss.arquillian.core.api.annotation 包中。

您可以在 Arquillian Recorder Reporter 扩展 here, here, and here 中看到这一点。我知道这可能比大多数人想要做的要多,但同样,如果您需要强大的功能和挂钩,Arquillian 应该能够满足您的需求。