2015-03-30 50 views
0

我嘗試運行jax-rs api的unittest,但出現錯誤。我執行的一些細節:JAX-RS Jersey Junit錯誤

控制器!

@Path("/webservices") 
@Produces(MediaType.APPLICATION_JSON) 
public class AController { 

    public AController() { 
     ApplicationContext applicationContext = new AnnotationConfigApplicationContext(
       AConfiguration.class); 
     AutowireCapableBeanFactory acbFactory = applicationContext 
       .getAutowireCapableBeanFactory(); 
     acbFactory.autowireBean(this); 
    } 

    @GET 
    public Response getAll() { 
         something hier... 

測試:

public class AControllerTest { 

    private static final int PORT = 8080; 
    private static final String LOCALHOST = "http://localhost"; 
    private static final String GLOBAL_PATH = "/thesis/webservices/"; 

    private static final URI URI = getBaseURI(); 

    private HttpServer httpServer; 
    private Client client; 

    private static URI getBaseURI() { 
     return UriBuilder.fromUri(LOCALHOST).port(PORT).build(); 
    } 

    @Before 
    public void setUp() throws IllegalArgumentException, NullPointerException, 
      IOException { 
     httpServer = com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory 
       .createHttpServer(URI); 
     httpServer.start(); 
    } 

    @Test 
    public void testGetAll() { 

     client = Client.create(new DefaultClientConfig()); 
     WebResource webResource = client.resource(getBaseURI()); 
     ClientResponse response = webResource.path(GLOBAL_PATH) 
       .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); 
     assertEquals(200, response.getStatus()); 
    } 

    @After() 
    public void stopServer() { 
     httpServer.stop(); 
    } 
} 

<artifactId>thesis</artifactId>在pom.xml

當我運行測試,我得到這個錯誤:

Failed tests: testGetAll(AControllerTest): expected:<200> but was:<404> 
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0 

[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD FAILURE 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 16.466s 
[INFO] Finished at: Mon Mar 30 22:23:05 CEST 2015 
[INFO] Final Memory: 18M/133M 
[INFO] ------------------------------------------------------------------------ 
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.10:test (default-test) on project thesis: There are test failures. 
[ERROR] 
[ERROR] Please refer to pathto/target/surefire-reports for the individual test results. 
[ERROR] -> [Help 1] 
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.10:test (default-test) on project thesis: There are test failures. 

有關於某人的想法問題? 謝謝:-)

+0

那麼,你有一個404,預計200.我建議檢查,看看你是否在端點上提供的東西,你認爲你是 – beresfordt 2015-03-30 20:38:42

+0

你是什麼意思?我使用commando調用了請求(GET),但沒有收到錯誤。 – emoleumassi 2015-03-30 20:44:34

+0

副手它看起來像你的控制器被映射到'/ webservices',但你發送一個請求到'/ thesis/webservices /'。某處需要將灰熊服務器映射到'/ thesis'或將您的請求路徑更改爲'/ webservices'。 – 2015-03-31 00:03:32

回答

0

我解決了它。我改變了/ webservices的路徑,它的工作原理。感謝的