Mock tests, Interview questions, Tutorials and Tech news
 
 

Archive

Posts Tagged ‘maven’

Maven Surefire for Integration Tests in JUnit

June 14th, 2011 Vinay 1 comment

Maven surefire plugin is used to run unit tests during test phase of build lifecycle . The reports are generated in .txt or .xml file. These files are generated at ${basedir}/target/surefire-reports

Surefire runs unit tests during build phase, not integration tests which are executed during package phase. But you can include integration tests also to be run

Below is the configuration for surefire plugin in your pom.xml

<profile>
<id>itest</id>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.report.version}</version>
<configuration>
<includes>
<include>**/*IntTest.java</include>
<include>**/*IntTests.java</include>
</includes>
</configuration>
</plugin>

</plugins>
</build>
</profile>

Categories: Programming / tutorials Tags: ,

Adding a definition in maven

December 29th, 2010 Vinay No comments

So you have a jar file which you would like to add in maven and not sure what the definition would be . Here is what you do

1. Go to http://mvnrepository.com/

2.  Search for file you want to add for eg c3p0

3. You might see couple of results matching your search.

4. Find the definition that matches your need  for eg in this case

<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1</version>
</dependency>

Categories: Programming / tutorials Tags:

Configuring c3p0 connection pool with maven

December 29th, 2010 Vinay No comments

Let us say you want to configure c3p0 connection pool in your application through maven.

Here is the definition you need to add in pom.xml

<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1</version>
</dependency>

If you would like to see  how to configure c3po connection pool in Spring see this post Using connection pool with MYSQL in Spring applicationContext.xml

Duplicate class warnings when using shade plugin

December 24th, 2010 Vinay No comments

In the last post, I had talked about Building an executable jar with Maven

The same can also be achieved using Shade plugin. Below are the changes you will do to build an executable jar with Maven’s shade plugin.

If you are using a shade plugin

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation=”org.apache.maven.plugins.shade.resource.ManifestResourceTransformer”>
<mainClass>com.your.executable.class.name</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

Then you might see some warnings like

[WARNING] We have a duplicate net/sf/cglib/util/StringSwitcher$StringSwitcherKey.class in C:\dev\repository\cglib\cglib-full\2.0.2\cglib-full-2.0.2.jar

What is fix for this ?

Use assembly plugin as described in here. Building executable jar with maven

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation=”org.apache.maven.plugins.shade.resource.ManifestResourceTransformer”>
<mainClass>com.fmcna.cmd.digicomm.DigicommController</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

Categories: Programming / tutorials Tags:

Building an executable jar with Maven

December 23rd, 2010 Vinay 6 comments

This is one of the most commonly asked questions on Maven.

How to build executable jar with maven.

The idea is to build a jar with all its dependencies so that you can execute java – <nameof jarfile.jar>

Here is how to do it in maven. Add this to your pom.xml

<build>
<plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>fully.qualified.nameOfCLass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

You can run mvn assembly:assembly and a jar file with all the dependencies will be built.

This is good if this is a standalone project. But if you have a parent pom and there is sub project which needs an executable jar, use goal single to build it along with the rest of the application.

Here is the pom.xml for this
Read more…

Categories: Programming / tutorials Tags:

Integrating selenium with maven

September 25th, 2010 Vinay 1 comment

In the last post , we talked about Unit testing with Selenium. In this post , we will talk about how to integrate selenium with Maven and run your unit tests with maven. It assumes that you are already familiar with Maven.

Add the following dependency and plugin into your pom.xml

<dependency>

<groupId>org.seleniumhq.selenium.client-drivers</groupId>

<artifactId>selenium-java-client-driver</artifactId>

<version>1.0.1</version>

<scope>test</scope>

</dependency>

<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>selenium-maven-plugin</artifactId>

<executions>

<execution>

<phase>pre-integration-test</phase>

<goals>

<goal>start-server</goal>

</goals>

<configuration>

<background>true</background>

</configuration>

</execution>

</executions>

</plugin>

<plugin>

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

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

<configuration>

<skip>true</skip>

</configuration>

<executions>

<execution>

<id>integration-test</id>

<phase>integration-test</phase>

<goals>

<goal>test</goal>

</goals>

<configuration>

<skip>false</skip>

<excludes>

<exclude>none</exclude>

</excludes>

<includes>

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

</includes>

</configuration>

</execution>

</executions>

</plugin>

Keep all you tests into web-app/src/test/java folder.

Setup classpath :

Add selenium-java-client-driver.jar, selenium-server.jar and junit.jar in the classpath.

Run tests:

Execute the test from command line with the command – mvn install

Debug Maven tests running inside eclipse

March 1st, 2010 Vinay 2 comments

For those of us who have been running JUnit tests inside eclipse , one of the biggest feature available to us was the debug ability of the JUnit tests.
When we started working on Maven tests , the first questions which came to mind was , How am I going to debug it ?
I started with running the tests class by selecting the class in eclipse and right –> Run as Junit Test.

But this does not work. There are some variables which are being injected through pom.xml and tons of other dependency bugs which would be thrown.

So here is how to run your units tests in debug mode while running through mvn command.  There are two steps to this. You will have to first set up a remote launcher, similar to the way you might have done for remote server.

Then you will run the tests from command prompt or

Create Remote Java Application Debug Configurations Read more…

Categories: Programming / tutorials Tags: ,
Get Adobe Flash playerPlugin by wpburn.com wordpress themes