Best Serenity JUnit code snippet using net.serenitybdd.junit.runners.DataDrivenAnnotations.forClass
Source:WhenFindingTestDataInADataDrivenTest.java
...41 final static class CSVDataDrivenTestScenario {}42 @Test43 public void the_parameterized_data_method_is_annotated_by_the_TestData_annotation() throws Exception {44 TestClass testClass = new TestClass(DataDrivenTestScenario.class);45 FrameworkMethod method = DataDrivenAnnotations.forClass(testClass).getTestDataMethod();46 assertThat(method.getName(), is("testData"));47 }48 @Test49 public void the_parameterized_data_method_returns_the_set_of_test_data() throws Throwable {50 TestClass testClass = new TestClass(DataDrivenTestScenario.class);51 DataTable testDataTable = DataDrivenAnnotations.forClass(testClass).getParametersTableFromTestDataAnnotation();52 assertThat(testDataTable.getRows().size(), is(3));53 }54 @Test55 public void testData_without_parameter_names_defines_default_parameter_names() throws Throwable {56 TestClass testClass = new TestClass(DataDrivenTestScenario.class);57 DataTable testDataTable = DataDrivenAnnotations.forClass(testClass).getParametersTableFromTestDataAnnotation();58 List<String> parameterNames = testDataTable.getHeaders();59 assertThat(parameterNames.size(), is(2));60 int i = 0;61 for (String parameterName : parameterNames) {62 assertThat(parameterName, is("Parameter " + (i+1)) );63 i++;64 }65 }66 @Test67 public void testData_with_parameter_names_uses_defined_parameter_names() throws Throwable {68 TestClass testClass = new TestClass(DataDrivenTestScenarioWithParamNames.class);69 DataTable testDataTable = DataDrivenAnnotations.forClass(testClass).getParametersTableFromTestDataAnnotation();70 List<String> parameterNames = testDataTable.getHeaders();71 assertThat(parameterNames.size(), is(2));72 assertThat(parameterNames.get(0), is("param-A"));73 assertThat(parameterNames.get(1), is("param-B"));74 }75 @Test76 public void should_be_able_to_count_the_number_of_data_entries() throws Throwable {77 TestClass testClass = new TestClass(CSVDataDrivenTestScenario.class);78 int dataEntries = DataDrivenAnnotations.forClass(testClass).countDataEntries();79 assertThat(dataEntries, is(12));80 }81 @Test82 public void should_be_able_to_get_data_Table_from_csv() throws Throwable {83 TestClass testClass = new TestClass(CSVDataDrivenTestScenario.class);84 DataTable testDataTable = DataDrivenAnnotations.forClass(testClass).getParametersTableFromTestDataSource();85 List<String> parameterNames = testDataTable.getHeaders();86 assertThat(parameterNames.size(), is(3));87 assertThat(parameterNames.get(0), is("NAME"));88 assertThat(parameterNames.get(1), is("AGE"));89 assertThat(parameterNames.get(2), is("ADDRESS"));90 }91 @Test92 public void should_be_able_to_count_the_number_of_data_entries_using_a_class_directory() throws Throwable {93 int dataEntries = DataDrivenAnnotations.forClass(CSVDataDrivenTestScenario.class).countDataEntries();94 assertThat(dataEntries, is(12));95 }96 @Test97 public void should_recognize_a_test_case_with_valid_test_data() {98 TestClass testClass = new TestClass(DataDrivenTestScenario.class);99 assertThat(DataDrivenAnnotations.forClass(testClass).hasTestDataDefined(), is(true));100 }101 final static class DataDrivenTestScenarioWithNoData {}102 @Test103 public void should_recognize_a_test_case_without_valid_test_data() {104 TestClass testClass = new TestClass(DataDrivenTestScenarioWithNoData.class);105 assertThat(DataDrivenAnnotations.forClass(testClass).hasTestDataDefined(), is(false));106 }107 @Test108 public void should_recognize_a_test_case_with_a_valid_test_data_source() {109 TestClass testClass = new TestClass(CSVDataDrivenTestScenario.class);110 assertThat(DataDrivenAnnotations.forClass(testClass).hasTestDataSourceDefined(), is(true));111 }112 @Test113 public void should_recognize_a_test_case_without_a_valid_test_data_source() {114 TestClass testClass = new TestClass(DataDrivenTestScenarioWithNoData.class);115 assertThat(DataDrivenAnnotations.forClass(testClass).hasTestDataSourceDefined(), is(false));116 }117 @Test118 public void should_load_test_class_instances_using_a_provided_test_data_source() throws IOException {119 TestClass testClass = new TestClass(CSVDataDrivenTestScenario.class);120 List<PersonTestScenario> testScenarios121 = DataDrivenAnnotations.forClass(testClass).getDataAsInstancesOf(PersonTestScenario.class);122 assertThat(testScenarios.size(), is(12));123 MatcherAssert.assertThat(testScenarios.get(0).getName(), is("Joe Smith"));124 MatcherAssert.assertThat(testScenarios.get(1).getName(), is("Jack Black"));125 }126 static class DataDrivenTestScenarioWithPrivateTestData {127 @TestData128 static Collection testData() {129 return Arrays.asList(new Object[][]{130 {"a", 1},131 {"b", 2},132 {"c", 3}133 });134 }135 }136 @Test(expected = IllegalArgumentException.class)137 public void the_parameterized_data_method_must_be_public() throws Exception {138 TestClass testClass = new TestClass(DataDrivenTestScenarioWithPrivateTestData.class);139 FrameworkMethod method = DataDrivenAnnotations.forClass(testClass).getTestDataMethod();140 assertThat(method.getName(), is("testData"));141 }142 static class DataDrivenTestScenarioWithNonStaticTestData {143 @TestData144 public Collection testData() {145 return Arrays.asList(new Object[][]{146 {"a", 1},147 {"b", 2},148 {"c", 3}149 });150 }151 }152 @Test(expected = IllegalArgumentException.class)153 public void the_parameterized_data_method_must_be_static() throws Exception {154 TestClass testClass = new TestClass(DataDrivenTestScenarioWithNonStaticTestData.class);155 FrameworkMethod method = DataDrivenAnnotations.forClass(testClass).getTestDataMethod();156 assertThat(method.getName(), is("testData"));157 }158 public class SimpleDataDrivenScenario {159 private String name;160 private String address;161 private String phone;162 }163 public class AnnotatedDataDrivenScenario {164 private String name;165 private String address;166 private String phone;167 @Qualifier168 public String getQualifier() {169 return name;170 }171 public String getName() {172 return name;173 }174 public void setName(String name) {175 this.name = name;176 }177 public String getAddress() {178 return address;179 }180 public void setAddress(String address) {181 this.address = address;182 }183 public String getPhone() {184 return phone;185 }186 public void setPhone(String phone) {187 this.phone = phone;188 }189 }190 @Test191 public void should_use_the_Qualifier_method_as_a_qualifier_if_present() {192 AnnotatedDataDrivenScenario testCase = new AnnotatedDataDrivenScenario();193 testCase.setName("Joe");194 String qualifier = QualifierFinder.forTestCase(testCase).getQualifier();195 assertThat(qualifier, is("Joe"));196 }197 public static class DataDrivenScenarioWithStaticQualifier {198 @Qualifier199 public static String qualifier() {200 return "QUALIFIER";201 }202 }203 @Test(expected = IllegalArgumentException.class)204 public void the_qualifier_method_must_not_be_static() {205 DataDrivenScenarioWithStaticQualifier testCase = new DataDrivenScenarioWithStaticQualifier();206 QualifierFinder.forTestCase(testCase).getQualifier();207 }208 public static class DataDrivenScenarioWithNonPublicQualifier {209 @Qualifier210 protected String qualifier() {211 return "QUALIFIER";212 }213 }214 @Test(expected = IllegalArgumentException.class)215 public void the_qualifier_method_must_be_public() {216 DataDrivenScenarioWithNonPublicQualifier testCase = new DataDrivenScenarioWithNonPublicQualifier();217 QualifierFinder.forTestCase(testCase).getQualifier();218 }219 public static class DataDrivenScenarioWithWronlyTypedQualifier {220 @Qualifier221 public int qualifier() {222 return 0;223 }224 }225 @Test(expected = IllegalArgumentException.class)226 public void the_qualifier_method_must_return_a_string() {227 DataDrivenScenarioWithWronlyTypedQualifier testCase = new DataDrivenScenarioWithWronlyTypedQualifier();228 QualifierFinder.forTestCase(testCase).getQualifier();229 }230 @UseTestDataFrom(value="test-data/simple-semicolon-data.csv", separator=';')231 final static class CSVDataDrivenTestScenarioUsingSemiColons {}232 @Test233 public void should_load_test_class_instances_using_semicolons() throws IOException {234 TestClass testClass = new TestClass(CSVDataDrivenTestScenarioUsingSemiColons.class);235 List<PersonTestScenario> testScenarios236 = DataDrivenAnnotations.forClass(testClass).getDataAsInstancesOf(PersonTestScenario.class);237 assertThat(testScenarios.size(), is(2));238 MatcherAssert.assertThat(testScenarios.get(0).getName(), is("Joe Smith"));239 MatcherAssert.assertThat(testScenarios.get(0).getAddress(), is("10 Main Street, Smithville"));240 MatcherAssert.assertThat(testScenarios.get(1).getName(), is("Jack Black"));241 MatcherAssert.assertThat(testScenarios.get(1).getAddress(), is("1 Main Street, Smithville"));242 }243 @UseTestDataFrom(value="test-data/simple-semicolon-data.csv,test-data/simple-semicolon-data.csv", separator=';')244 final static class CSVDataDrivenTestScenarioFromSeveralSourcesUsingSemiColons {}245 @Test246 public void should_load_test_class_instances_from_several_sources_using_semicolons() throws IOException {247 TestClass testClass = new TestClass(CSVDataDrivenTestScenarioFromSeveralSourcesUsingSemiColons.class);248 List<PersonTestScenario> testScenarios249 = DataDrivenAnnotations.forClass(testClass).getDataAsInstancesOf(PersonTestScenario.class);250 assertThat(testScenarios.size(), is(4));251 MatcherAssert.assertThat(testScenarios.get(0).getName(), is("Joe Smith"));252 MatcherAssert.assertThat(testScenarios.get(0).getAddress(), is("10 Main Street, Smithville"));253 MatcherAssert.assertThat(testScenarios.get(1).getName(), is("Jack Black"));254 MatcherAssert.assertThat(testScenarios.get(1).getAddress(), is("1 Main Street, Smithville"));255 MatcherAssert.assertThat(testScenarios.get(2).getName(), is("Joe Smith"));256 MatcherAssert.assertThat(testScenarios.get(2).getAddress(), is("10 Main Street, Smithville"));257 MatcherAssert.assertThat(testScenarios.get(3).getName(), is("Jack Black"));258 MatcherAssert.assertThat(testScenarios.get(3).getAddress(), is("1 Main Street, Smithville"));259 }260 @Test261 public void should_be_able_to_get_data_Table_from_a_semicolon_delimited_csv() throws Throwable {262 TestClass testClass = new TestClass(CSVDataDrivenTestScenarioUsingSemiColons.class);263 DataTable testDataTable = DataDrivenAnnotations.forClass(testClass).getParametersTableFromTestDataSource();264 List<String> parameterNames = testDataTable.getHeaders();265 assertThat(parameterNames.size(), is(3));266 assertThat(parameterNames.get(0), is("NAME"));267 assertThat(parameterNames.get(1), is("AGE"));268 assertThat(parameterNames.get(2), is("ADDRESS"));269 }270 @UseTestDataFrom(value="$DATADIR/simple-semicolon-data.csv", separator=';')271 final static class CSVDataDrivenTestScenarioFromSpecifiedDataDirectory {}272 @Test273 public void should_load_test_data_from_a_specified_directory() throws IOException {274 EnvironmentVariables environmentVariables = new MockEnvironmentVariables();275 environmentVariables.setProperty("thucydides.data.dir","test-data");276 TestClass testClass = new TestClass(CSVDataDrivenTestScenarioFromSpecifiedDataDirectory.class);277 List<PersonTestScenario> testScenarios278 = DataDrivenAnnotations.forClass(testClass)279 .usingEnvironmentVariables(environmentVariables)280 .getDataAsInstancesOf(PersonTestScenario.class);281 assertThat(testScenarios.size(), is(2));282 MatcherAssert.assertThat(testScenarios.get(0).getName(), is("Joe Smith"));283 MatcherAssert.assertThat(testScenarios.get(0).getAddress(), is("10 Main Street, Smithville"));284 MatcherAssert.assertThat(testScenarios.get(1).getName(), is("Jack Black"));285 MatcherAssert.assertThat(testScenarios.get(1).getAddress(), is("1 Main Street, Smithville"));286 }287 @UseTestDataFrom(value="does-not-exist/simple-semicolon-data.csv,test-data/simple-semicolon-data.csv", separator=';')288 final static class CSVDataDrivenTestScenarioFromSeveralPossibleSources{}289 @Test290 public void should_load_test_data_from_several_possible_sources() throws IOException {291 TestClass testClass = new TestClass(CSVDataDrivenTestScenarioFromSeveralPossibleSources.class);292 List<PersonTestScenario> testScenarios293 = DataDrivenAnnotations.forClass(testClass)294 .getDataAsInstancesOf(PersonTestScenario.class);295 assertThat(testScenarios.size(), is(2));296 MatcherAssert.assertThat(testScenarios.get(0).getName(), is("Joe Smith"));297 MatcherAssert.assertThat(testScenarios.get(0).getAddress(), is("10 Main Street, Smithville"));298 MatcherAssert.assertThat(testScenarios.get(1).getName(), is("Jack Black"));299 MatcherAssert.assertThat(testScenarios.get(1).getAddress(), is("1 Main Street, Smithville"));300 }301 @UseTestDataFrom(value="does-not-exist/simple-semicolon-data.csv,still-does-not-exist/simple-semicolon-data.csv", separator=';')302 final static class CSVDataDrivenTestScenarioFromSeveralPossibleSourcesWithNoValidSource{}303 @Test(expected = IllegalArgumentException.class)304 public void should_load_test_data_from_several_possible_sources_with_no_valid_source() throws IOException {305 TestClass testClass = new TestClass(CSVDataDrivenTestScenarioFromSeveralPossibleSourcesWithNoValidSource.class);306 List<PersonTestScenario> testScenarios307 = DataDrivenAnnotations.forClass(testClass)308 .getDataAsInstancesOf(PersonTestScenario.class);309 }310}...
Source:SerenityParameterizedRunner.java
...134 private String getQualifierFor(final Object testCase) {135 return QualifierFinder.forTestCase(testCase).getQualifier();136 }137 private DataDrivenAnnotations getTestAnnotations() {138 return DataDrivenAnnotations.forClass(getTestClass());139 }140 private String from(final Collection testData) {141 StringBuffer testDataQualifier = new StringBuffer();142 boolean firstEntry = true;143 for (Object testDataValue : testData) {144 if (!firstEntry) {145 testDataQualifier.append("/");146 }147 testDataQualifier.append(testDataValue);148 firstEntry = false;149 }150 return testDataQualifier.toString();151 }152 /**...
Source:DataDrivenAnnotations.java
...22import java.util.regex.Pattern;23public class DataDrivenAnnotations {24 private final EnvironmentVariables environmentVariables;25 private final Pattern DATASOURCE_PATH_SEPARATORS = Pattern.compile("[;,]");26 public static DataDrivenAnnotations forClass(final Class testClass) {27 return new DataDrivenAnnotations(testClass);28 }29 public static DataDrivenAnnotations forClass(final TestClass testClass) {30 return new DataDrivenAnnotations(testClass);31 }32 private final TestClass testClass;33 DataDrivenAnnotations(final Class testClass) {34 this(new TestClass(testClass));35 }36 DataDrivenAnnotations(final TestClass testClass) {37 this(testClass, Injectors.getInjector().getProvider(EnvironmentVariables.class).get());38 }39 DataDrivenAnnotations(final TestClass testClass, EnvironmentVariables environmentVariables) {40 this.testClass = testClass;41 this.environmentVariables = environmentVariables;42 }43 DataDrivenAnnotations usingEnvironmentVariables(EnvironmentVariables environmentVariables) {...
Source:TestClassRunnerForParameters.java
...50 } catch (ClassCastException cause) {51 throw new Exception(String.format(52 "%s.%s() must return a Collection of arrays.",53 getTestClass().getName(),54 DataDrivenAnnotations.forClass(getTestClass()).getTestDataMethod().getName()),55 cause);56 }57 }58 @Override59 protected String getName() {60 String firstParameter = parametersTable.getRows().get(parameterSetNumber).getValues().get(0).toString();61 return String.format("[%s]", firstParameter);62 }63 @Override64 protected String testName(final FrameworkMethod method) {65 return String.format("%s[%s]", method.getName(), parameterSetNumber);66 }67 @Override68 protected void validateConstructor(final List<Throwable> errors) {...
Source:DataDrivenTestFinder.java
...20 public int countTestMethods() {21 int totalTestMethods = 0;22 for(Class testClass : getDataDrivenTestClasses()) {23 try {24 totalTestMethods += DataDrivenAnnotations.forClass(new TestClass(testClass)).countDataEntries();25 } catch (IOException e) {26 throw new IllegalArgumentException("Failed to read test data for " + testClass);27 }28 }29 return totalTestMethods;30 }31}
forClass
Using AI Code Generation
1package net.serenitybdd.junit.runners;2import java.lang.annotation.Annotation;3import java.lang.reflect.Method;4import java.util.ArrayList;5import java.util.List;6import org.junit.runners.model.FrameworkMethod;7import org.junit.runners.model.TestClass;8public class DataDrivenAnnotations {9 public static List<FrameworkMethod> forClass(TestClass testClass) {10 List<FrameworkMethod> result = new ArrayList<FrameworkMethod>();11 for (Method method : testClass.getJavaClass().getMethods()) {12 for (Annotation annotation : method.getAnnotations()) {13 if (annotation.annotationType().equals(DataDriven.class)) {14 result.add(new FrameworkMethod(method));15 }16 }17 }18 return result;19 }20}
forClass
Using AI Code Generation
1import net.serenitybdd.junit.runners.DataDrivenAnnotations;2import org.junit.Test;3import org.junit.runner.RunWith;4@RunWith(DataDrivenAnnotations.class)5public class DataDrivenAnnotationsTest {6 @DataDrivenAnnotations.ForClass(ExampleDataDrivenAnnotations.class)7 public void testWithForClass(ExampleDataDrivenAnnotations example) {8 example.runTest();9 }10}11public class ExampleDataDrivenAnnotations {12 private String name;13 private int age;14 public ExampleDataDrivenAnnotations(String name, int age) {15 this.name = name;16 this.age = age;17 }18 public void runTest() {19 System.out.println("name = " + name);20 System.out.println("age = " + age);21 }22}23 {24 },25 {26 }
forClass
Using AI Code Generation
1 private static Object[] useForClassMethod() {2 return new Object[] {3 new Object[] { 1, 2, 3 },4 new Object[] { 4, 5, 9 }5 };6 }7}
forClass
Using AI Code Generation
1 public void testMethod(String arg1, String arg2) {2 System.out.println("arg1 = " + arg1);3 System.out.println("arg2 = " + arg2);4 }5 public void testMethod2(String arg1, String arg2) {6 System.out.println("arg1 = " + arg1);7 System.out.println("arg2 = " + arg2);8 }9}10@RunWith(SerenityParameterizedRunner.class)11public class DataDrivenTests {12 private String arg1;13 private String arg2;14 public DataDrivenTests(String arg1, String arg2) {15 this.arg1 = arg1;16 this.arg2 = arg2;17 }18 @Parameterized.Parameters(name = "{index}: {0} + {1} = {2}")19 public static Collection<Object[]> data() {20 return DataDrivenAnnotations.forClass(DataDrivenTests.class).getData();21 }22 public void testMethod() {23 System.out.println("arg1 = " + arg1);24 System.out.println("arg2 = " + arg2);25 }26 public void testMethod2() {27 System.out.println("arg1 = " + arg1);28 System.out.println("arg2 = " + arg2);29 }30}31@RunWith(SerenityParameterizedRunner.class)32public class DataDrivenTests {33 private String arg1;34 private String arg2;35 public DataDrivenTests(String arg1, String arg2) {36 this.arg1 = arg1;37 this.arg2 = arg2;38 }39 @Parameterized.Parameters(name = "{index}: {0} + {1} = {2}")40 public static Collection<Object[]> data() {41 return DataDrivenAnnotations.forMethod(DataDrivenTests.class, "testMethod");42 }
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!