Hi All,
Thanks again to stay here!
Today, I would like to show you how to create a java module configuration to test your WS service using Arquillian, Selenium, Spring Boot and Junit...
Pre-Requirements
JBoss EAP 6.x
Java 7 or 8
Maven 3.3
So, let's do it!
First of all and the most important point: configure your pom.xml with frameworks and appropriate versions to run tests.
Arquillian
Define your arquillian.xml file into test/resources:
<arquillian xmlns="http://jboss.org/schema/arquillian"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://jboss.org/schema/arquillian
http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
<defaultProtocol type="Servlet 3.0"/>
<container qualifier="jboss" default="true">
<configuration>
<property name="jbossHome">/opt/java/jboss/jboss-eap-6.4/</property>
<property name="serverConfig">standalone-full-ha.xml</property>
<property name="javaVmArguments">-Xms512m -Xmx1024m -XX:MaxPermSize=256m -XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC -XX:+UseCompressedOops -XX:+AggressiveOpts -XX:+HeapDumpOnOutOfMemoryError -noverify -Xss512k -XX:StackShadowPages=8 -XX:+UseFastAccessorMethods</property>
<property name="managementAddress">localhost</property>
<property name="managementPort">9999</property>
<property name="username">admin</property>
<property name="password">yadayadaxpto</property>
</configuration>
</container>
</arquillian>
Put the versions and packages into pom.xml
<arquillian.version>1.1.2.Final</arquillian.version>
<arquillian.warp.version>1.0.0.Alpha6</arquillian.warp.version>
<arquillian.rest-client.version>1.0.0.Alpha2</arquillian.rest-client.version>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.protocol</groupId>
<artifactId>arquillian-protocol-servlet</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-rest-client-impl-jersey</artifactId>
<version>1.0.0.Alpha3</version>
<exclusions>
<exclusion>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
</exclusion>
<exclusion>
<artifactId>javax.ws.rs</artifactId>
<groupId>javax.ws.rs-api</groupId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-warp</artifactId>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
<dependency>
<!-- http://arquillian.org/modules/contacts-extension/ -->
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-rest-warp-impl-jaxrs-2.0</artifactId>
<version>${arquillian.rest-client.version}</version>
<exclusions>
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-rest-client-impl-3x</artifactId>
<version>${arquillian.rest-client.version}</version>
<exclusions>
<exclusion>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-rest-client-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-rest-client-impl-base</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-rest-warp-api</artifactId>
</dependency>
Create your test base class
@RunWith(Arquillian.class)
public class ArquillianDefaultTest {
@Deployment
public static WebArchive createTestArchive() {
return ShrinkWrap.create(WebArchive.class, "test-arquillian.war")
.addPackages(true, Filters.exclude(".*Test.*"),
ArquillianDefaultTest.class.getPackage())
.addAsLibraries(Maven.resolver().resolve("javax.ws.rs:javax.ws.rs-api:2.0").withTransitivity().asFile())
.addAsLibraries(Maven.resolver().resolve("org.jboss.arquillian.extension:arquillian-rest-warp-api:1.0.0.Alpha2").withTransitivity().asFile())
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
@RunAsClient
@Test
public void testWS(@ArquillianResource URL deploymentURL) throws URISyntaxException {
WebTarget target = buildWebTarget(deploymentURL);
System.out.println("WebTarget: " + target);
assertNotNull(target);
}
}
Spring Boot
<spring.boot.version>1.3.5.RELEASE</spring.boot.version>
<spring.boot.maven.plugin.version>1.3.5</spring.boot.maven.plugin.version>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring.boot.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot.version}</version>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
<scope>provided</scope>
</dependency>
Test Sample
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = BaseSpringBootApplicationStartup.class)
@WebAppConfiguration
@IntegrationTest("server.port:7070")
public class ApplicationSpringBootITest extends FluentTest {
@Value("${local.server.port:7070}")
private int serverPort;
protected WebDriver webDriver;
@Before
public void setUp() {
webDriver = new FirefoxDriver();
webDriver.get("http://google.com");
System.out.println(webDriver.getPageSource());
}
@After
public void teardown() {
if (webDriver != null) {
webDriver.quit();
}
}
@Override
public WebDriver getDefaultDriver() {
return webDriver;
}
protected String getUrl() {
return "http://localhost:" + serverPort;
}
@Test
public void hasPageTitleMock() {
when().
get(getUrl() + "/homeMock").
then().
assertThat().
statusCode(200);
}
}
Selenium and JUnit
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>2.53.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.53.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>2.53.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
<version>2.53.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
It's done! But I just want to show the libraries and versions here, because you can find all details about implementation at my github project.
So, check all artefacts and classes over here:
https://github.com/alvesfred/samples-jboss/tree/master/samples-test
How to Run the tests or the environment...
1 - mvn clean install or
2 - mvn clean integration-test -P integration-ws-test
Thanks a lot and regards!
No comments:
Post a Comment