TestNG's DataProvider:

 Providing test data(parameters) to the test cases methods are the crucial part in unit testing, usally it can be provided with the help of testng.xml file,which does not scale and  java annotation as @Parameters({"first-name"}) in below the test case methods, But does not scale when you have a large test data. Suggested solution might be to provide the data at the run-time, while execution of the test methods using TestNG's Parameters with DataProvider or Factory.

Parameters with DataProvider:

Specifying parameters in testng.xml might not be sufficient in the following cases:

  • You are not using a testng.xml.
  • You need to pass complex parameters, or parameters that need to be created from Java (complex objects, objects read from a property file or a database, etc...).
The Data Provider method can return one of the following two types:
  • An array of array of objects (Object[][]) where the first dimension's size is the number of times the test method will be invoked and the second dimension size contains an array of objects that must be compatible with the parameter types of the test method. This is the cast illustrated by the example above.
  • An Iterator<Object[]>. The only difference with Object[][] is that an Iterator lets you create your test data lazily. TestNG will invoke the iterator and then the test method with the parameters returned by this iterator one by one. This is particularly useful if you have a lot of parameter sets to pass to the method and you don't want to create all of them upfront.
You can use any of them to provide the test data in unit test cases.

Code Sample:

   public class MyI18nTestCase
        extends CoreTestCase {

    private static final String UNITTEST_BUNDLE_NAME = "org.myserver.core.i18n";
    
    @Configuration(beforeTestClass = true)
    public void setUp() {
    // code that will be invoked when this test is instantiated
    }

    @DataProvider(name = "localedata")
    private static Object[][] createLocaleData() throws Exception {

        Object[][] data = new Object[][]{
            {new Locale("de", "DE").toString(), ResourceBundle.getBundle(MyI18nTestCase.UNITTEST_BUNDLE_NAME)},
            {new Locale("fr", "FR").toString(), ResourceBundle.getBundle(MyI18nTestCase.UNITTEST_BUNDLE_NAME)},
            {new Locale("es", "ES").toString(), ResourceBundle.getBundle(MyI18nTestCase.UNITTEST_BUNDLE_NAME)},
            {new Locale("ja", "JP").toString(), ResourceBundle.getBundle(MyI18nTestCase.UNITTEST_BUNDLE_NAME)},
            {new Locale("ko", "KR").toString(), ResourceBundle.getBundle(MyI18nTestCase.UNITTEST_BUNDLE_NAME)},
            {new Locale("zh", "CN").toString(), ResourceBundle.getBundle(MyI18nTestCase.UNITTEST_BUNDLE_NAME)},
            {new Locale("zh", "TW").toString(), ResourceBundle.getBundle(MyI18nTestCase.UNITTEST_BUNDLE_NAME)}
        };
       
        return data;
    }


    @Test(dataProvider = "localedata", groups = {"i18n-group"})
    public void testMyi18nTestcase(String locale, ResourceBundle bundle) throws Exception {
      System.out.println("It works for me in locale:" + locale + "and bundled key-value pairs " + bundle.getString("first.name"));
    }
}

//File i18n_en_US.properties under the directory org/myserver/core
first.name=first name
// File i18n_es_ES.properties under the directory org/myserver/core
first.name=nombre
// File i18n_de_DE.properties under the directory org/myserver/core
first.name=Vorname
// File i18n_fr_FR.properties under the directory org/myserver/core
first.name=pr\u00e9nom
// File i18n_ja_JP.properties under the directory org/myserver/core
first.name=\u540d\u524d
// File i18n_ko_KR.properties under the directory org/myserver/core
first.name=\uc774\ub984
// File i18n_zh_TW.properties under the directory org/myserver/core
first.name=\u540d\u5b57
// File i18n_zh_CN.properties under the directory org/myserver/core
first.name=\u540d\u5b57

References:

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed

This blog copyright 2009 by shankar