unit- and integration testing with maven -...

38
Web Site: www.soebes.com EMail: [email protected] Dipl.Ing.(FH) Karl-Heinz Marbaise Unit- and Integration Testing with Maven

Upload: others

Post on 03-May-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

Web Site:www.soebes.com

EMail:[email protected] Dipl.Ing.(FH) Karl-Heinz Marbaise

Unit- andIntegration Testing

with Maven

Page 2: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

2© www.soebes.de 2012 Apache Con 2012 – Release 1.0

Agenda

1.Overview Maven

2.Maven Lifecycle

3.Unit Tests

4.Unit Tests Multi Module

5.Integration Tests

6.Maven Plugin Development

Page 3: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

3© www.soebes.de 2012 Apache Con 2012 – Release 1.0

1. Overview - Maven

● Official Web Site● http://maven.apache.org

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.

Page 4: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

4© www.soebes.de 2012 Apache Con 2012 – Release 1.0

1. Overview - Maven

● Currently Maven 3.0.4 is most recent version● Convention over Configuration● Large number of plugins

– jar, war, ear, ejb, rpm, assembly, appassembler etc.● Support in many tools like CI (Jenkins, Hudson,

TeamCity, Bamboo etc.), IDE (Eclipse, IntelliJ, Netbeans etc.)

Page 5: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

5© www.soebes.de 2012 Apache Con 2012 – Release 1.0

1. Overview - Maven

● Separate folder for production code andappropriate resources

● Separate folder forunit test code andappropriate resources

https://github.com/khmarbaise/sapmhttp://khmarbaise.github.com/sapm/

Page 6: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

6© www.soebes.de 2012 Apache Con 2012 – Release 1.0

1. Overview - Maven

● Maven Coordinates● Identify an artifact

– groupIdgroupId, artifactIdartifactId, versionversion (GAV), packaging (default: jar; ejb, war, ear, rpm etc.), classifier (jdk15 etc.)

– Version● Released artifacts:

– 1.2.0, 3.1,...● Not Released artifacts (These artifacts are currently under development):

– 1.2.0-SNAPSHOT, 3.1-SNAPSHOT,...● Example:

org.bouncycastle:bcprov-jdk15:jar:1.45

Page 7: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

7© www.soebes.de 2012 Apache Con 2012 – Release 1.0

1. Overview - Maven

● Dependency Management● Simple as well as transitive

dependencies.– Your project uses

the Tika library.– What about the

dependencies of the Tikalibrary?● This is handled by Maven

which are called transivite dependencies.transivite dependencies.

Page 8: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

8© www.soebes.de 2012 Apache Con 2012 – Release 1.0

2. MavenDefault Lifecycle

● validateinitialize

● generate-sourcesprocess-sourcesgenerate-resourcesprocess-resources

● compileprocess-classes

Page 9: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

9© www.soebes.de 2012 Apache Con 2012 – Release 1.0

2. MavenDefault Lifecycle

● generate-test-sourcesgenerate-test-sourcesprocess-test-sourcesprocess-test-sources

generate-test-resourcesgenerate-test-resourcesprocess-test-resourcesprocess-test-resources

test-compiletest-compileprocess-test-classesprocess-test-classes

testtest to be continuedto be continued

Page 10: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

10© www.soebes.de 2012 Apache Con 2012 – Release 1.0

3. Unit TestsResponsibilities

● Maven-resources-plugin● Goal: resourcesresources:testResources

– copying resources from src/test/resources folder to target/test-classes/

● Lifecycle-Phase: process-test-resources

● Maven-compiler-plugin● Goal: compilercompiler:testCompile

– Compiling test code src/test/java to target/test-classes● Lifecycle-Phase: test-compile

Page 11: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

11© www.soebes.de 2012 Apache Con 2012 – Release 1.0

3. Unit TestsResponsibilities

● Maven-surefire-plugin● Goal: surefiresurefire:test

– Execute the unit tests● Lifecycle-Phase: test

Page 12: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

12© www.soebes.de 2012 Apache Con 2012 – Release 1.0

3. Unit TestsBasic Structure

● Execution of the unit testsis done by:

● maven-surefire-plugin

● The naming conventionfor unit tests:

● **/Test*.java● **/*Test.java● **/*TestCase.java

Page 13: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

13© www.soebes.de 2012 Apache Con 2012 – Release 1.0

3. Unit TestsBasic Structure

● Execution:[INFO] --- maven-surefire-plugin:2.12:test (default-test) @ unit-test-example ---[INFO] Surefire report directory: /example/src/main/resources/ut-example/target/surefire-reports

------------------------------------------------------- T E S T S-------------------------------------------------------Running com.soebes.training.maven.simple.BitMaskTestTests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.089 sec

Results :

Tests run: 5, Failures: 0, Errors: 0, Skipped: 0[INFO]

See real demo output

Page 14: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

14© www.soebes.de 2012 Apache Con 2012 – Release 1.0

4. Unit Tests – Multi ModuleBasic Structure

● Execution of the unit testsis done module by module.

● Every module runs itsunit test separately.

Page 15: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

15© www.soebes.de 2012 Apache Con 2012 – Release 1.0

4. Unit Tests – Multi ModuleExecution

[INFO] --- maven-surefire-plugin:2.12:test (default-test) @ ut-example-mm-mod1 ---[INFO] Surefire report directory: /project/mod1/mod1/target/surefire-reports------------------------------------------------------- T E S T S-------------------------------------------------------Running com.soebes.training.maven.simple.BitMaskTestTests run: 5, Failures: 0, Errors: 0, Skipped: 0, Timeelapsed: 0.066 sec

Results :

Tests run: 5, Failures: 0, Errors: 0, Skipped: 0……

Page 16: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

16© www.soebes.de 2012 Apache Con 2012 – Release 1.0

4. Unit Tests – Multi ModuleExecution

....[INFO] --- maven-surefire-plugin:2.12:test (default-test) @ ut-example-mm-mod2 ---[INFO] Surefire report directory: /project/mod2mod2/target/surefire-reports

------------------------------------------------------- T E S T S-------------------------------------------------------Running com.soebes.training.maven.simple.BitMaskTestTests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.078 sec

Results :

Tests run: 5, Failures: 0, Errors: 0, Skipped: 0

See real demo...

Page 17: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

17© www.soebes.de 2012 Apache Con 2012 – Release 1.0

4. Unit Tests – Multi ModuleCommon Code

● What about common code in unit tests?● Sometimes the problem occurs to have unit test code in

common between modules and of course don't want to duplicate it.

How to solve this?

Page 18: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

18© www.soebes.de 2012 Apache Con 2012 – Release 1.0

4. Unit Tests – Multi ModuleCommon Code

● In the module (mod-ut-propagate) you want to propagate code from to others:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <executions> <execution> <goals> <goal>test-jar</goal> </goals> </execution> </executions> </plugin>

Page 19: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

19© www.soebes.de 2012 Apache Con 2012 – Release 1.0

4. Unit Tests – Multi ModuleCommon Code

● In the module you want to use the code of the other module:

<dependency> <groupId>${project.groupId}</groupId> <artifactId>mod-ut-propagate</artifactId> <version>${project.version}</version> <type>test-jar</type><type>test-jar</type> <scope>test</scope> <scope>test</scope> </dependency>

Page 20: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

20© www.soebes.de 2012 Apache Con 2012 – Release 1.0

4. Unit Tests – Multi ModuleCommon Code

● Propagate all classes (src/test/java as well as src/test/resources) of the module mod-ut-mod-ut-propagatepropagate to other modules classpath which are using it.

● Sometimes you don't like that.● The solution for this problem create a separate unit-test-

common module.

Page 21: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

21© www.soebes.de 2012 Apache Con 2012 – Release 1.0

4. Unit Tests – Company WideCommon Code

● Create a separate project in Maven● Create a release of it● And use it as usual dependency

<dependency> <groupId>com.soebes.modules</groupId> <artifactId>unit-test</artifactId> <version>1.0.0</version> <type>test-jar</type><type>test-jar</type> <scope>test</scope> <scope>test</scope> </dependency>

Page 22: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

22© www.soebes.de 2012 Apache Con 2012 – Release 1.0

2. MavenDefault Lifecycle – Part 2

● prepare-packagepackage

● pre-integration-testpre-integration-testintegration-testintegration-testpost-integration-testpost-integration-test

● verify● install

deploy

Page 23: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

23© www.soebes.de 2012 Apache Con 2012 – Release 1.0

5. Integration TestsResponsibilities

● Maven-failsafe-plugin● Goal: failsafefailsafe:integration-test

– Execute the integration tests● Lifecycle-Phase: integration-test

Page 24: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

24© www.soebes.de 2012 Apache Con 2012 – Release 1.0

5. Integration TestsBasic Structure

● Execution of the unit testsis done by:

● maven-failsafe-plugin

● The naming conventionfor integration tests:

● **/IT*.java● **/*IT.java● **/*ITCase.java

Page 25: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

25© www.soebes.de 2012 Apache Con 2012 – Release 1.0

5. Integration TestsBasic Structure

● The configuration ofmaven-failsafe-pluginis needed as follows:

● Target folder for compiledclasses:target/test-classes.

Page 26: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

26© www.soebes.de 2012 Apache Con 2012 – Release 1.0

5. Integration TestsBasic Structure

Page 27: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

27© www.soebes.de 2012 Apache Con 2012 – Release 1.0

5. Integration TestsSeparate Source Folder

● Separate source folder forintegration tests.

Page 28: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

28© www.soebes.de 2012 Apache Con 2012 – Release 1.0

5. Integration TestsSeparate Source Folder

● Use build-helper-maven-pluginto add the separate sourcelocation.

Page 29: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

29© www.soebes.de 2012 Apache Con 2012 – Release 1.0

5. Integration TestsSeparate Module

● Define a separate module with the integration tests.

● Separation of● compiling● configuration● Running

● activate/deactivate via profile.

See Example

Page 30: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

30© www.soebes.de 2012 Apache Con 2012 – Release 1.0

6. Maven Plugin Development Integration Tests

● Typical Integration Tests for plugins should simulate a full Maven environment with:

● A Repository● Calling different lifecycle-phases and/or goals● Checking the results● Etc.

● The maven-invoker-plugin is intended for such purposes.

Page 31: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

31© www.soebes.de 2012 Apache Con 2012 – Release 1.0

6. Maven Plugin DevelopmentIT's - Basic Structure

+- pom.xml+- src/ +- it/ +- settings.xml +- first-it/ | +- pom.xml | +- src/ | +- invoker.properties | +- verify.bsh +- second-it/ +- pom.xml +- invoker.properties +- verify.bsh +- src/

Page 32: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

32© www.soebes.de 2012 Apache Con 2012 – Release 1.0

6. Maven Plugin DevelopmentIT's - Repository

● settings.xml is used to simulate a Maven Remote Repository during the integration tests.

● It's using the users localmaven repository($HOME/.m2/repository).

Page 33: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

33© www.soebes.de 2012 Apache Con 2012 – Release 1.0

6. Maven Plugin Development Integration Tests

● “first-it” describesa usual Mavenproject.

+- pom.xml+- src/ +- it/ +- settings.xml +- first-it/ | +- pom.xml | +- src/ | +- invoker.properties | +- verify.bsh +- second-it/ +- pom.xml +- invoker.properties +- verify.bsh +- src/

Page 34: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

34© www.soebes.de 2012 Apache Con 2012 – Release 1.0

6. Maven Plugin Development Integration Tests

● The pom.xml is more or less ausual pom.xmlexcept:

● @project.groupId@● @project.artifactId@● @project.version.@

Page 35: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

35© www.soebes.de 2012 Apache Con 2012 – Release 1.0

6. Maven Plugin Development Integration Tests

Page 36: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

36© www.soebes.de 2012 Apache Con 2012 – Release 1.0

6. Maven Plugin Development Integration Tests

● Problem● Artifacts are not in the local repository

● Solution● Usage of the Mock Repository Manager Plugin

Page 37: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

37© www.soebes.de 2012 Apache Con 2012 – Release 1.0

Questions?

● Contact:

[email protected]

Page 38: Unit- and Integration Testing with Maven - ApacheConarchive.apachecon.com/...Apache_Daily/aceu-2012-unit-and-integrati… · Web Site: EMail: apachecon@soebes.com Dipl.Ing.(FH) Karl-Heinz

38© www.soebes.de 2012 Apache Con 2012 – Release 1.0

References

● Maven Homepage● http://maven.apache.org

● Mailing lists (User)● http://maven.apache.org/mail-lists.html

● Maven References● http://www.sonatype.com/Support/Books