need your help - Running simple spray route test getting: Could not initialize class spray.http.Uri

117 views
Skip to first unread message

Avi Levi

unread,
Mar 6, 2015, 12:29:30 AM3/6/15
to spray...@googlegroups.com

I am fairly new to spray . I am trying to get the testing correctly so I used the example shown in spary testkit documentation 

I am using maven (dependencies and versions below ) everything compiles. however I am getting this errors. please see below the errors, the class and the dependencies. any assistance will greatly appreciated :

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(RouteTester.scala:38)
    at com.tr.route.EventRouteTester$$anonfun$3$$anonfun$apply$2.apply(RouteTester.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(RouteTester.scala:44)
    at com.tr.route.EventRouteTester$$anonfun$3$$anonfun$apply$6.apply(RouteTester.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(RouteTester.scala:50)
    at com.tr.route.EventRouteTester$$anonfun$3$$anonfun$apply$9.apply(RouteTester.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

this is the test class

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"
      }
    }
  }
}

I am using maven this are the dependencies and versions

  <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>

Thank you 
Avi

Stevo Slavić

unread,
Mar 6, 2015, 3:58:28 AM3/6/15
to spray...@googlegroups.com
Check your build log, scala maven plugin is surely printing warnings that there are scala binary incompatible dependencies. Don't ignore the warnings.

spray-can and spray-routing artifacts both depend on scala 2.10 while the rest of declared dependencies depend on scala 2.11.
Define scala.binary.version property in your maven build, set it to 2.11 and then update artifact id's of all of these dependencies to have same "_${scala.binary.version}" suffix.

Even better, for scala projects switch to sbt, especially if you want to build across multiple scala binary versions.

Kind regards,
Stevo Slavic.



--
You received this message because you are subscribed to the Google Groups "spray.io User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
Visit this group at http://groups.google.com/group/spray-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/spray-user/e75ee60e-5124-4651-8180-5c8e79da8aa4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Avi Levi

unread,
Mar 6, 2015, 2:41:42 PM3/6/15
to spray...@googlegroups.com
Thank you very much Stevo ,That was the problem !!!
changed the dependencies to:
<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>
and that solved it !
Thanks !!!
Avi

Reply all
Reply to author
Forward
0 new messages