JUnitParams
JUnitParams copied to clipboard
Question about custom ParametersProvider (@CustomParameters)
Hello, I have my own annotation with some configuration
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@CustomParameters(provider = MyCustomProvider.class)
public @interface MyCustomAnnotation {
String[] namesOfFilesToSearch() default { "something.txt, something2.json" };
}
and provider:
public class MyCustomProvider implements ParametersProvider<MyCustomAnnotation> {
...
@Override
public Object[] getParameters() {
// a lot of business logic, searching for files and so on...
return someArrayWithMyCustomObjects;
}
I would love to split whole logic from getParameters() into many classes and then inject them via constructor. How can I do that? Is it even possible?
The result I want:
public class MyCustomProvider implements ParametersProvider<MyCustomAnnotation> {
public FunctionalTestsProvider(FilesManager filesManager, SomeBusinessExecutor business) {
this.filesManager = filesManager;
this.business = business;
}
...
@Override
public Object[] getParameters() {
...
filesManager.doSomething();
business.doSomethingElse();
return someArrayWithMyCustomObjects;
}
It is, but not via constructor. What you could do is use some sort of Dependency injection (ServiceLocator) and pass your context via ThreadLocal.
Spring uses this technique in SpringBeanAutowiringSupport
(Spring way example):
public class MyCustomProvider implements ParametersProvider<MyCustomAnnotation> {
private FilesManager filesManager;
public MyCustomProvider() {
// this would inject your dependency
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
}