0

我将使用akka-httpwith Java。路由成功完成并运行。但是当我尝试使用 编写测试用例时JUnitRouteTest,出现错误。

com.james.controllers.PingPongApiTest is not abstract and does not override abstract method yeOldeTestNames() in org.scalatest.Suite

我正在关注akka http route testkit 文档。根据这个文档,我们只需要akka-http-testkit. 但是对于Java我们也需要Junit依赖。只有junit依赖我得到另一个错误是cannot access org.scalatest.junit.JUnitSuiteLike。我也注入了 scala 测试依赖,如下所示:

libraryDependencies ++= Seq(
 "com.typesafe.akka" % "akka-http-experimental_2.11" % "2.4.8",
 "com.typesafe.akka" % "akka-http-testkit_2.11" % "2.4.8" % "test",
 "junit" % "junit" % "4.12" % "test",
 "org.scalatest" % "scalatest_2.11" % "3.0.0" % "test"
) 

然后我收到以下错误:

com.james.controllers.PingPongApiTest is not abstract and does not override abstract method yeOldeTestNames() in org.scalatest.Suite
[error] public class PingPongApiTest extends JUnitRouteTest {
[error]     TestRoute route = testRoute(new PingPongApi().handleGetPingRequest());
[error] 
[error]     @Test
[error]     public void testGetPingRequest() {
[error] 
[error]         route.run(HttpRequest.GET("/ping"))
[error]                 .assertStatusCode(StatusCodes.OK)
[error]                 .assertEntity("pong");
[error]     }
[error] }
[error] (test:compileIncremental) javac returned nonzero exit code
[error] Total time: 1 s, completed 10 Aug, 2016 11:28:11 AM

我怎么能解决这个问题。以下是我的代码:

路线类

public class PingPongApi  {

 public Route handleGetPingRequest() {
    return get(() -> route(
            path("ping", () -> complete("pong"))
    ));
 } 
}

测试班

public class PingPongApiTest extends JUnitRouteTest {
 TestRoute route = testRoute(new PingPongApi().handleGetPingRequest());

 @Test
 public void testGetPingRequest() {

    route.run(HttpRequest.GET("/ping"))
            .assertStatusCode(StatusCodes.OK)
            .assertEntity("pong");
 }
}
4

1 回答 1

1

我找到了上述问题的解决方案。我们的依赖版本的主要问题。为了运行 Java 路由测试用例,我们需要scalatest依赖,因为JUnitRouteTest使用scalatest. 以下是我们需要的依赖项。

libraryDependencies ++= Seq(
 "com.typesafe.akka" % "akka-http-experimental_2.11" % "2.4.8",
 "com.typesafe.akka" % "akka-http-testkit_2.11" % "2.4.8" % "test",
 "org.scalatest" %% "scalatest" % "2.2.4" % "test",
 "junit" % "junit" % "4.12" % "test",
 "com.novocode" % "junit-interface" % "0.11" % "test" exclude("junit", "junit")
)
于 2016-08-11T12:04:38.240 回答