Create and run a maven project

In the previous post, we have learnt about the pom.xml. In this post, we will create a project and execute it through maven commands.

We have already seen how to install Maven, creating maven project and importing into eclipse. We have also seen POM.xml generated by maven. Now, let’s see a detailed tutorial how to create and run a maven project step by step.

Step# 1 Create a maven project as shown in this post and import it into eclipse.

Step# 2 You will be able to see a folder “src/test/java“. For testing purpose , we need to use this folder. We will create the packages and classes (or say test cases) inside this. Let’s create a package named as “com.learnmvn“. After that, we have created 2 classes inside the same package.

Class1: LoginClassTest

package com.learnmvn;

import org.testng.SkipException;
import org.testng.annotations.Test;


public class LoginClassTest {
  
	@Test
	public void firstTest(){
		System.out.println("first test - enter username AND Password");
		System.out.println("first test - click on Login button");
		
	}
	
	@Test
	public void secondTest(){
		System.out.println("second test - Logout");
		throw new SkipException("skipping the test");
	}
	

}

Class 2: ChangeNameTest

package com.learnmvn;

import org.testng.annotations.Test;

public class ChangeNameTest {

	@Test
	public void FirstNameTest()
	{
		System.out.println("First name ");
	}
	
	@Test
	public void LastNameTest()
	{
		System.out.println("Last name ");
	}
}

Step# 3 Create a package “com.resources” and under that create a testng.xml file where we will add both the classes.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
  
<suite name="My Suite Maven" verbose="1" >

  <test name="My Test">
    <classes>
      <class name="com.learnmvn.ChangeName"></class>
      <class name="com.learnmvn.LoginClass"></class>
    </classes>
  </test>
</suite>

The whole project will look like this.

 

Maven Project

Step# 4 Now, we need to configure pom.xml file. You will find by default junit dependency added into that. You can remove this dependency as we are running test cases with testNG and add the dependency for testNG as given below(You can change version according to requirement).

    <dependency>
 	 <groupId>org.testng</groupId>
  	 <artifactId>testng</artifactId>
  	 <version>6.8.8</version>
     <scope>test</scope>
    </dependency>

Step# 5 We will create the surefire report via this project. For that , we need to add the sure fire plugin into POM.xml.

    

    <plugins> 
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-report-plugin</artifactId>
                <version>2.18.1</version>
              <configuration>
		                 <suiteXmlFiles>
						    <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
						</suiteXmlFiles>
				            <includes>
				              <include>**/Test*.java</include>
				              <include>**/*Tests*.java</include>
				              <include>**/*Tests.java</include>
				              <include>**/*Test.java</include>
				              <include>**/*TestCase.java</include>
				            </includes>
		                    <!-- Skip the normal tests, we'll run them in the integration-test phase -->
		                    <skip>true</skip>
		                    
		          <linkXRef>false</linkXRef>  
		      </configuration>
            </plugin>	
    
            </plugins>

Step# 6 As you can observe includes tags in the above code, we have defined some formats like **Test* and *Test etc. That means surefire will recognize the test cases which is having Keyword Test into its test case name i.e. LoginTest etc.

Step# 7 We can add the plugin for reporting as given below.

		  <reporting>
		    <plugins>
		      <plugin>
		        <groupId>org.apache.maven.plugins</groupId>
		        <artifactId>maven-surefire-report-plugin</artifactId>
		        <version>2.18.1</version>
		      </plugin>
		    </plugins>
		  </reporting>

Step# 8 Finally the whole POM.xml file will look like as given below.

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.learnmaven</groupId>
  <artifactId>FirstMavenProj</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>FirstMavenProj</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
		
    <dependency>
 	 <groupId>org.testng</groupId>
  	 <artifactId>testng</artifactId>
  	 <version>6.8.8</version>
     <scope>test</scope>
    </dependency>

  </dependencies>
  		  <build>

        <plugins> 
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-report-plugin</artifactId>
                <version>2.18.1</version>
              <configuration>
		                 <suiteXmlFiles>
						    <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
						</suiteXmlFiles>
				            <includes>
				              <include>**/Test*.java</include>
				              <include>**/*Tests*.java</include>
				              <include>**/*Tests.java</include>
				              <include>**/*Test.java</include>
				              <include>**/*TestCase.java</include>
				            </includes>
		                    <skip>true</skip>
		                    
		          <linkXRef>false</linkXRef>  
		      </configuration>
            </plugin>	
    
            </plugins>
    </build>
    
		  <reporting>
		    <plugins>
		      <plugin>
		        <groupId>org.apache.maven.plugins</groupId>
		        <artifactId>maven-surefire-report-plugin</artifactId>
		        <version>2.18.1</version>
		      </plugin>
		    </plugins>
		  </reporting>

     <pluginRepositories>
    <pluginRepository>
      <id>reporty-ng</id>
      <url>https://github.com/cosminaru/reporty-ng/raw/master/dist/maven</url>
    </pluginRepository>
  </pluginRepositories>
</project>

Step# 9 Now, when everything is set up so we can run the test cases. For that open a command window by going into start menu. Then navigate to project path by using the CD command.

cd c:\sample\FirstMavenProj

Step# 10  There are few commands which does the following.

  • mvn clean : It will clean the target folder under the project.
  • mvn test: It will run the test cases.
  • mvn site: It will run test cases as well as generate the surefire report under a newly create folder named as site.
  • mvn surefire-report:report  – You can generate the report by using this command as well.

We can also run the commands altogether i.e. mvn clean site.

After running the command, you should get the “Build Success” message that means everything is working fine. You will also see the test case running as given below.

Maven output

Step# 11 Now, under the folder “target –> site ” , you can see surefire-report.html. It will have all the details about the test case executed.

Maven report

Maven report


Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum

 

Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...