12 Replies Latest reply on May 14, 2014 6:00 AM by albinjoseph

    Arquillian REST extension

    albinjoseph

      Hello,

       

      I was trying to test a REST service using the rest extention. The tutorial I followed was http://arquillian.org/blog/2013/10/28/arquillian-extension-rest-1-0-0-Alpha1 . But when I run the test case I am getting an exception. The stack trace of that exception is given below.

       

      Caused by: java.lang.NoClassDefFoundError: org/jboss/resteasy/core/ResourceMethod

          Caused by: java.lang.ClassNotFoundException: org.jboss.resteasy.core.ResourceMethod from [Module \"deployment.UnitTest.war:main\" from Service Module Loader]"}}

        at org.jboss.as.controller.client.helpers.standalone.impl.ServerDeploymentPlanResultFuture.getActionResult(ServerDeploymentPlanResultFuture.java:134)

        at org.jboss.as.controller.client.helpers.standalone.impl.ServerDeploymentPlanResultFuture.getResultFromNode(ServerDeploymentPlanResultFuture.java:123)

        at org.jboss.as.controller.client.helpers.standalone.impl.ServerDeploymentPlanResultFuture.get(ServerDeploymentPlanResultFuture.java:85)

        at org.jboss.as.controller.client.helpers.standalone.impl.ServerDeploymentPlanResultFuture.get(ServerDeploymentPlanResultFuture.java:42)

        at org.jboss.as.controller.client.helpers.standalone.ServerDeploymentHelper.deploy(ServerDeploymentHelper.java:55)

        at org.jboss.as.arquillian.container.ArchiveDeployer.deployInternal(ArchiveDeployer.java:77)

        ... 90 more

       

      My pom.xml is given here. http://pastebin.com/xSEyZ60i

       

      Is there a dependency that I am missing?

       

      Regards

      -Albin

        • 1. Re: Arquillian REST extension
          bmajsak

          How does your test code look like?

          • 2. Re: Arquillian REST extension
            albinjoseph

            Thank you for the reply.

             

            My code is available at http://pastebin.com/bkPbc62Y

             

            Please note that I tested without @Before and @BeforeClass method at first, however @ArquillianResteasyResource object was null and my test was throwing an NPE. So I added the @Before and @BeforeClass and after that it started giving the ClassNotFoundException.

             

            Regards

            -Albin

            • 3. Re: Arquillian REST extension
              bmajsak

              Both @Before and @BeforeClass are invoked in the server for your test, therefore you will need to deploy required libraries as well. On the other hand there is no reason to deploy testable archive in this case, as your tests are black-box, so you want to exercise your REST endpoints from the client perspective. Can you try to set @Deployment(testable=false) and share the results? I think if you do it with this approach you won't need @Before and @BeforeClass as resource should be created properly.

               

              Hope that helps.

              • 4. Re: Arquillian REST extension
                albinjoseph

                Thank you for the reply.

                 

                I have removed the @Before and @BeforeClass methods from my test class. Now I have only create method which is annotated with @Deployment(testable=false) and testLoginUser method which is annotated with @Test and @RunAsClient . Now when I run there are no exceptions at the startup. But now I am getting a NullPointerException because

                @ArquillianResteasyResource UserResource userResource1

                is null.

                 

                The attachment RestTest.zip file contains my complete project along with the test case and source service.

                 

                Regards

                -Albin

                • 5. Re: Arquillian REST extension
                  kwintesencja

                  Hi there,

                   

                  as i understood your code userResource is also a server side(white-box) resource and hence will not be available in arquillian client mode .

                   

                  You should deploy userResource as you already did but test it from the client, something like:

                   

                  @Test

                  public void testLogin(@ArquillianResource URL url) {

                              //resteasy client api

                              ClientRequest request = new ClientRequest(url+"rest/user/login/");

                              Userdata userData = new UserData("login","pass");

                              ClientResponse<String> response;

                              Gson gson = new Gson();

                              response = request.body(MediaType.APPLICATION_JSON,gson.toJson(userData)).post(String.class);

                              Assert.assertTrue(response.getStatus()== Response.Status.OK.getStatusCode());         

                              LoginResponse loginResponse = gson.fromJson(response.getEntity(String.class), LoginResponse.class);

                  }

                    

                  the way i implemented the test your login would work with json strings:

                   

                       @POST

                      @Path("/login")

                      public  Response login(String json) {

                           Gson gson = new Gson();

                          UserData data = gson.fromJson(json,UserData.class))

                          LoginResponse response = new LoginResponse();

                          if (data.getUsername().equals(data.getPassword())) {

                              double value = Math.random() * 100000;

                              response.setMessage("Success");

                              response.setLogonId((long) value);

                          } else {

                              response.setLogonId(-1L);

                              response.setMessage("Fail");

                          }

                   

                          return Response.ok().entity(response).build();;

                      }

                  • 6. Re: Arquillian REST extension
                    albinjoseph

                    Thank you Rafael Pestano for your reply. It works. However I think it does not use the Arquillian REST extension. Can we get our unit testing working in the same way mentioned in this post http://arquillian.org/blog/2013/10/28/arquillian-extension-rest-1-0-0-Alpha1 ?

                     

                    Regards

                    -Albin

                    • 7. Re: Re: Arquillian REST extension
                      blabno

                      @albinjoseph how is your current code different from the one in blog?

                      • 8. Re: Arquillian REST extension
                        kwintesencja

                        Sorry Albin, i misread your post...i've never used warp rest module, i'will let the warp guys to help you

                        • 9. Re: Arquillian REST extension
                          albinjoseph

                          Thank you for your reply.

                           

                          @Bernard Labno The code is not different. I suspect, it is something to do with the pom.xml.

                           

                          Regards

                          -Albin

                          • 10. Re: Arquillian REST extension
                            albinjoseph

                            Could anyone please share their working pom.xml file for WildFly? I strongly believe it is an issue with my pom.xml file. If it is not an issue with pom.xml file, it could be a bug.

                            • 11. Re: Arquillian REST extension
                              blabno
                              • 12. Re: Arquillian REST extension
                                albinjoseph

                                @Bernard Labno Thank you very much for your reply. With that pom.xml file, I am able to get the object and there is no NPE anymore. Thanks a lot.

                                 

                                However I am facing another issue now. The login method accept only post and seems like when I call userResource.login it is a GET method. Now my test is failing with the following exception.

                                 

                                javax.ws.rs.NotAllowedException: HTTP 405 Method Not Allowed

                                  at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.handleErrorStatus(ClientInvocation.java:183)

                                  at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:154)

                                  at org.jboss.resteasy.client.jaxrs.internal.proxy.extractors.BodyEntityExtractor.extractEntity(BodyEntityExtractor.java:58)

                                  at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:104)

                                  at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:62)

                                  at com.sun.proxy.$Proxy32.login(Unknown Source)

                                 

                                 

                                How can I post to this method?

                                 

                                Regards

                                -Albin