BedCon 2015
Integrationstests mit Docker
Docker-Compose: Einstellungen für den Container
Daten nicht im Container vorhalten. Von außen importieren und anschließend wieder löschen
Oracle Testdaten:
Docker container mit oracle-xe starten,
mit create db-link aus Docker heraus zur "echten" Datenbank,
copy tables (dump tables wo contraints -> importieren mittels sqlplus), copy data (mittels insert into table as select * from table@dblink ), copy contrainsts.., drop db-link, oracle-xe stoppen
Docker image davon erzeugen
--> Docker image mit Oracle-DB und Testdaten
Maven plugin: rhuss/maven-docker-plugin
Gradle kann noch mehr..
Freitag, 18. September 2015
Donnerstag, 17. September 2015
Freitag, 30. Januar 2015
REST and SOAP Webservice Maven Project
REST and SOAP Webservice Maven Project
This is a sample project, which consists of a REST and a SOAP hello world webservice.The application is being built and tested during a maven run.
Maven
- compiles
- packages the application
- starts a tomcat
- deployes the application
- runs tests against the webservice
- stops the tomcat afterwards
Implementation:
- Java: JDK 8_25
- Rest: jax-rs: jersey
- Soap: jax-ws: sun's jaxws-rt
Automated integration test:
- Rest: JUnit with rest-assured
- Soap: JUnit with wsimport
Project structure:
- src/main/java
- org.klarblick.webservice/
- rs/
- resource/
- HelloWorldResource.java
- RestWebservice.java
- ws/
- impl/
- SoapWebserviceImpl.java
- SoapWebservice.java
- src/test/java
- org.klarblick.webservice
- rs
- RestRunningIT.java
- ws
- SoapRunningIT.java
- pom.xml
Source-Code:
RestWebservice.java
package org.klarblick.webservice.rs;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import org.klarblick.webservice.rs.resource.HelloWorldResource;
import java.util.HashSet;
import java.util.Set;
/**
*/
@ApplicationPath("/rs")
public class RestWebservice extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
// register root resource
classes.add(HelloWorldResource.class);
return classes;
}
}
HelloWorldResource.java
package org.klarblick.webservice.rs.resource;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("hello")
public class HelloWorldResource {
@GET
@Produces("text/plain")
public String getHello() {
return "Hello World from Rest!";
}
}
SoapWebservice.java
package org.klarblick.webservice.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;
@WebService
@SOAPBinding(style = Style.DOCUMENT, use = Use.LITERAL)
public interface SoapWebservice {
@WebMethod
public String getHello();
}
SoapWebserviceImpl.java
package org.klarblick.webservice.ws.impl;
import javax.jws.WebService;
import org.klarblick.webservice.ws.SoapWebservice;
@WebService(endpointInterface = "org.klarblick.webservice.ws.SoapWebservice")
public class SoapWebserviceImpl implements SoapWebservice {
public SoapWebserviceImpl() {
}
public String getHello() {
return "Hello World from Soap!";
}
}
Source-Code Tests:
RestRunningIT.java
package org.klarblick.webservice;
import static com.jayway.restassured.RestAssured.*;
import org.junit.Test;
public class RestRunningIT {
@Test
public void testStatusOK() {
given().port(8082).
expect().
statusCode(200).
when().
get("/klarblick.webservice/rs/hello");
}
}
SoapRunningIT.java
package org.klarblick.webservice;
import java.lang.reflect.InvocationTargetException;
import javax.xml.ws.BindingProvider;
import org.junit.Assert;
import org.junit.Test;
public class SoapRunningIT {
/**
* Using Java reflections because client stub classes are generated during maven runtime
*/
@Test
public void testSoapService(){
try {
Object webservicePort = getWebserviceClient();
String helloResponse = (String)callMethod(webservicePort, "getHello");
Assert.assertEquals("Hello World from Soap!", helloResponse);
} catch (Exception ex) {
ex.printStackTrace();
Assert.fail(ex.getMessage());
}
}
private Object getWebserviceClient() throws InstantiationException,
IllegalAccessException, ClassNotFoundException,
NoSuchMethodException, InvocationTargetException {
Object webservice = Class.forName("org.klarblick.webservice.ws.impl.SoapWebserviceImplService").newInstance();
Object webservicePort = Reflections.callMethod(webservice, "getSoapWebserviceImplPort");
BindingProvider bp = (BindingProvider)webservicePort;
bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
"http://localhost:8082/klarblick.webservice/ws/hello");
return webservicePort;
}
private static Object callMethod(Object obj, String name) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException {
return obj.getClass().getMethod(name, new Class[]{}).invoke(obj, new Object[]{});
}
}
POM File
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.klarblick</groupId>
<artifactId>klarblick.webservice</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>klarblick.webservice</name>
<properties>
<jersey.version>2.15</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<finalName>klarblick.webservice</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<excludes>
<exclude>**/*IT.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.17</version>
<configuration>
<excludes>
<exclude>**/*Test.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8082</port>
<path>/${project.name}</path>
<useTestClasspath>true</useTestClasspath>
</configuration>
<executions>
<!-- At pre-integration-test phase, run the war in an embedded Tomcat
server. -->
<execution>
<id>start-tomcat</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run-war-only</goal>
</goals>
<configuration>
<port>8082</port>
<fork>true</fork>
</configuration>
</execution>
<!-- At post-integration-test phase, stop the embedded Tomcat server. -->
<execution>
<id>stop-tomcat</id>
<phase>post-integration-test</phase>
<goals>
<goal>shutdown</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>generate-wsdl</id>
<phase>process-classes</phase>
<goals>
<goal>wsgen</goal>
</goals>
<configuration>
<sei>org.klarblick.webservice.ws.impl.SoapWebserviceImpl</sei>
<genWsdl>true</genWsdl>
<resourceDestDir>${basedir}/src/main/webapp/WEB-INF/wsdl</resourceDestDir>
<sourceDestDir>${basedir}/src/gen/java</sourceDestDir>
</configuration>
</execution>
<execution>
<id>generate-stubs</id>
<phase>process-classes</phase>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<wsdlFiles>
<wsdlFile>${basedir}/target/klarblick.webservice/WEB-INF/wsdl/SoapWebserviceImplService.wsdl</wsdlFile>
</wsdlFiles>
<!-- <wsdlDirectory>${basedir}/target/klarblick.webservice/WEB-INF/wsdl</wsdlDirectory> -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.jvnet.jax-ws-commons
</groupId>
<artifactId>
jaxws-maven-plugin
</artifactId>
<versionRange>
[2.2,)
</versionRange>
<goals>
<goal>wsgen</goal>
<goal>wsimport</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencyManagement>
<dependencies>
<!-- REST Jersey -->
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- REST Jersey -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
</dependency>
<!-- uncomment this to get JSON support <dependency> <groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId> </dependency> -->
<!-- SOAP -->
<dependency>
<groupId>javax.jws</groupId>
<artifactId>jsr181-api</artifactId>
<version>1.0-MR1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2.6</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>2.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Run the project
mvn clean package verify
Abonnieren
Posts (Atom)