TestNG Method with Return Statement

How to use Return in TestNG Methods: We have seen in Java that a method can return some value after processing the data; similarly, here we will try to learn whether a test method can return value in a testNG class.

package com.softwaretestingo.testng;
import org.testng.annotations.Test;
public class TestMethodWithReturnStatement 
{
	// A @Test method which is returning a value will be ignored by TestNG
	@Test
	public String returnMethod()
	{
		System.out.println("Returning Method");
		return "SoftwareTestingo";
	}

	@Test
	public void normalMethod()
	{
		System.out.println("Normal Method");
	}
}

When we execute the above testng class, in the output, you can see in tests, run showing one whereas we have two methods. That means if we use the return statement inside a testNG method, then that method is skipped by testNG.

Output:

Normal Method
PASSED: com.softwaretestingo.testng.TestMethodWithReturnStatement.normalMethod

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


===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

How to Call Other Test Method?

Similarly, we will learn another new thing: can we call a test method from another test method?

In response to the above question, we can call a test method from another test method.

package com.softwaretestingo.testng;
import org.testng.annotations.Test;
public class CallOtherTestMethod 
{
	// A @Test method which is returning a value will be ignored by TestNG
	@Test
	public String returnMethod()
	{
		System.out.println("Returning Method");
		return "SoftwareTestingo";
	}

	@Test
	public void normalMethod()
	{
		System.out.println("Normal Method");
		System.out.println(returnMethod());
	}
}

As we all know, TestNG is mainly used for unit testing, and a unit test method should not return a value. That is why when a method returns some value, that method is ignored by the testNG, which is the default behavior of TestNG.

But we can force TestNG to add those methods in the execution that return values by mentioning allow-return-values as true at the suite level.

Here is the testng.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
	<test thread-count="5" name="Test">
		<classes>
			<class name="com.softwaretestingo.testng.CallOtherTestMethod" />
		</classes>
	</test> <!--
	Test -->
</suite> <!--
Suite -->

Output:

Normal Method
Returning Method
SoftwareTestingo
PASSED: com.softwaretestingo.testng.CallOtherTestMethod.normalMethod

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


===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

Note: We can force TestNG to change the behavior, but the result displays the return value.

Where can we use this concept?

TestNG is a testing framework that both developers and testers can use. This concept can be used when inheriting an interface or abstract class and testing its methods without changing method signatures.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment