0
votes

is there a way to tell a Test with annotations or something like that, to load properties based on a custom annotation and run the tests equally to the number of parameters that test has.

For example: I want to run test A that has values injected with Spring @value , three times, and for run 1 I want the test to get the values from property file X for run 2 from property file Y and you got it, run 3 from property file Z.

@Value("${person.name}")
private String person.name;

@RunTestWithProperties(properties = {X,Y,Z})
@Test
public void testA() {(System.out.println(person.name); }

On the first run, this test would print the person.name from X properties file, on the second run the test would print the person.name from Y and so on.

What would be expected:

testA runs 3 times(each run with different properties) from files X, Y and Z;

I could use data providers or something like that, load properties with system variables but it is not the solution I want.

Technologies I use are Java, TestNG and Spring. Any solution is more than welcomed.

Thank you in advance guys!

1
You can try using mockitoNaveen Kulkarni
@Value("{person.name}") change to @Value("${person.name}")Naveen Kulkarni
Yes, I writed that down manually and I forgot the $.ib23
Try using reflection utilsNaveen Kulkarni

1 Answers

0
votes

You can use parameterized tests. You need to create a method annotated with @Parameterized.Parameters where you can load all you data in a collection (Basically the parameters you need to pass for each run).

Then create a constructor to pass the arguments and this constructor argument will be passed from this collection on each run

e.g.

 @RunWith(Parameterized.class)
 public class RepeatableTests {

 private String name;

 public RepeatableTests(String name) {
    this.name = name;
 }

 @Parameterized.Parameters
 public static List<String> data() {
    return Arrays.asList(new String[]{"Jon","Johny","Rob"});
 }

 @Test
 public void runTest() {
    System.out.println("run --> "+ name);
 }
}

or if you don't want to use constructor injection you can use @Parameter annotation to bind the value

@RunWith(Parameterized.class)
public class RepeatableTests {

@Parameter
public String name;

@Parameterized.Parameters(name="name")
public static List<String> data() {
    return Arrays.asList(new String[]{"Jon","Johny","Rob"});
}

@Test
public void runTest() {
    System.out.println("run --> "+ name);
}
}