在Java中使用不同的部署配置运行相同的测试用例

时间:2018-07-10 07:55:28

标签: java gradle multi-project

我具有以下多模块项目结构

root | | ---- project A with arquillian testcases | | ---- project B builds war and runs same tests with different deployment config

我想用两个不同的部署配置运行测试用例。

在项目A中,我使用Arquillian运行测试用例。然后,在项目B中建立了战争之后,我使用testcontainers框架运行相同的测试用例。

实现此目标的最佳方法是什么?我在这里基本上看到了两种选择:

  1. 在项目A和项目B中复制相同的测试用例
  2. 具有一个包含测试用例的基类,该基类由覆盖部署配置方法的特定类扩展了

通常我希望使用方法2,但会遇到一些问题:

  • 需要在多个项目之间共享测试用例的基类,我在不同的讨论中读到,在项目之间共享测试代码是一种不好的做法。

那你怎么看?你们会做什么?如果您知道其他方法来共享测试用例,除了复制代码或扩展之外,请告诉我。

谢谢。

示例方法1:

项目A包含以下测试类:

@RunWith(Arquillian.class)
public class YTest
{
@ArquillianResource
private URL webappUrl;

/**
 * @return {@link JavaArchive}
 */
@Deployment(name = "myservice")
public static Archive<?> createTestArchive()
{
    final WebArchive archive = ShrinkWrap
        .create(WebArchive.class, "myservice.war")
        .addClasses(X.class, Y.class);

    return archive;
}

private WebTarget webTarget()
{
    return ClientBuilder.newClient().target(webappUrl.toExternalForm());
}

@Test
public void whenPostServiceShouldReturn200() throws IOException
{
    final Response response = webTarget()
        .path("/myservice")
        .request()
        .header("Content-Type", "application/json")
        .post(Entity.entity(TestDataGeneration.generatePayload, MediaType.APPLICATION_JSON_TYPE));

    assertEquals(200, response.getStatus());
}
}

项目B包含以下测试类:

public class Y2Test
{
private static final Logger LOGGER = LoggerFactory.getLogger(Y2Test.class);

@ClassRule
public static GenericContainer<?> webAppContainer = new GenericContainer<>(
    new ImageFromDockerfile().withFileFromPath("..", new File("..").toPath()))
        .withLogConsumer(new Slf4jLogConsumer(LOGGER))
        .withExposedPorts(9080);


private WebTarget webTarget()
{
    return ClientBuilder
        .newClient()
        .target(
            "http://"
                + webAppContainer.getContainerIpAddress()
                + ":"
                + webAppContainer.getMappedPort(9080)
                + "/provisioning-service");
}

@Test
public void whenPostServiceShouldReturn200() throws IOException
{
    final Response response = webTarget()
        .path("/myservice")
        .request()
        .header("Content-Type", "application/json")
        .post(Entity.entity(TestDataGeneration.generatePayload(), MediaType.APPLICATION_JSON_TYPE));

    assertEquals(200, response.getStatus());
}
}

示例方法2:

项目A包含以下测试类:

public abstract class YTestCases
{
abstract WebTarget webTarget();

@Test
public void whenPostMyServiceCorrectlyShouldReturn200() throws IOException
{
    final Response response = webTarget()
        .path("/registerphone")
        .request()
        .header("Content-Type", "application/json")
        .post(null);

    assertEquals(200, response.getStatus());
}
}

@RunWith(Arquillian.class)
public class YTest extends YTestCases
{
@ArquillianResource
private URL webappUrl;

/**
 * @return {@link JavaArchive}
 */
@Deployment(name = "myservice")
public static Archive<?> createTestArchive()
{
    final WebArchive archive = ShrinkWrap
        .create(WebArchive.class, "myservice.war")
        .addClasses(A.class, B.class);

    return archive;
}

@Override
WebTarget webTarget()
{
    return ClientBuilder.newClient().target(webappUrl.toExternalForm());
}
}

项目B包含以下测试类,现在仅扩展了basetestcases类

public class Y2T extends YTestCases
{
private static final Logger LOGGER = LoggerFactory.getLogger(Y2T.class);
@ClassRule
public static GenericContainer<?> webAppContainer = new GenericContainer<>(
    new ImageFromDockerfile().withFileFromPath("..", new File("..").toPath()))
        .withLogConsumer(new Slf4jLogConsumer(LOGGER))
        .withExposedPorts(9080);

@Override
WebTarget webTarget()
{
    // cassandra.getContainerIpAddress();
    // cassandra.getMappedPort(9042);
    return ClientBuilder
        .newClient()
        .target(
            "http://"
                + webAppContainer.getContainerIpAddress()
                + ":"
                + webAppContainer.getMappedPort(9080)
                + "/myservice");
}
}

0 个答案:

没有答案