EasyTest : Understanding Property Editor Support

anujgandharv edited this page Nov 7, 2012 · 1 revision

EasyTest tries to make the life of its user as simple as it is possible. A step in that direction was the support of Property Editors in EasyTest to pass input test data to the methods. If you want you can look at more details of how PropertyEditors work here : http://docs.oracle.com/javase/6/docs/api/java/beans/PropertyEditorManager.html

EasyTest supports the same mechanism that Java PropertyEditorManager supports. Lets try to understand it with an example. Lets assume we have the following test method :

       @RunWith(DataDrivenTestRunner.class)
       @DataLoader(filePaths={"xmlBasedData.xml"} , loaderType= LoaderType.XML)
       public class EasyTestPropertyEditorExample {

         private ItemService testSubject;

         @Before
         public void setUp() {
             testSubject = new MockItemService();
         }

         @Test
         public void getItemsData(@Param ItemId itemId){
            Item searchedItem = testSubject.findItem(itemId);
            Assert.assertNotNull(searchedItem);
            Assert.assertEquals(itemId, searchedItem.getItemId());
      }

You might have noticed that the input parameter to a method is ItemId, which is not a standard Java Type. Instead its a user defined object.

Now, EasyTest on its own cannot figure out how to convert a simple String value that the user might have provided in his XML file to an ItemId object. So it tries certain things before failing with an error of something like: "Test data was not found for the test method with the given parameter...."

It works with java's Property Editors to figure out if there is any Editor associated with the given Type. In this case, EasyTest will look into Java PropertyEditor support to find out if there is an editor registerd for the class ItemId. It will use the same mechanism as described in the PropertyEditorManager's java doc : http://docs.oracle.com/javase/6/docs/api/java/beans/PropertyEditor.html

If the Editor is found, EasyTest will convert the Data from String to the given type(in this case to ItemId). In case it is not found, it will throw an error.

Obviously, the software development is not that straight forward and sometimes you encounter situations where you were not able to follow Java's Property Editor mechanism. in that case you can register your PropertyEditors in your test class and EasyTest Framework will make sure that they are avaialble and are used.

You can register your PropertyEditors inside the method annotated with @BeforeClass annotation like this:

       @BeforeClass
       public static void setup(){
             PropertyEditorManager.registerEditor(ItemId.class, ItemIdEditor.class);
   
      }

And thats it. Thus you have now more reasons to work with EasyTest.

Happy EasyTest(ing)