Automation with Maven + TestNG + Java

Abhijit Valvekar
The Software Automation
5 min readDec 11, 2019

--

The QA has now become a developer. Definition of the QA has been changed ,isn’t it? QA Engineer is now equally responsible to write the parallel wrapper on top of the developers code to get the good tests coverage. It could be in any form of automation like front end , backend, database or anything…

Automation engineer has to put a cap of architect to decide about the tools, technologies ,languages , integrations and other so many things to produce reliable automation framework.

Content –
1.Pre-requisites
2.Lets create the sample project
3.About
A. Maven
B. TestNG
4.Define POM file
5.Define TestNG suite file
6.Its time to code
7.Here we go .. Execution
8.Reports
9.Get the project from Github

A.Java 1.8 +1.Installation – if you are on Unix system fire the below commands on terminalsudo apt updatesudo apt install openjdk-8-jdk2.Windows – Simply download the installer and install ithttps://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htmlB.Maven 3.41.Installation – if you are on Unix system fire the below command on terminalsudo apt install maven2.Windows –Download the zip file from below location -> store it on your location like “D:\Software\maven”http://apachemirror.wuchna.com/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.zip3.Set Environment VariablesC.Your favorite IDE – I will choose eclipse
  1. Eclipse

2. Command Line

Your project needs to reside somewhere , create the directory and fire the below command.

mvn archetype:generate -DgroupId=com.av.integration.tests -DartifactId=com.av.integration.tests -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Folder Structure

A. Maven

Maven is an influential Automation and management tool which helps to build and Manage java project in most relaxed way. It is based on the POM(project object model). Ex — In old school days, developers were suppose to download the essential dependencies i.e. Jars and then they were needed to be configured in a classpath. Now , the tools like Maven or Gradle makes the life happier.

Maven Repository helps to store the jars , plugins or artifacts. There are three types of repositories
1. Local — .m2 repo at your home directory. It gets created , once the maven project gets build. Maven prefers local repo at every execution , its goes to central or remote if the artifact is new.
2. Central — Maven community manages its own repository where it contains commonly used library files.
3. Remote –This will be managed by company. Companies , sometimes maintains the library on their own server
Good read — https://maven.apache.org/guides/getting-started/

B. TestNG

TestNG is a testing framework where it helps to manage your test cases.

•In Brief :

1.We can bucket the test cases into sanity , regression , bvt, integration-test etc..

2. Run your tests in multithread pools[reduce build time execution]

3. Powerful annotations helps to make stronger framework.

4. DataDriven Testing with the help of @Dataprovider annotation

•Useful Annotations

1.@BeforeSuite: The annotated method will be run before all tests in this suite have run.

2.@AfterSuite: The annotated method will be run after all tests in this suite have run.

3.@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.

4.@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.

5.@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.

6.@AfterClass: The annotated method will be run after all the test methods in the current class have been run.

7.@BeforeMethod: The annotated method will be run before each test method.

8.@AfterMethod: The annotated method will be run after each test method.

For comprehensive study: https://testng.org/doc/documentation-main.html#introduction

Create TestClass under src/test/java/com/av/integration/test/TestClass.java

Below code Snippet contains

  • Simple TestNG test case execution
  • Groups of Test cases example
  • Depends on Method example
  • parameterization with the dataprovider — String , int and List examples
  1. Fire the below command to execute the test groups

mvn clean install -Dgroups=sanity

There are 3 test cases are tagged with sanity hence it will execute only 3 test cases.

mvn clean install -Dgroups=regression

This will results all test cases execution i.e. 6

2. Make the below changes to execute the TestNG suite file

uncommen the suiteXmlFiles tags and comment out groups line from pom.xml file.

<suiteXmlFiles>
<suiteXmlFile>${suite}</suiteXmlFile>
</suiteXmlFiles>

// <groups>${groups}</groups>

Now, Run the following command

Reports Directory — <<Integration-test>>/target/surefire-report/emailable-report.html

  1. You can also get the project by executing following command

git clone git@github.com:abhi1231/Integration-test.git

OR

Download the project with following steps -

a. Go to https://github.com/abhi1231/Integration-test

b. Click on Clone or Download → Download zip .

2. Go to the directory and execute the command mentioned in chapter #7

mvn clean install -Dgroups=regression

--

--