I want to have a custom JUnit annotation, and if that annotation is present on a test method, it should make the List
of that object available to that test method, mostly through as a Method parameter.
Company
is a outer class object which contains List<Employee>
, and the test needs to have flexibility to have default employee list or providing a custom list.
For my test method, I am processing the annotation in respective test, however how can I have this annotation run for all the test files ( similar to @BeforeMethod
) and if my custom annotation is present on a method, inject it as List<Employee>
?
@Test
@CompanyAnnotation(salary = 5)
@CompanyAnnotation(salary = 50)
public void testCompany(// Want to inject as method parameter of List<Employee> list) {
for(Method method : CompanyTest.class.getDeclaredMethods()) {
CompanyAnnotation[] annotations = method.getAnnotationsByType(
CompanyAnnotation.class);
for(CompanyAnnotation d : annotations) {
System.out.println(b);
}
}
}
===
class Company {
// many other properties
List<Employee> employeeList;
}
class Employee {
// more properties
Integer age;
}
class CompanyBuilder {
int defaultEmployeeSize = 10;
public Company create(List<Employee> incoming) {
List<Employee> employees = new ArrayList<>();
employees.addAll(incoming);
if ( employees.size() < defaultEmployeeSize ) {
for(int i = 0; i < (defaultEmployeeSize - employees.size()); i++) {
employee.add(new Employee());
}
}
return employees;
}
}