在Scala中使用Jersey测试框架

时间:2014-09-11 21:42:01

标签: scala jersey jersey-2.0

我正在尝试使用Jersey测试框架和specs2来测试Scala中的HTTP客户端。这是我到目前为止所做的:

package com.mypackage

import java.net.URL
import javax.ws.rs.{GET, Path}
import org.glassfish.jersey.server.ResourceConfig
import org.glassfish.jersey.test.{TestProperties, JerseyTest}
import org.junit.runner.RunWith
import org.specs2.mutable.Specification
import org.specs2.runner.JUnitRunner

// This class provides an HTTP server for testing.
@Path("/")
class MockServer extends JerseyTest {

  @GET
  @Path("ok")
  def getOk = "OK"

  override def configure = {
    forceSet(TestProperties.CONTAINER_PORT, "0") // Choose first available port.
    new ResourceConfig(getClass)
  }

  def getUrl(relativePath: String) = new URL(getBaseUri.toURL, relativePath)
}

// This class contains the tests.
@RunWith(classOf[JUnitRunner])
class TestHTTPAdapter extends Specification {

  val server = new MockServer

  step {
    server.setUp()
    success
  }

  "HTTPAdapter" should {
    "GET text" in {
      // HTTPAdapter is the HTTP client being tested.
      // Create a new one with a 5 second timeout.
      val httpAdapter = new HTTPAdapter(5)

      // Request data
      val result = httpAdapter.get(server.getUrl("ok"))

      // Verify response
      result.code.code mustEqual 200
      result.bodyString mustEqual "OK"
    }
  }

  step {
    server.tearDown()
    success
  }

}

当我运行测试时,我得到一个例外:

[info] TestHTTPAdapter
[info] ! step error
[error]  NoSuchMethodError: : javax.ws.rs.core.Application.getProperties()Ljava/util/Map;  (ApplicationHandler.java:304)
[error] org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:304)
[error] org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:285)
[error] org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.<init>(GrizzlyHttpContainer.java:331)
[error] org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory.createHttpServer(GrizzlyHttpServerFactory.java:141)
[error] org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory$GrizzlyTestContainer.<init>(GrizzlyTestContainerFactory.java:82)
[error] org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory$GrizzlyTestContainer.<init>(GrizzlyTestContainerFactory.java:66)
[error] org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory.create(GrizzlyTestContainerFactory.java:130)
[error] org.glassfish.jersey.test.JerseyTest.createTestContainer(JerseyTest.java:277)
[error] org.glassfish.jersey.test.JerseyTest.setUp(JerseyTest.java:609)
[error] com.mypackage.TestHTTPAdapter$$anonfun$1.apply(TestHTTPAdapter.scala:32)
[error] com.mypackage.TestHTTPAdapter$$anonfun$1.apply(TestHTTPAdapter.scala:31)

为什么这不起作用?我错过了依赖吗?在我的SBT文件中,唯一的Jersey引用是"org.glassfish.jersey.test-framework.providers" % "jersey-test-framework-provider-grizzly2" % "2.12" % Test

我很乐意使用Jersey测试框架,但如果有人可以推荐更好的选择,我会对其他框架开放。

谢谢。

1 个答案:

答案 0 :(得分:1)

我发现我项目的一个依赖项是使用旧的1.x版本的jersey-core。我在build.sbt中添加了对libraryDependencies的排除:("otherProjectGroupId" % "otherProjectArtifactId" % "otherProjectVersion").exclude("com.sun.jersey", "jersey-core")现在一切正常。