我具有以下多模块项目结构
root
|
|
---- project A with arquillian testcases
|
|
---- project B builds war and runs same tests with different deployment config
我想用两个不同的部署配置运行测试用例。
在项目A中,我使用Arquillian运行测试用例。然后,在项目B中建立了战争之后,我使用testcontainers框架运行相同的测试用例。
实现此目标的最佳方法是什么?我在这里基本上看到了两种选择:
通常我希望使用方法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");
}
}