1

我对喷涂还很陌生。我正在尝试正确进行测试,因此我使用了spary testkit中显示的示例,但是我收到了此错误。任何帮助将不胜感激:

The service should

Could not initialize class spray.http.Uri$
java.lang.NoClassDefFoundError: Could not initialize class spray.http.Uri$
    at spray.httpx.RequestBuilding$RequestBuilder.apply(RequestBuilding.scala:36)
    at spray.httpx.RequestBuilding$RequestBuilder.apply(RequestBuilding.scala:34)
    at com.tr.route.EventRouteTester$$anonfun$3$$anonfun$apply$2.apply(EventRouteTester.scala:38)
    at com.tr.route.EventRouteTester$$anonfun$3$$anonfun$apply$2.apply(EventRouteTester.scala:38)

return a 'PONG!' response for GET requests to /ping

Could not initialize class spray.http.Uri$
java.lang.NoClassDefFoundError: Could not initialize class spray.http.Uri$
    at spray.httpx.RequestBuilding$RequestBuilder.apply(RequestBuilding.scala:36)
    at spray.httpx.RequestBuilding$RequestBuilder.apply(RequestBuilding.scala:34)
    at com.tr.route.EventRouteTester$$anonfun$3$$anonfun$apply$6.apply(EventRouteTester.scala:44)
    at com.tr.route.EventRouteTester$$anonfun$3$$anonfun$apply$6.apply(EventRouteTester.scala:44)

leave GET requests to other paths unhandled

scala/collection/GenTraversableOnce$class
java.lang.NoClassDefFoundError: scala/collection/GenTraversableOnce$class
    at spray.http.Uri$Query.<init>(Uri.scala:496)
    at spray.http.Uri$Query$Empty$.<init>(Uri.scala:575)
    at spray.http.Uri$Query$Empty$.<clinit>(Uri.scala)
    at spray.http.parser.UriParser.<init>(UriParser.scala:37)
    at spray.http.Uri$.apply(Uri.scala:231)
    at spray.http.Uri$.apply(Uri.scala:203)
    at spray.http.Uri$.<init>(Uri.scala:194)
    at spray.http.Uri$.<clinit>(Uri.scala)
    at spray.httpx.RequestBuilding$RequestBuilder.apply(RequestBuilding.scala:36)
    at spray.httpx.RequestBuilding$RequestBuilder.apply(RequestBuilding.scala:34)
    at spray.httpx.RequestBuilding$RequestBuilder.apply(RequestBuilding.scala:33)
    at com.tr.route.EventRouteTester$$anonfun$3$$anonfun$apply$9.apply(EventRouteTester.scala:50)
    at com.tr.route.EventRouteTester$$anonfun$3$$anonfun$apply$9.apply(EventRouteTester.scala:50)
Caused by: java.lang.ClassNotFoundException: scala.collection.GenTraversableOnce$class
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    ... 13 more

return a MethodNotAllowed error for PUT requests to the root path

这是测试类

class RouteTester extends Specification with Specs2RouteTest with HttpService {
  def actorRefFactory = system // connect the DSL to the test ActorSystem

  val smallRoute =
    get {
      pathSingleSlash {
        complete {
          <html>
            <body>
              <h1>Say hello to <i>spray</i>!</h1>
            </body>
          </html>
        }
      } ~
        path("ping") {
          complete("PONG!")
        }
    }

  "The service" should {

    "return a greeting for GET requests to the root path" in {
      Get() ~> smallRoute ~> check {
        responseAs[String] must contain("Say hello")
      }
    }

    "return a 'PONG!' response for GET requests to /ping" in {
      Get("/ping") ~> smallRoute ~> check {
        responseAs[String] === "PONG!"
      }
    }

    "leave GET requests to other paths unhandled" in {
      Get("/kermit") ~> smallRoute ~> check {
        handled must beFalse
      }
    }

    "return a MethodNotAllowed error for PUT requests to the root path" in {
      Put() ~> sealRoute(smallRoute) ~> check {
        status === MethodNotAllowed
        responseAs[String] === "HTTP method not allowed, supported methods: GET"
      }
    }
  }
}

我正在使用 maven 这是依赖项和版本

  <scala.version>2.11.2</scala.version>
        <spray.version>1.3.1</spray.version>
        <akka.version>2.3.8</akka.version>

      <dependency>
            <groupId>io.spray</groupId>
            <artifactId>spray-can</artifactId>
            <version>${spray.version}</version>
        </dependency>
        <dependency>
            <groupId>io.spray</groupId>
            <artifactId>spray-routing</artifactId>
            <version>${spray.version}</version>
        </dependency>
        <dependency>
            <groupId>io.spray</groupId>
            <artifactId>spray-json_2.11</artifactId>
            <version>${spray.version}</version>
        </dependency>
         <dependency>
            <groupId>io.spray</groupId>
            <artifactId>spray-testkit_2.11</artifactId>
             <version>${spray.version}</version>
        </dependency>
4

2 回答 2

1

解决了 !问题是需要不同版本的依赖项。即 spray-can 和 spray-routing 工件都依赖于 scala 2.10,而声明的其余依赖项依赖于 scala 2.11。

我所要做的就是将依赖项更改为:

<scalaBinaryVersion>2.11</scalaBinaryVersion>
<dependency>
    <groupId>io.spray</groupId>
    <artifactId>spray-can_${scalaBinaryVersion}</artifactId>

    <version>${spray.version}</version>
</dependency>
<dependency>
    <groupId>io.spray</groupId>
    <artifactId>spray-routing_${scalaBinaryVersion}</artifactId>

    <version>${spray.version}</version>
</dependency>

问题解决了!

于 2015-03-06T19:45:22.267 回答
1

最近我在各种测试套件上苦苦挣扎,最终使用纯 Akka TestKit,因为 Spray TestKit 迫使我将我的 HttpServiceActor 拆分为单独的特征和演员,这对我来说是不可接受的。

我可以分享一些代码以获得灵感,您可以尝试一下(除非有人给出更好的答案):

RestInterface.scala:

import spray.routing.HttpServiceActor

class RestInterface extends HttpServiceActor {
  override def receive: Receive = runRoute(smallRoute)
  val smallRoute =
    get {
      pathSingleSlash {
        complete {
          <html>
            <body>
              <h1>Say hello to <i>spray</i>!</h1>
            </body>
          </html>
        }
      } ~
      path("ping") {
        complete("PONG!")
      }
    }
}

RestInterfaceSpec.scala:

import akka.actor.{Props, ActorSystem}
import akka.testkit.{TestActorRef, ImplicitSender, TestKit}
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}
import spray.http.HttpResponse
import spray.httpx.RequestBuilding

class RestInterfaceSpec(_system: ActorSystem) extends TestKit(_system) with ImplicitSender with WordSpecLike with Matchers with BeforeAndAfterAll with RequestBuilding with SpecHelper {
  def this() = this(ActorSystem("MySpec"))

  "The service" must {
    "return a response" in {
      val service = TestActorRef[RestInterface]
      service ! Get("/")
      expectMsgType[HttpResponse]
    }
  }
}

SpecHelper.scala:

import akka.actor.ActorSystem
import akka.testkit.TestKit

trait SpecHelper extends BeforeAndAfterAll { this: Suite =>
  implicit val system: ActorSystem

  override def afterAll(): Unit = {
    TestKit.shutdownActorSystem(system)
  }
}
于 2015-03-05T20:52:49.157 回答