Flyway plugin with Maven for integration test - java

I want to run Flyway plugin when running integration test in Maven. For integration tests I'm using failsafe plugin.
First of all is it possible to define Flyway plugin two times? One for general usage (eg. from command line) and one for integration test? How to define a seperate configuration in Flyway plugin for integration tests?

You can achieve this through different executions of the plugin. Each execution can have its own configuration.

You can add an execution for Failsafe's pre-integration-test phase with a different configuration, see Maven Failsafe Plugin:
The Maven lifecycle has four phases for running integration tests:
pre-integration-test for setting up the integration test environment.
integration-test for running the integration tests.
post-integration-test for tearing down the integration test environment.
verify for checking the results of the integration tests.
and Guide to Configuring Plug-ins:
Using the <executions> Tag
You can also configure a mojo using the tag. This is most commonly used for mojos that are intended to participate in some phases of the build lifecycle.
For example:
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>4.0.3</version>
<configuration>
<url>jdbc:jtds:sqlserver://myCompany.com/generalDatabase</url>
<user>dbUser</user>
<password>password</password>
<locations>
<location>filesystem:src/main/resources/db/migration</location>
</locations>
</configuration>
<dependencies>
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.2.7</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<executions>
<execution>
<id>integration-test-database-setup</id>
<phase>pre-integration-test</phase>
<goals>
<goal>clean</goal>
<goal>migrate</goal>
</goals>
<configuration>
<url>jdbc:jtds:sqlserver://myCompany.com/testDatabase</url>
<user>dbUser</user>
<password>password</password>
<locations>
<location>filesystem:src/test/resources/db/migration</location>
</locations>
</configuration>
</execution>
</executions>
</plugin>

Related

Skip running PITest in maven build

I'm trying to run a maven build from command line and exclude PITest from running any mutations. Currently the reports are failing and we need to be able to give a parameter to ignore running the mutation tests or ignore the results and continue the build
I've running with some parameters like mvn package -Dpit.report=true
or mvn package -Dmaven.report.skip=true
This is the PITest setup in my pom
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>1.1.10</version>
<configuration>
<timestampedReports>false</timestampedReports>
<mutationThreshold>95</mutationThreshold>
</configuration>
<executions>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>mutationCoverage</goal>
</goals>
</execution>
</executions>
</plugin>
The problem is that is is still running PITest and causing the build to fail
There is no native way of skipping a plugin execution, but there are least 2 workarounds:
First, is adding a property to override execution phase:
Define a property pitPhase with default value as the default phase of plugin execution.
Then in plugin configuration:
<execution>
<phase>${pitPhase}</phase>
...
</execution>
After that, when you want to skip execution mvn -DskipPit=pitPhase package
The other alternative is to add a Maven profile with the plugin execution
The execution of Pitests can be skipped in Maven.
In your pom.xml:
Set in general properties:
<properties>
<pitest.execution.skip>true</pitest.execution.skip>
</properties>
Set in the plugin:
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>Your_Version</version>
<configuration>
<skip>${pitest.execution.skip}</skip>
</configuration>
</plugin>
Since 1.4.11 there is the option skipPitest. See here: https://github.com/hcoles/pitest/releases/tag/pitest-parent-1.4.11
So you do: -DskipPitest

Automation of JMeter tests using Maven

I have included JMeter plugin by lazycode in my application.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>2.1.0</version>
<executions>
<execution>
<id>jmeter-tests</id>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
This runs when I use mvn verify. But, it runs all integration tests and unit tests too. I don't want this to happen. I want to run just mvn jmeter:jmeter and run performance tests.
If I run mvn jmeter:jmeter, I get
No plugin found for prefix 'jmeter' in the current project and in the plugin groups [org.sonarsource.scanner.maven, org.apache.maven.plugins, org.codehaus.mojo] available from the repositories
I don't want to configure global .m2/settings.xml. How to run it using Maven?
You are almost there, the correct Maven command to run only JMeter tests would be:
mvn jmeter:jmeter -Pjmeter
References:
Maven - Introduction to Build Profiles
JMeter Maven Plugin
Five Ways To Launch a JMeter Test without Using the JMeter GUI

How to run a Maven enforcer rule before the Maven release prepare changes the pom.xml?

I've created a custom Maven enforcer rule. This rule will check the content of the <scm><connection> value to ensure that it points to the trunk or branches/* (i.e. not a tag).
This enforcer is configured in the pom.xml like that:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.3.1</version>
<dependencies>
<dependency>
<groupId>my.company</groupId>
<artifactId>maven-release-enforcer</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>enforce-release-check</id>
<goals>
<goal>enforce</goal>
</goals>
<phase>validate</phase>
<configuration>
<rules>
<releaseCheck implementation="my.company.maven.release.enforcer.MavenReleaseEnforcer"/>
</rules>
</configuration>
</execution>
</executions>
</plugin>
Unfortunately, when we use it with the Maven Release plugin, the latter plugin changes the content of the <scm><connection> value during its [enter link description here]prepare2 goal, so before the enforcer is effectively called. This results in a failure of my custom rule, as the <scm><connection> points to a tag at this time.
So my question: is there a way to force the enforcer to be called before the Maven Release plugin start to modify the pom.xml?
ps: the Jenkins job is divided into 2 steps: mvn clean release:prepare and mvn release:perform.
when you call
mvn clean release:prepare
only the clean phase and the prepare goal is excuted.
You could use
mvn clean validate release:prepare
to include the validate phase or
mvn clean maven-enforcer-plugin:enforce release:prepare
to just trigger the enforcer plugin

Starting Jetty before test phase in a Maven multi-module project

I have a WAR Module in a multi-module Maven project (let's say foo-web), which realises a web-service. Then I have a foo-cli, which implements a web service client and tests it in a couple of unit tests.
In order to make this working, I start Jetty before the test phase this way:
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>foo-web</artifactId>
<version>${project.version}</version>
<type>war</type>
<scope>test</scope>
</dependency>
...
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.5.v20120716</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<useTestScope>true</useTestScope>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>8080</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>process-test-classes</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
</executions>
</plugin>
This works perfectly well while I run 'mvn test' from within the foo-cli module (it even stops automatically, with no need to specify anything else). However, when I attempt to go to the upper level (foo) and issue 'mvn test' from there, i.e., I try to run all the tests for all the modules in the project, it fails with '404 - not found'. From the output, I can see that the overlay (the war dependency) seems to be totally ignored.
Thanks in advance for any help.
You should try moving your integration test to the top level project. This way it will run after the WAR artifact has been built.
Have you had a look at the Maven Failsafe Plugin? It's designed for the sort of thing you're doing, which is actually an integration test and not a unit test. The page offers some good advice on why you might want to use the integration-test phase for your integration testing.
Namely, it describes why you might want to do start-jetty during pre-integration-test and so on, so that you can tear it all down appropriately.

How do you create automated tests of a Maven plugin using JUnit?

I've got a (mostly) working plugin developed, but since its function is directly related to the project it processes, how do you develop unit and integration tests for the plugin. The best idea I've had is to create an integration test project for the plugin that uses the plugin during its lifecycle and has tests that report on the plugins success or failure in processing the data.
Anyone with better ideas?
You need to use the maven-plugin-testing-harness,
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
You derive your unit test classes from AbstractMojoTestCase.
You need to create a bare bones POM, usually in the src/test/resources folder.
<project>
<build>
<plugins>
<plugin>
<groupId>com.mydomain,mytools</groupId>
<artifactId>mytool-maven-plugin</artifactId>
<configuration>
<!-- Insert configuration settings here -->
</configuration>
<executions>
<execution>
<goals>
<goal>mygoal</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Use the AbstractMojoTest.lookupMojo(String,File) (or one of the other variations) to load the Mojo for a specific goal and execute it.
final File testPom = new File(PlexusTestCase.getBasedir(), "/target/test-classes/mytools-plugin-config.xml");
Mojo mojo = this.lookupMojo("mygoal", testPom);
// Insert assertions to validate that your plugin was initialised correctly
mojo.execute();
// Insert assertions to validate that your plugin behaved as expected
I created my a plugin of my own that you can refer to for clarification http://ldap-plugin.btmatthews.com,
If you'd like to see some real-world examples, the Terracotta Maven plugin (tc-maven-plugin) has some tests with it that you can peruse in the open source forge.
The plugin is at: http://forge.terracotta.org/releases/projects/tc-maven-plugin/
And the source is in svn at: http://svn.terracotta.org/svn/forge/projects/tc-maven-plugin/trunk/
And in that source you can find some actual Maven plugin tests at: src/test/java/org/terracotta/maven/plugins/tc/

Categories

Resources