I have the following multi module project structure
root
|
|
---- project A with arquillian testcases
|
|
---- project B builds war and runs same tests with different deployment config
I want to run my testcases with two different deployment configurations.
In project A I run the testcases with Arquillian. Then, I run the same testcases with the testcontainers framework after the war is built in project B.
What is the best approach to achieve this? I see here basically two alternatives:
- Duplicate the same testcases in project A and project B
- Having a base class containing the testcases which is extended by the specific classes which override the deployment config methods
With method 2, which normally I would prefer, I see some issues:
- The testcase base class needs to be shared across multiple projects and I read in different discussion that it is a bad practice to share test code across projects.
So what do you think? What would you guys do? If you know other ways to share the testcases except of duplicating the code or extending, please let me know.
Thanks in advance.
Example method 1:
Project A contains following testclass:
@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());
}
}
Project B contains following testclass:
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());
}
}
Example method 2:
Project A contains following testclasses:
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());
}
}
and
@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());
}
}
Project B contains following testclass which now only extends the basetestcases class
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");
}
}