gpt4 book ai didi

java - 用于启动和停止已部署应用程序的简单 Arquillian 测试

转载 作者:行者123 更新时间:2023-11-28 22:38:03 26 4
gpt4 key购买 nike

我正在尝试全神贯注于 Arquillian,甚至可能开始在我的项目中使用它。我有一个简单的 Java Web 应用程序,它作为 WAR 部署到 Tomcat。

在我的项目中,我定义了一个 ServletContextListener 实现,这样我就可以在 Tomcat 启动和停止应用程序时执行代码。

我正在尝试编写一个 super 简单的 Arquillian 测试类,它使用 ShrinkWrap 和:

  1. 确认我的捆绑 WAR 可以部署到 Tomcat 并在没有抛出异常的情况下启动;和
  2. 可以在应用程序运行后访问一个简单的系统属性(ServletContextListener 检查);和
  3. 确认当 Tomcat 关闭时,没有抛出异常(干净关闭)

此外,我实现 ServletContextListener 的类称为 AppLifecycleManager:

public class AppLifeCycleManager implements ServletContextListener {
private String logLevel;

// Injected by Guice, but that's not really relevant for this question.
@Inject
private Logger logger;

// Getter and setter for logLevel and logger

@Override
public void contextInitialized(ServletContextEvent event) {
logLevel = System.getProperty("log.level");
}

@Override
public void contextDestroyed(ServletContextEvent event) {
logger.info("Peacefully shutting down the application.");
}
}

到目前为止,这是我最好的尝试:

@RunWith(Arquillian.class)
public class MyFirstRealIntegrationTest {
@Deployment
public static Archive<?> createDeployment() {
// Haven't figured this part out yet, but for the sake of
// this question lets pretend this returns a properly-packaged
// WAR of my web app, the same that my Ant build currently produces.
}

@Test
public void shouldBeAbleToStartTomcatWithoutExceptions() {
// Given
Archive war = createDeployment();

// When - deploy war to Tomcat container
try {
// ??? how to access/init a Tomcat container?
TomcatContainer tomcat = new TomcatContainer(); // this is wrong
tomcat.start();
} catch(Throwable throwable) {
// Starting the container should not throw exceptions
Assert.fail();
}
}

@Test
public void shouldBeAbleToStopTomcatWithoutExceptions {
// Same setup as above test but stops tomcat and checks for
// thrown exceptions. Omitted for brevity.
}

@Test
public void shouldHaveAccessToSysPropsOnceRunning() {
// Here, deploy to the container and start it.
// Then, confirm that AppLifecycleManager correctly read
// the log.level system property.

// Given
Archive war = createDeployment();
TomcatContainer tomcat = new TomcatContainer();

// When - AppLifeycleManager should now read the system property
tomcat.start();

// Then - make sure log.level was set to "DEBUG" and that it was
// correctly read by AppLifeCycleManager.
Assert.assertTrue(war.getClass(AppLifeCycleManager.class)
.getLogLevel().equals("DEBUG"));
}
}

因此,鉴于我的方法,我立即遇到了几个问题:

  1. 我不确定如何访问/实例化我的 Tomcat 容器,以便它甚至可以启动/停止
  2. 我不确定如何从我正在运行/已部署的网络应用程序中实际执行测试。在上面的第三个测试中,我使用了 war.getClass(AppLifeCycleManager.class).getLogLevel() 来尝试访问“实时”类实例并检查其 logLevel 属性运行时值,但我知道这是错误的。

所以我想问:一位身经百战的 Arquillian 老兵将如何编写这 3 个简单的测试,以及我实际上如何从 JUnit 测试内部对我“正在运行的”网络应用程序执行测试?提前致谢。

最佳答案

我认为您不应该在测试中处理 tomcat 的启动/关闭。如果您使用嵌入式 tomcat 容器,我会容易得多:https://docs.jboss.org/author/display/ARQ/Tomcat+7.0+-+Embedded .在这种情况下,arquillian 将处理 tomcat 的启动和关闭。

您应该在使用@Deployment 注释的方法中创建部署。只需阅读以下指南:http://arquillian.org/guides/getting_started/

关于java - 用于启动和停止已部署应用程序的简单 Arquillian 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14694962/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com