Arquillian API REST 示例

3

我找不到任何使用Arquillian进行JAX-RS测试的示例。我正在使用Wildfly 10托管容器。

我尝试自己编写样例代码:

@RunWith(Arquillian.class)
public class DeploymentTest {

 @Deployment(testable = false)
public static Archive<?> deploy() {
    return ShrinkWrap.create(WebArchive.class, "cos-arq-test.war")
            .addClasses(MANUEJB.class, HelloWorld.class, HelloWorldRESTImpl.class)
            .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
}

@ArquillianResource
private URL base;

private static WebTarget target;

@Before
public void setUpClass() throws MalformedURLException {
    Client client = ClientBuilder.newClient();
    target = client.target(URI.create(new URL(base, "rest/helloWorldREST").toExternalForm()));
}

@Test
@RunAsClient
public void testResponse(@ArquillianResource URL base) throws InterruptedException, ExecutionException {

    System.out.println("====================================================");
    System.out.println("This test should run inside the Wildfly 10 container");
    System.out.println("====================================================");

    try {
        System.out.println("URL TARGET: " + target.getUri().toURL().toString());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    assertEquals("Hello", target.request().get().readEntity(String.class));

    /*

    Future<Response> r1 = target.request().async().get();

    Response response = r1.get();

    if (null != response) {

        assertEquals(HttpStatus.OK, response.getStatus());

        assertNotNull(response.getEntity());

        assertEquals("Hello " + "manuel" + "!", response.readEntity(String.class));
    }

    */
 }

}

这是我的服务代码:

@Path("/helloWorldREST")
public class HelloWorldRESTImpl implements HelloWorld {

@GET
public Response sayHi() {
    return Response.ok("Hello").build();
}

@Override
@GET
@Path("/sayHi/{name}")
@Produces(MediaType.APPLICATION_JSON)
public Response sayHi(@PathParam("name") String name) {

    MANUEJB ejb = null;

    javax.naming.Context initialContext = null;

    try {

        initialContext = new InitialContext();

    } catch (NamingException e) {
        e.printStackTrace();
    }

    try {

        ejb = (MANUEJB) initialContext.lookup("java:app/cos-arq-test/MANUEJB");

    } catch (NamingException e) {
        e.printStackTrace();
    }

    String result = ejb.method(name);

    return Response.ok(result).build();
  }
}

但是我遇到了错误,无法找到该服务。

我使用 Arquillian 库与一个受控的 Wildfly 10 容器:

    <dependency>
         <groupId>org.jboss.arquillian</groupId>
          <artifactId>arquillian-bom</artifactId>
          <version>1.1.11.Final</version>
          <scope>import</scope>
          <type>pom</type>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>

    <dependency>
        <groupId>org.jboss.arquillian.junit</groupId>
        <artifactId>arquillian-junit-container</artifactId>
    </dependency>

    <dependency>
        <groupId>org.wildfly.arquillian</groupId>
        <artifactId>wildfly-arquillian-container-managed</artifactId>
        <version>2.0.0.Final</version>
    </dependency>

以下是我的容器的Arquillian配置:

<arquillian xmlns="http://jboss.org/schema/arquillian"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">

<container qualifier="wildfly10" default="true">
    <configuration>
        <property name="jbossHome">/home/manumg/test-containers/wildfly-10.1.0.Final</property>
    </configuration>
</container>


你是否收到了异常?是因为服务器找不到你的服务,还是客户端无法定位REST服务?请分享异常信息。 - undefined
抱歉,我用 @ApplicationPath("/services") 解决了这个问题。public class JaxRsActivator extends Application {} - undefined
1个回答

2

一个简单的例子,您可以参考https://github.com/arquillian/arquillian-extension-rest/blob/master/rest-client/README.md

自述文件中描述的代码在同一存储库的https://github.com/arquillian/arquillian-extension-rest/tree/master/rest-client/test-app处。

对于初学者的maven项目,建议使用与Wildfly 21(jakarta ee 8容器)相关的maven原型wildfly-jakartaee-webapp-archetype。

要运行测试,您需要:

  1. 激活arq-managed配置文件
  2. 下载Wildfly 21(从https://www.wildfly.org/downloads/
  3. 定义JBOSS_HOME变量(指向本地Wildfly目录)

谢谢。这是简单的休息测试 :) - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接