在Test中迭代所有Play Framework路由

Moh*_*shi 7 java integration-testing routes playframework-2.3

有没有办法迭代routes文件中描述的所有服务?需要URL和HTTP方法.

我需要此功能来运行一些集成测试.

我正在使用Play for Java.

seb*_*ter 4

不容易。不久前我设法破解了它(没有 scala 知识)。我将发布该代码也许它有用。

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;
}
Run Code Online (Sandbox Code Playgroud)

不用担心.iterator()显示The method iterator() is ambiguous for the type Seq<Tuple3<String,String,String>>. 它在游戏中编译得很好。