Iterating all Play Framework routes in Test
Is there any way to Iterate all described services in routes
file? URL and HTTP methods are needed.
I need this feature for running some integration test.
I am using Play for Java.
Not easily. I managed to hack my way through it a while ago(no scala know-how). I'll post that code maybe it can be of use.
public static List<String[]> parseRoutes() {
scala.Option<play.core.Router.Routes> option = Play.application().getWrappedApplication().routes();
if (option.isDefined()) {
play.core.Router.Routes routes = option.get();
scala.collection.Seq<scala.Tuple3<String, String, String>> doc = routes.documentation();
scala.collection.Iterator<scala.Tuple3<String, String, String>> it = doc.iterator();
List<String[]> listOfRoutes = new ArrayList<String[]>();
while(it.hasNext()) {
scala.Tuple3<String, String, String> tuple = it.next();
//tuple._1() is the method and tuple._2() the url... tuple._3() is the controller name
String[] route = {tuple._1(), tuple._2()};
listOfRoutes.add(route);
Logger.debug("route -> " + Arrays.toString(route));
}
return listOfRoutes;
}
return null;
}
Don't worry about the .iterator()
showing a The method iterator() is ambiguous for the type Seq<Tuple3<String,String,String>>
. It compiles just fine in play.
上一篇: 如何为AutoCompleteTextView建议添加空间
下一篇: 在测试中迭代所有Play框架路线