Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use function parameters if function have @Test annotation?

Tags:

java

testng

Is there any way to use parameters in a function if the function has @test annotation. I have a function like below:

@Test(@Test(priority=1, alwaysRun =true))
public void Home_page_Flextronics(String sUserName, String sPassword) throws FileNotFoundException 
    {
        CommonFunctions.LaunchApplication();            
        CommonFunctions.Login(sUserName, sPassword);    
        CommonFunctions.ClickOnModule("Customers"); 
        CommonFunctions.ClickOnHome();
        CommonFunctions.Logout();       
    }

However when I am trying to run above code its giving me error:

Method Home_page_Flextronics requires 2 parameters but 0 were supplied in the @Test annotation.

If I remove the parameters and use hardcoded values, its working fine and it is requirement of my framework. I have gone through other solutions mostly suggest to use @Parameter annotation or data provider. But I don't want to use that as I want to take testdata from excel sheet. Please let me know if there is any other way present to handle this. Thanks for your help in advance.

like image 711
shubham pandey Avatar asked May 10 '26 15:05

shubham pandey


1 Answers

You could take a look at Junit Theories (introduction)

import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertTrue;

@RunWith(Theories.class)
public class AdditionWithTheoriesTest {

  @DataPoints
  public static int[] positiveIntegers() {
       return new int[]{
                        1, 10, 1234567};
  }

  @Theory
  public void a_plus_b_is_greater_than_a_and_greater_than_b(Integer a, Integer b) {
      assertTrue(a + b > a);
      assertTrue(a + b > b);
  }
}

This way you can pass parameters to a test. In your method annotated with DataPoints you'll need to fetch all the excell data and return it.

like image 118
Tom Jonckheere Avatar answered May 12 '26 05:05

Tom Jonckheere