lundi 25 septembre 2017

Arquillian JAX-RS Testing 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://ift.tt/Rj5rcy"
        xmlns:xsi="http://ift.tt/ra1lAU"
        xsi:schemaLocation="http://ift.tt/Rj5rcy http://ift.tt/U5TLp1">

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

Aucun commentaire:

Enregistrer un commentaire