简体   繁体   中英

Arquillian API REST Example

I cannot found any example for jax-rs testing with arquillian. I use a wildfly 10 managed container.

I am trying to do it by my own, this is my sample code:

@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));
    }

    */
 }

}

And this is my service code:

@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();
  }
}

But I get an error, and it does not find the service.

I use arquillian libraries with a managed wildfly 10 container:

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

And this is my arquillian config for the container:

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

For a starter example, you can see https://github.com/arquillian/arquillian-extension-rest/blob/master/rest-client/README.md .

The code described in the readme is in the same repository at https://github.com/arquillian/arquillian-extension-rest/tree/master/rest-client/test-app .

For a starter maven project, I suggest you using the maven archetype wildfly-jakartaee-webapp-archetype, related to Wildfly 21 (jakarta ee 8 container).

To run the test you have to:

  1. Activate the profile arq-managed
  2. Download Wildfly 21 (from https://www.wildfly.org/downloads/ )
  3. Define the JBOSS_HOME variable (pointing to the local Wildfly directory)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM