2016-01-15 30 views
1

我正在嘗試爲Neo4j項目開發一些測試文件。我發現Neo4j提供了JUnit Rule(http://neo4j.com/docs/stable/server-unmanaged-extensions-testing.html),我試圖用它來進行測試。但是,它不能作爲主體。如何使用JUnit測試Neo4j服務器的非託管擴展

我的代碼是完全一樣的Neo4j的幫助頁面:

@Rule 
public Neo4jRule neo4j = new Neo4jRule() 
    .withFixture("CREATE (admin:Admin)") 
    .withFixture(new Function<GraphDatabaseService, Void>() 
    { 
     @Override 
     public Void apply(GraphDatabaseService graphDatabaseService) throws RuntimeException 
     { 
      try (Transaction tx = graphDatabaseService.beginTx()) 
      { 
       graphDatabaseService.createNode(DynamicLabel.label("Admin")); 
       tx.success(); 
      } 
      return null; 
     } 
    }); 

@Test 
public void shouldWorkWithServer() throws Exception 
{ 
    // Given 
    URI serverURI = neo4j.httpURI(); 

    // When I access the server 
    HTTP.Response response = HTTP.GET(serverURI.toString()); 

    // Then it should reply 
    assertEquals(200, response.status()); 
} 

我收到此錯誤:

Failed tests: shouldWorkWithServer(test.Test): expected:<200> but was:<500> 

我已經嘗試解決 'DB /數據/' 路徑:

URI serverURI = neo4j.httpURI().resolve("db/data/"); 

結果是一樣的。我也嘗試使用TestServerBuilder,並使用默認的'neo4j:neo4j'生成Http Authentication頭,並再次得到相同的錯誤。服務器確實創建了,我可以通過getGraphDatabaseService()訪問它並查看節點,但我無法通過HTTP訪問它。

的Neo4j的版本是3.2.1和JUnit的版本是4.11

+0

既然你重新獲得500,應該有一個堆棧跟蹤。我的直覺認爲你缺乏具有分類器測試的'neo4j-io' jar,http://mvnrepository.com/artifact/org.neo4j/neo4j-io/。 –

+3

我確實發現了這個問題。 javax.ws.rs:javax.ws.rs-api:2.0由Neo4j手冊推薦的artefact實際上與org.neo4j.test衝突:neo4j-harness:2.3.1。我已經刪除rs庫,現在一切都神奇地工作。 – Dmitry

+0

@Dmitry感謝您的支持! – gordysc

回答

0

如果使用Maven,不要忘記,你聲明的依賴關係的順序有重要意義。因此,您應該在「測試」之後聲明「提供的」依賴關係,因此在提供的測試庫之前會使用測試庫的實現。對於所有與JEE相關的圖書館來說這是一個很好的做法

在你的情況,一定要聲明javax.ws.rs:javax.ws.rs-api:2.0org.neo4j.test:Neo4j的綜線:2.3.1