Arquillian to test Jersey implemented rest service with embedded tomcat 7
iboppana Aug 7, 2014 11:04 PMHi
I am trying to test Jersey implemented rest service with Arquillian and embedded tomcat.
In my example instead of applying JAX-RS annotations directly on the Java class that
implements our service we defined a Java interface that contains all our JAX-RS annotation metadata.
@Path( "HelloService" )
public interface HelloService
{
@Path( "sayHello/{who}" )
@GET
@Produces( { MediaType.TEXT_PLAIN } )
String sayHello( @PathParam( "who" ) String who );
}
public class DefaultHelloService
implements HelloService
{
public String sayHello( String who )
{
return "Hello " + who;
}
}
In the arquillian test
@Deployment(testable = false)
public static WebArchive createDeployment() {
WebArchive jar = ShrinkWrap.create(WebArchive.class, "ROOT.war")
.addClass(HelloService.class)
.addClass(DefaultHelloService.class)
.addAsManifestResource("arquillian.xml")
.setWebXML("in-container-web.xml");
System.out.println(jar.toString(true));
return jar;
}
@Test
public void testHelloService() throws Exception {
try {String who = "test";
Client c = ClientBuilder.newClient();
WebTarget target = c.target(
baseURL + "webapi/HelloService/sayHello/{who}")
.resolveTemplate("who", who);
System.out.println(target.getUri());
String responseMsg = target.request().get(String.class);
Assert.assertEquals("Hello " + who, responseMsg);
}catch(Exception ex){
ex.printStackTrace();
}
}
When I run the test I am getting 505-Internal Server Error . The actual error I am seeing in the trace is
] with root cause
java.lang.NoSuchMethodException: Could not find a suitable constructor in embedd
ed.tomcat.example.api.HelloService class.
at org.glassfish.jersey.internal.inject.JerseyClassAnalyzer.getConstruct
or(JerseyClassAnalyzer.java:189)
at org.jvnet.hk2.internal.Utilities.getConstructor(Utilities.java:183)
at org.jvnet.hk2.internal.Utilities.justCreate(Utilities.java:885)
at org.jvnet.hk2.internal.ServiceLocatorImpl.create(ServiceLocatorImpl.j
ava:872)
Is there a way for me to test this using arqulilian or I should always place the JAX-RS annotations on the implementation class itself.
If I annotate the class directly the test runs fine.
Appreciate your input
Thanks
Indrani