Springboot and TestNG

In the market there are lot of test automation tutorials which describe the standard framework involving Selenium Webdriver for Java, TestNG & Maven. And the most widely used design pattern is Page Object Model (POM). In this blog, we will see an additional framework ‘Spring Boot’ which is used to develop production grade application in short amount of time.

I will try to explain the framework from s/w test perspective. The details of Spring Boot is not in the scope of the blog.

Let’s start..

Spring Boot is an open source framework which provides Java developers with a platform to start with an auto configurable production-grade Spring application. How will it be useful for the Automation Test Specialists?

In most the standard test automation framework, properties files are used :

  • to store the server urls
  • browser details (if you want to run the test cases using chrome or firefox etc)
  • used to store the xpaths
  • internationalization 

To read the properties file, we have to write certain lines of code involving Java file class. What if there is a way to read the properties file without explicitly using Java file class? Spring Boot enables us to do exactly that so that we test automation specialist can focus on writing more test cases quickly. 

Lets see another example :

Log4j is widely used for logging by developer and test specialists. Usage of this involves in downloading the jar, creating corresponding properties file or xml file. Won’t be it good to have the feature by default if you create an automation project? Spring Boot enables this feature by default.

 Step by step guide to create test automation framework with the help of Spring Boot.

Developing Your First Spring Boot Application

The simplest way to a create Spring Boot project is to create it using Spring Initializr.

  • Go to Spring Initializr.
  • Select Project -> Maven Project
  • Language -> Java
  • Spring Boot -> The latest version available (its 2.2.5 now)
  • Enter Project Metadata: Group, Artifact
  • Click ‘Generate – Ctrl + Enter button’ to download the project
  • The project will be downloaded with the artifact name. In our example its ‘firstautomation’
  • Unzip the file
  • Import the project as a Maven project to Eclipse (Version: Neon.3 Release (4.6.3) is used for the tutorial)
  • Once the project is imported, Build the project. It will look like the following screenshot
  • Go to src/main/java and check for the FirstautomationApplication.java file. Just make its available. We will not touch it unless required.

Create a simple test case with TestNG

Now our Spring Application is ready. It’s time to create a test cases with the help of TestNG and run it.

  • Add TestNG dependency in pom.xml file and build the project
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.5</version>
</dependency>
  • Once the TestNG dependency is added, create a test case
  • Create a TestNG Test Case say FirstTestCase.java and make sure that FirstTestCase class extends AbstractTestNGSpringContextTests abstract class.
  • Create a test case as below and run the test case.
@Test
public void firstTest() {
Assert.assertTrue(4==(2+2), "4 is not equal to 2+2"); 
}
  • The test will be executed successfully

Complete test case code

package base.firstautomation;
 
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.Test;
 
public class FirstTestCase extends AbstractTestNGSpringContextTests {
  @Test
  public void firstTest() {
  Assert.assertTrue(4==(2+2), "4 is not equal to 2+2");
  }
}

About This Site

The main aim of this site is to share knowledge with fellow software test specialists who are keen to grow both technically and professionally.

Categories