Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

EasyTest : Understanding @Param annotation

Anuj Kumar edited this page · 5 revisions

In accordance with version 0.8 of easytest core module.

EasyTest provides its users with the facility to do Data Driven Testing in Java and uses JUnit classes behind the scene to achieve its objective.

One of the major components of EasyTest is the @Param annotation. Users make use of the @Param annotation on the method parameters. Here is a simple example depicting the use of @Param annotation in a simple Test case.

@RunWith(DataDrivenTestRunner.class)
@DataLoader(filePaths={testData.xls})
 public class TestClass{

 @Test
 public void simplTestMethod(@Param(name="name")String paramName){
   ...............//your test conditions here
 }

}

As you can see, we have marked our input parameter with the @Param annotation. This marking provides the EasyTest framework with the clear information that the value for the parameter "paramName" should be provided by the EasyTest Framework.

Let's see how it works behind the scenes.

When EasyTest Framework starts the execution of test methods in a Test class, it first loads the Test Data that would be required by the test methods in that particular class. Thus, in our example above, EasyTest framework will read the input test data from testData.xls file and will keep it in memory, in a data structure that is understandable by the EasyTest Framework.

Next, when a particular test method needs to be executed, EasyTest Framework looks at the method's parameters. It can process two types of test methods:

  • Test methods with NO Parameters at all , or
  • Test methods with Parameters

In case of no parameters, it is executed just like any other JUnit test case. In case test method parameters have @Param annotation, the following algo is applied.

  • EasyTest will look at the name attribute of the @Param annotation.
  • If the name attribute is present, EasyTest will try to get the parameter value for tha given name from the test data that it loaded in its in memory data structure.
  • If the value for the given param name is found it is processed to be supplied to the test method.
  • If the value for the given param name is NOT found, then ParamAssertionError is thrown to its users.
  • If the @Param annotation is NOT present, then EasyTEst framework takes the name of the class of the Paramater and try to find a value in its data structure.
  • If the value is found, it is passed to the Test method.
  • If the value is not found, a ParamAssertionError is thrown.

Note that it is useful to omit the @Param annotation alltogether in cases where you are passing your own custom objects(ItemId , Item , LibraryId etc) instead of objects that are standard Java Types(String , int, float etc).

Heres an example of a test method that omits the @Param annotation :

public void testGetItems(ItemId itemId){
    System.out.println("ItemId is :" + itemId.toString());
    Item item = testSubject.findItem(itemId);
    Assert.assertNotNull(item);
    Assert.assertEquals(item.getItemId(), itemId);

}

In the above scenario, EasyTest will try to find the value for the parameter with name "ItemId". If found the value is supplied. If not, an Error is thrown. Moreover, you now don't have to supply any editor in case the object as parameter has a constructor that takes standard Java Objects (Long,Float,Short,Integer,Byte,Character,String etc) as input parameter. Easytest will automatically instantiate the object for you.

I hope I am able to convey the essence of @Param annotation. If you have any queries or clarifications, please do not hesitate to write to anujkumar@easetech.org or open an issue here : https://github.com/EaseTech/easytest/issues?state=open

Happy EasyTest(ing)

Something went wrong with that request. Please try again.