CLOVER馃崁

That was when it all began.

Arquillian銇AX-RS銇儐銈广儓

鍓嶃伀銆丄rquillian銇с倓銇c仧銆併儐銈广儓銇稓銇嶃傚墠鍥炪伅EJBJPA銇犮亼銇犮仯銇熴伄銇с佷粖搴︺伅JAX-RS銇儐銈广儓銈掋倓銇c仸銇裤伨銇欍

銇ㄣ亜銇c仸銈傘併伝銇ㄣ倱銇┿亾銇撱伄銉炪儘銇с伅銇傘倞銇俱仚銇屻

arquillian-showcase / jaxrs
https://github.com/arquillian/arquillian-showcase/tree/master/jaxrs

澶с亶銇忛仌銇嗐伄銇Scala锛媠bt銇с倓銇c仧銇撱仺銇ㄣ丷ESTEasy銇甈roxyFactory銇Ε鍗斻仐銇熴亾銇ㄣ仹銇椼倗銇嗐亱鈥︺

婧栧倷

sbt銇ō瀹氥亱銈夈
build.sbt

name := "arquillian-jax-rs"

version := "0.0.1-SNAPSHOT"

scalaVersion := "2.10.3"

organization := "littlewings"

resolvers += "Public JBoss Group" at "http://repository.jboss.org/nexus/content/groups/public-jboss"

fork in Test := true

envVars in Test += ("JBOSS_HOME", "/path/to/jboss-as-7.1.1.Final")

libraryDependencies ++= Seq(
  "org.jboss.spec" % "jboss-javaee-web-6.0" % "3.0.2.Final" % "provided",
  "org.jboss.as" % "jboss-as-arquillian-container-managed" % "7.1.1.Final" % "test" excludeAll(
    ExclusionRule(organization = "org.jboss.shrinkwrap"),
    ExclusionRule(organization = "org.jboss.shrinkwrap.descriptors")
  ),
  "org.jboss.arquillian.junit" % "arquillian-junit-container" % "1.1.1.Final" % "test" excludeAll(
    ExclusionRule(organization = "org.jboss.shrinkwrap"),
    ExclusionRule(organization = "org.jboss.shrinkwrap.descriptors")
  ),
  "org.jboss.shrinkwrap" % "shrinkwrap-api" % "1.0.0-cr-1" % "test",
  "org.jboss.shrinkwrap" % "shrinkwrap-impl-base" % "1.1.2" % "test",
  "org.jboss.shrinkwrap.descriptors" % "shrinkwrap-descriptors-spi" % "2.0.0-alpha-3" % "test",
  "org.jboss.shrinkwrap.resolver" % "shrinkwrap-resolver-depchain" % "2.0.1" % "test" excludeAll(
    ExclusionRule(organization = "org.jboss.shrinkwrap")
  ),
  "org.jboss.resteasy" % "resteasy-jaxrs" % "2.3.2.Final" % "test",
  "junit" % "junit" % "4.11" % "test",
  "com.novocode" % "junit-interface" % "0.10" % "test"
)

浠婂洖銇佸墠鍥炴厡銇︺仸閫氥仐銇熴儵銈ゃ儢銉┿儶銇緷瀛橀枹淇傘倰鏁寸悊銇欍伖銇忋Maven銇rquillian銇緷瀛橀枹淇傘倰dependenc:tree銇ц銇亴銈夈倓銈娿伨銇椼仧銇屻佺祼灞鍚屻仒銈堛亞銇祼鏋溿伀銇倞銇俱仐銇熴

JAX-RS銇湁鍔瑰寲銆
src/main/scala/javaee6/web/jaxrs/SimpleApplication.scala

package javaee6.web.jaxrs

import javax.ws.rs.ApplicationPath
import javax.ws.rs.core.Application

@ApplicationPath("/rest")
class SimpleApplication extends Application {
}

銉嗐偣銉堝璞°伄銈儵銈广
src/main/scala/javaee6/web/jaxrs/SimpleResource.scala

package javaee6.web.jaxrs

import javax.ws.rs.{GET, Path, Produces}
import javax.ws.rs.core.MediaType

@Path("simple")
class SimpleResource {
  @GET
  @Produces(Array(MediaType.TEXT_PLAIN))
  def hello: String =
    "Hello World"
}

銇斻亸銇斻亸銆佺啊鍗樸仾銈傘伄銇仐銇俱仐銇熴

銉嗐偣銉堛偝銉笺儔

Arquillian銈掍娇銇c仧銉嗐偣銉堛偝銉笺儔銇併亾銇°倝銆
src/test/scala/javaee6/web/jaxrs/SimpleResourceTest.scala

package javaee6.web.jaxrs

import java.net.URL

import javax.ws.rs.ApplicationPath
import javax.ws.rs.core.{MediaType, Response}

import org.jboss.arquillian.container.test.api.{Deployment, RunAsClient}
import org.jboss.arquillian.junit.Arquillian
import org.jboss.arquillian.test.api.ArquillianResource
import org.jboss.resteasy.client.{ClientRequest, ClientResponse}
import org.jboss.resteasy.plugins.providers.RegisterBuiltin
import org.jboss.resteasy.spi.ResteasyProviderFactory
import org.jboss.shrinkwrap.api.ShrinkWrap
import org.jboss.shrinkwrap.api.spec.WebArchive
import org.jboss.shrinkwrap.resolver.api.maven.Maven

import org.junit.{BeforeClass, Test}
import org.junit.runner.RunWith

import org.junit.Assert._
import org.hamcrest.CoreMatchers._

@RunWith(classOf[Arquillian])
@RunAsClient
class SimpleResourceTest {
  private val resourcePrefix =
    classOf[SimpleApplication]
      .getAnnotation(classOf[ApplicationPath])
      .value
      .substring(1)

  @ArquillianResource
  private var deploymentUrl: URL = _

  @Test
  def helloTest: Unit = {
    val request = new ClientRequest(deploymentUrl + resourcePrefix + "/simple")
    request.header("Accept", MediaType.TEXT_PLAIN)
    val response = request.get(classOf[String])
    assertThat(response.getStatus, is(Response.Status.OK.getStatusCode))
    assertThat(response.getEntity, is("Hello World"))
  }
}

object SimpleResourceTest {
  @Deployment
  def createDeployment: WebArchive =
    ShrinkWrap
      .create(classOf[WebArchive], "arquillian-test.war")
      .addPackages(true, "javaee6.web.jaxrs")
      .addAsLibraries {
        Maven
          .resolver
          .resolve("org.scala-lang:scala-library:2.10.3")
          .withTransitivity
          .asFile: _*
      }
}

銇俱亗銆丷ESTEasy銇偗銉┿偆銈€兂銉API銈掍娇銇c仸銉偗銈ㄣ偣銉堛倰鎶曘亽銇︺亜銈嬨仩銇戙併仺銈傝█銇堛伨銇欍亴銆俿howcase銇倐銇倐銆併仢銈撱仾鎰熴仒銇с仐銇熴仐銇
JAX-RS 2.0銇偗銉┿偆銈€兂銉API銇WildFly銇屽嚭銇熴倝浣裤亜銇俱仚

銈€儣銉偙銉笺偡銉с兂銈点兗銉銇嬨倝瑕嬨仸銈儵銈ゃ偄銉炽儓銇ㄣ仐銇﹀嫊浣溿仌銇涖倠銇熴倎銇嬨併儐銈广儓銈儵銈广伀@RunAsClient銈€儙銉嗐兗銈枫儳銉銇屼粯銇勩仸銇勩倠銇撱仺銇屻儩銈ゃ兂銉堛伩銇熴亜銇с仚銆

@RunWith(classOf[Arquillian])
@RunAsClient
class SimpleResourceTest {

銇濄伄浠栥

  @ArquillianResource
  private var deploymentUrl: URL = _

銇с儑銉椼儹銈ゅ厛銇熀鏈殑銇猆RL銇屽彇銈屻倠銇撱仺銇ㄣJAX-RS銇ō瀹氥倰銇嗐伨銇忎娇銇c仸銉戙偣銈掔祫銇跨珛銇︺倠銇伅銆併仾銈嬨伝銇┿仾銉笺仺鎬濄亜銇俱仐銇熴

  private val resourcePrefix =
    classOf[SimpleApplication]
      .getAnnotation(classOf[ApplicationPath])
      .value
      .substring(1)

銇傘仺銇併儐銈广儓銈掑疅琛屻仚銈嬨仩銇戙

> test

绨″崢銇с伅銇傘倞銇俱仚銇屻併亾銈撱仾銇ㄣ亾銈嶃仹銆傝繎銇勩亞銇°伀銆丷emote銉€兗銉夈倐銈勩倝銇亜銇ㄣScala銇伨銇俱仹銆併仹銇嶃倠銇嬨仾锛