Categories: testing

TestNG enabled example

In this post we will learn how to disable or ignore a test using @Test(enabled=false). Occasionally, we want to disable some tests because of some reasons(like a particular api being tested in those tests is changing but we don’t want to stop the complete CI build which apart from this API should be working fine). In those situations, we can simply ignore/disable those tests by setting enabled attribute of @Test annotation to false. Let’s begin.


Let’s take a simple Calculator class to demonstrate this feature.

package com.websystique.testng;

public class Calculator {

 public double add(double a, double b){
  return a+b;
 }
 
 public double subtract(double a, double b){
  return a-b;
 }
 
}

Let’s write a trivial test class with two test methods add & subtract. Let’s assume that subtract api is broken and we don’t want to execute subtract test method for the moment.

package com.websystique.testng;

import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestNGEnabledExample {

 Calculator calculator;

 @BeforeClass
 public void setup() {
  System.out.println("setup()");
  calculator = new Calculator();
 }

 @AfterClass
 public void tearDown() {
  System.out.println("tearDown()");
  calculator = null;
 }

 @BeforeMethod
 public void beforeMethod() {
  System.out.println("beforeMethod()");
 }

 @AfterMethod
 public void afterMethod() {
  System.out.println("afterMethod()");
 }

 @Test
 public void testAdd() {
  System.out.println("testAdd()");
  Assert.assertEquals(calculator.add(3, 4), 7.0);
 }

 @Test(enabled = false)
 public void testSubtract() {//We are disabling this test. Look at enabled=false with @Test
  System.out.println("testSubtract()");
  Assert.assertEquals(calculator.subtract(5, 2), 3.0);
 }

}

We have also included the @BeforeClass & @BeforeMethod to see the overall effect.

Run above test class using TestNG Eclipse plugin or maven (mvn clean test). Following is the outcome:

setup()
beforeMethod()
testAdd()
afterMethod()
tearDown()
PASSED: testAdd

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================

You can see that we have disabled the subtract test method by using @Test(enabled=false). So that subtract Test method wasn’t executed.

Additional noticeable points are :

  • @BeforeClass/@AfterClass was called as there is at least one @Test method in this class which got executed.
  • @BeforeMethod/@AfterMethod was called only for the @Test method which got executed. For the @Test which was disabled, no @BeforeMethod/@AfterMethod was called.

That’s it.

References

View Comments

  • can you please explain how to test controller and service layer code together using TestNg and Powermock with example

Share
Published by

Recent Posts

Spring Boot + AngularJS + Spring Data + JPA CRUD App Example

In this post we will be developing a full-blown CRUD application using Spring Boot, AngularJS, Spring Data, JPA/Hibernate and MySQL,…

8 years ago

Spring Boot Rest API Example

Spring Boot complements Spring REST support by providing default dependencies/converters out of the box. Writing RESTful services in Spring Boot…

8 years ago

Spring Boot WAR deployment example

Being able to start the application as standalone jar is great, but sometimes it might not be possible to run…

8 years ago

Spring Boot Introduction + hello world example

Spring framework has taken the software development industry by storm. Dependency Injection, rock solid MVC framework, Transaction management, messaging support,…

8 years ago

Secure Spring REST API using OAuth2

Let's secure our Spring REST API using OAuth2 this time, a simple guide showing what is required to secure a…

8 years ago

AngularJS+Spring Security using Basic Authentication

This post shows how an AngularJS application can consume a REST API which is secured with Basic authentication using Spring…

8 years ago