简体   繁体   中英

jersey test framework documentation doesn't work

Trying to build a hello world jersey test. https://jersey.java.net/documentation/2.5.1/test-framework.html makes it look so simple, but when overriding the configure method as documented doesn't work.

From the documentation

package api;

import static org.junit.Assert.assertEquals;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Application;

import org.junit.Test;

import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.core.ResourceConfig;
import com.sun.jersey.test.framework.JerseyTest;
import com.sun.jersey.test.framework.WebAppDescriptor;

public class JerseyTester extends JerseyTest {

@Path("hello")
public static class HelloResource {
    @GET
    public String getHello() {
        return "Hello World!";
    }
}


@Override
protected Application configure() {
    return new ResourceConfig(HelloResource.class);
}

@Test
public void testHelloWorld() {
    WebResource webResource = resource();
    String responseMsg = webResource.path("helloworld").get(String.class);
    assertEquals("Hello World", responseMsg);
}

}

The issue is the configure method override doesn't work - I get the error: "The return type is incompatible with JerseyTest.configure()". I also get the error: "Cannot instantiate the type ResourceConfig" - how can that be when the documentation explicitly says to instantiate it?!

This is so basic I don't know why it wouldn't work. I'm just trying to get a plain-jane jersey endpoint under test.

Here's my dependencies:

dependencies {
compile 'javax.ws.rs:jsr311-api:1.1.1'

compile 'com.sun.jersey:jersey-server:1.19'
compile 'com.sun.jersey:jersey-core:1.19'
compile 'com.sun.jersey:jersey-client:1.19'
compile 'com.sun.jersey:jersey-servlet:1.19'
compile 'com.sun.jersey:jersey-json:1.19'

compile 'com.yammer.metrics:metrics-core:2.2.0'
compile 'com.yammer.metrics:metrics-servlet:2.2.0'
compile 'com.yammer.metrics:metrics-jersey:2.2.0'
compile 'com.yammer.metrics:metrics-graphite:2.2.0'

compile 'log4j:log4j:1.2.16'

testCompile 'junit:junit-dep:4.10'
testCompile 'com.sun.jersey.jersey-test-framework:jersey-test-framework-grizzly2:1.19'
testCompile 'org.slf4j:slf4j-simple:1.6.1'
}

Yeah you're looking at the wrong documentation. The documentation you're looking at is for Jersey 2.x. But you are using Jersey 1.x. You could look at the documentation for 1.x , but there's not much going on there. Best thing to do is look at the source code tests for examples. You can also see another example at the bottom of this answer

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