Showing posts with label maven. Show all posts
Showing posts with label maven. Show all posts

Monday, 13 August 2018

Apache Maven Build Tool Learnings - Java

How to seperate unit and integration test?

- maven-surefire-plugin : designed to handle unit test(Junit)
- maven-failsafe-plugin : designed to handle integration tests


           Reference Blog for complete information
           How to separate integration test from unit test




Following is the process to run Unit and Integration Tests separately

1) Under properties section of the pom.xml file, define following properties
<skipTests>false</skipTests>
<skipITs>${skipTests}</skipITs>
<skipUTs>${skipTests}</skipUTs>

2) Under build section add maven-failsafe-plugin(for integration tests) and maven-surefire-plugin(for unit tests) as follows

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.13</version>
<configuration>

<skipTests>${skipTests}</skipTests>
<skipITs>${skipITs}</skipITs>
</configuration>
<executions>
<execution>
<id>failsafe-integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<trimStackTrace>false</trimStackTrace>
<skipTests>${skipUTs}</skipTests>
</configuration>
</plugin>

3) Below are the commands to skip/run the tests seperately

i) mvn install -DskipUTs : Skips Unit tests
ii)mvn install -DskipITs : Skips Integration tests
iii)mvn install -DskipTests : Skips both Unit and Integration Tests

4) We need to name the integration test as per maven-failsafe-plugin naming convensions for example, *IT.java




Following are the steps to skip tests by default but want the ability to re-enable tests from the command line in surefire plugin.

1) In the Properties section of the pom.xml need to add a property

<skipTests>true</skipTests>

2) Modify maven-surefire-plugin the plugin under build section as follows

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<configuration>

<trimStackTrace>false</trimStackTrace>

<includes>

<include>**/*Test.java</include>

</includes>

<excludes>

<exclude>**/*IntegrationTest.java</exclude>

<exclude>**/*IT.java</exclude>

</excludes>

<skipTests>${skipTests}</skipTests>

</configuration>

</plugin>

3) When we run , mvn clean install or mvn test, unit tests will not run by default

4) We can use "mvn install -DskipTests=false" to run the tests from the command line

Saturday, 11 August 2018

Read values from properties file in maven project- Java

If the file is placed under target/classes after compiling, then it is already in a directory that is part of the build path. The directory src/main/resources is the Maven default directory for such resources, and it is automatically placed to the build path by the Eclipse Maven plugin (M2E). So, there is no need to move your properties file.
The other topic is, how to retrieve such resources. Resources in the build path are automatically in the class path of the running Java program. Considering this, you should always load such resources with a class loader.

References : Stackoverflow


Example code:
String resourceName = "myconf.properties"; // could also be a constant
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Properties props = new Properties();
try(InputStream resourceStream = loader.getResourceAsStream(resourceName)) {
    props.load(resourceStream);
}
// use props here ...