I develop some unmanaged extensions for neo4j 2.3.0. Now I want to test the functionality of my code with junit. Is there a way to test my methods locally without an neo4j instance running on my pc?
I want something like this: - create a temporary instance of neo4j before executing test - fill instance with data - call my extension via rest - check the results
I have created a Neo4JTestServer class (using neo4j-harness):
public final class Neo4jTestServer {
public static final String EXTENSION_MOUNT_POINT = "/v1";
public static final String EXTENSION_RESOURCES = "my.company.neo4j.extension";
private static Neo4jTestServer INSTANCE = null;
public static synchronized Neo4jTestServer getInstance() {
if (INSTANCE == null) {
INSTANCE = new Neo4jTestServer();
}
return INSTANCE;
}
private final ServerControls serverControls;
private Neo4jTestServer() {
serverControls = TestServerBuilders.newInProcessBuilder()
.withExtension(EXTENSION_MOUNT_POINT, EXTENSION_RESOURCES)
.newServer();
}
public ServerControls getServerControls() {
return serverControls;
}
public void shutdown() {
serverControls.close();
}
}
And my test class is looking like this:
public class TestResource {
private Neo4jTestServer server;
@Before
public void prepare(){
this.server = Neo4jTestServer.getInstance();
}
@After
public void endup(){
// Shutdown server
this.server.shutdown();
}
@Test
public void test(){
//TODO fill neo4j with data
HTTP.Response response = HTTP.GET(this.server.getServerControls().httpURI().resolve("/v1/calculation/test").toString());
}
}
Here is a link! on the resource i have used.
I also checked the questions from here! but i don't understand if they are running the tests on a 'real' neo4j instance or not.
Can this only be executed on the server or is there a way to run such tests locally?
EDIT: I always get a 500 response calling the rest method.
And this is the output when executing the test:
Nov 04, 2015 10:33:17 AM com.sun.jersey.api.core.PackagesResourceConfig init
INFORMATION: Scanning for root resource and provider classes in the packages:my.company.neo4j.extension
Nov 04, 2015 10:33:17 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFORMATION: Root resource classes found: class my.company.neo4j.extension.Resource
Nov 04, 2015 10:33:17 AM com.sun.jersey.api.core.ScanningResourceConfig init
INFORMATION: No provider classes found.
Nov 04, 2015 10:33:17 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFORMATION: Initiating Jersey application, version 'Jersey: 1.19 02/11/2015 03:25 AM'
Nov 04, 2015 10:33:17 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFORMATION: Initiating Jersey application, version 'Jersey: 1.19 02/11/2015 03:25 AM'
Nov 04, 2015 10:33:17 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFORMATION: Initiating Jersey application, version 'Jersey: 1.19 02/11/2015 03:25 AM'
Nov 04, 2015 10:33:17 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFORMATION: Initiating Jersey application, version 'Jersey: 1.19 02/11/2015 03:25 AM'
Is this "INFORMATION: No provider classes found." a problem maybe?
EDIT - this is my extension (it is just for testing)
@Path("/calculation")
public class ResourceV1 {
@Path("/test")
@GET
public Response test() throws Exception{
return Response.ok().build();
}
}
I also found the neo4j.log file from the temporary database:
2015-11-04 11:52:39.876+0100 INFO [o.n.s.d.LifecycleManagingDatabase] Successfully started database
2015-11-04 11:52:39.896+0100 INFO [o.n.s.CommunityNeoServer] Starting HTTP on port 7474 (4 threads available)
2015-11-04 11:52:40.087+0100 INFO [o.n.s.m.ThirdPartyJAXRSModule] Mounted unmanaged extension [my.company.neo4j.extension] at [/v1]
2015-11-04 11:52:40.156+0100 INFO [o.n.s.w.Jetty9WebServer] Mounting static content at /webadmin
2015-11-04 11:52:40.233+0100 WARN [o.n.s.w.Jetty9WebServer] No static content available for Neo Server at port 7474, management console may not be available.
2015-11-04 11:52:40.252+0100 INFO [o.n.s.w.Jetty9WebServer] Mounting static content at /browser
2015-11-04 11:52:41.267+0100 INFO [o.n.s.CommunityNeoServer] Remote interface ready and available at http://localhost:7474/