Testing Neo4j managed extensions

蓝咒 提交于 2019-12-06 14:34:06

Correct me if I'm wrong but this sounds like a more general question about testing RESTful APIs.

Since you've tagged junit, you might want to consider something like restfuse which is a RESTful API testing extension for JUnit. If you're already testing your unmanaged extension using the harnesses provided by neo4j, this might be the best fit.

RESTful tests might look something like this:

@RunWith( HttpJUnitRunner.class )
public class RestfuseTest {
  @Rule
  public Destination destination = new Destination( "http://localhost:7474/db/data/ext/NeoPlugin/graphdb/myExt" ); 

  @Context
  private Response response; // will be injected after every request

  @HttpTest( method = Method.GET, path = "/" )
  public void checkRestfuseOnlineStatus() {
    assertOk( response );
  }  
}

For more "retail" testing instead of "wholesale" programmatic testing, I would tend to use the Postman extension on Chrome, which makes crafting requests and inspecting the results really easy (think an easier interface to curl, which you're using now).

Finally FWIW there's a gajillion options for other approaches to testing RESTful APIs. Pick your favorite tech or stack, whether frisby on javascript or vREST.

Michael Hunger

It's a bit hidden in the docs, either you spin up a CommunityNeoServer with ServerBuilder (which lives in org.neo4j.app:neo4j-server:test-jar) or you use the new test-harness which is documented here: http://neo4j.com/docs/stable/_testing_your_extension.html

<dependency>
   <groupId>org.neo4j.test</groupId>
   <artifactId>neo4j-harness</artifactId>
   <version>${neo4j.version}</version>
   <scope>provided</scope>
</dependency>

The test toolkit provides a mechanism to start a Neo4j instance with custom configuration and with extensions of your choice. It also provides mechanisms to specify data fixtures to include when starting Neo4j.

Usage example.

@Path("")
public static class MyUnmanagedExtension
{
    @GET
    public Response myEndpoint()
    {
        return Response.ok().build();
    }
}

@Test
public void testMyExtension() throws Exception
{
    // Given
    try ( ServerControls server = TestServerBuilders.newInProcessBuilder()
            .withExtension( "/myExtension", MyUnmanagedExtension.class )
            .newServer() )
    {
        // When
        HTTP.Response response = HTTP.GET( server.httpURI().resolve( "myExtension" ).toString() );

        // Then
        assertEquals( 200, response.status() );
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!