I have created a custom annotation as below:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Connection {
String protocol();
}
and I have a couple of classes with this annotation as below:
@Connection(protocol = "p1")
public class Connection1 {
public boolean isValidConnection(ConnectionVo connection) {
System.out.print("p1");
}
}
@Connection(protocol = "p2")
public class Connection2 {
public boolean isValidConnection(ConnectionVo connection) {
System.out.print("p2");
}
}
I have a method which selects Connection 1 or Connection 2 isValidConnection() method using Reflection as below:
public void getConnectionProtocol(ConnectionVo connectionVo) throws Exception {
Reflections reflections = new Reflections(Constants.CONNECTION_VALIDATOR_PKG);//package with Connection1 and Connection2 classes
Set<Class<?>> connections = reflections.getTypesAnnotatedWith(Connection.class);
for (Class<?> connection : connections) {
Connection request = connection.getAnnotation(Connection.class);
System.out.print(request.protocol() == connectionVo.protocol);//connectionVo has field protocol with calues p1,p2 etc.
}
}
Now I am trying to write a unit test for the same. I am encountering a problem where reflections.getTypesAnnotatedWith(Connection.class) is just returning names of classes with @Connection annotation i.e., Connection1 and Connection2. How can I get the value that it has i.e., p1 and p2.
Aucun commentaire:
Enregistrer un commentaire