I have two annotations: @Feature and @Scenario.
The @Feature annotation I'm using it in the Controller methods (GET/PUT/POST... in path "src/main/java...").
The @Scenario annotation I'm using it in my unit tests (Controller or Service tests in path "src/test/java...").
I need to list all methods annotated with both annotations, so I created a class in "src/main/java..." path to do that. I'm able to find all methods annotated with @Feature, but I can't find the methods annotated with @Scenario. How can I do that?
I tried to do that using Reflections library
final Set<Method> features = new Reflections("com.myprefix", new MethodAnnotationsScanner()).getMethodsAnnotatedWith(Feature.class);
final Set<Method> scenarios = new Reflections("com.myprefix", new MethodAnnotationsScanner()).getMethodsAnnotatedWith(Scenario.class);
"features" returns all methods annotated with @Feature, but "scenarios" returns empty
Have you tried writing a JUnit test to solve your problem? Here is one that should work for your problem:
@Test
public void findMethodsannotatedWith(){
List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
classLoadersList.add(ClasspathHelper.contextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader());
Reflections reflections = new Reflections(new ConfigurationBuilder()
.setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner()
, new MethodAnnotationsScanner())
.setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0])))
.filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("com.myprefix"))));
final Set<Method> features = reflections.getMethodsAnnotatedWith(Feature.class);
final Set<Method> scenarios = reflections.getMethodsAnnotatedWith(Scenario.class);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.