cancel
Showing results for 
Search instead for 
Did you mean: 

Cannot solve issues in methods of my test class for Spring Boot with the usage of Neo4j

sngermiyanoglu
Node Clone

Hi,
I have some other issues in my Spring Boot example with the usage of Neo4j to calculate the Shortest path.
I updated my repository again. I added some new options (many cities with routes instead of two cities with one route between them) for some methods but I couldn't fix them.
Is it possible to look through all test methods in AppApplicationTest class again if you don't mind?

Here are my issues shown below.

1 ) ListAll -> No JSON value $[0].name

2 ) getCityId -> java.lang.NullPointerException: Cannot invoke "com.springshortpath.app.model.City.getId()" because "city" is null

3 ) getCityByName -> org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException: Cannot invoke "com.springshortpath.app.model.City.getId()" because "city" is null

4 ) updateCity -> org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException: Cannot invoke "com.springshortpath.app.model.City.toString()" because "updatedCity" is null

5 ) addRoute -> org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException: Cannot invoke "com.springshortpath.app.model.Route.toString()" because "savedRoute" is null

6 ) getRouteById -> No issue

7 ) listRoutesByCity -> java.lang.AssertionError: No value at JSON path "$[0]['from']"

8 ) updateRoute -> org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException: Cannot invoke "com.springshortpath.app.model.Route.toString()" because "updatedRoute" is null

9 ) deleteRoute -> java.lang.IllegalStateException: Transaction must be open, but has already been closed.

10 ) shortestPath -> java.lang.AssertionError: Status expected:<200 OK> but was:<404 NOT_FOUND>

11 ) shortestPathInTime -> java.lang.AssertionError: Status expected:<200 OK> but was:<404 NOT_FOUND>

12 ) shortestPathNewConnection -> java.lang.AssertionError: Status expected:<200 OK> but was:<404 NOT_FOUND>

13 ) shortestPathInTimeNewConnection -> java.lang.AssertionError: Status expected:<200 OK> but was:<404 NOT_FOUND>

How can I fix it?

Here is my repository: Project Link 

1 ACCEPTED SOLUTION

glilienfield
Ninja
Ninja

I just ran the tests for the latest code. All but two tests passed.

1. listRoutesByCity: In this case, the test returns a list of two items. You assumed the order of the items, which does not match the order in which the data was returned. You are only testing for one, but there are actually two results returned that you should test for, as 'Istanbul' has a route to both 'Ankara' and 'Antalya'. When I ran the tests, 'Antalya' mapped to element '0' and 'Ankara' mapped to element '1'. 

2. addRoute: I found on the assertions testing the cities. You only have two of the answers, but there are actually three cities since you updated your initialize script to create three. 

I fixed these two issues, and all tests ran green. 

Screen Shot 2022-06-27 at 11.35.10 PM.png

View solution in original post

9 REPLIES 9

gerrit_meier
Neo4j
Neo4j

Your test setup is broken. You used the labels (` City1` , ` City2`....) as names... it should be more like this:

 

session.run("MERGE (City1:City{id:$cityId1, name:'Istanbul'})" +
"MERGE (City2:City{id:$cityId2, name:'Ankara'})" +
"MERGE (City3:City{id:$cityId3, name:'Antalya'})" +
"MERGE (City1)-[:ROUTES]->(:Route{id:$routeId1,from:'Istanbul', destination:'Ankara', duration: 2})-[:ROUTES]->(City2)" +
"MERGE (City1)-[:ROUTES]->(:Route{id:$routeId2,from:'Istanbul', destination:'Antalya', duration: 3})-[:ROUTES]->(City3)" +
"MERGE (City2)-[:ROUTES]->(:Route{id:$routeId3,from:'Ankara', destination:'Antalya', duration: 2})-[:ROUTES]->(City3)",

There is one problem with the deleteRoute and this should be more like this, otherwise you would delete the city as well.

@Query("MATCH (route:Route {id: $routeId}) " +
"OPTIONAL MATCH (route)-[r:ROUTES]-(city:City)" +
"DELETE route, r")
void deleteRoute(UUID cityId, UUID routeId);

I leave the rest of the test fixes to you because from the changed setup data, the expectations vs. outcome can easily be spotted and fixed. Something like wrong destination etc.

Thank you for your help.

I have no idea how to fix this issue (Request processing failed; nested exception). I tried to add toString method in CityResponse and RouteResponse but nothing changed.

Where is the problem in the shortest path process? I always get 404 NOT_FOUND error.

I cannot see any 404 problem. All tests are passing besides:
1. addRoute and listRoutesByCity due to wrong expectation but correct result (test needs to get aligned)
2. deleteRoute (wrong query as I have posted above)

Even if I updated my repository, I still couldn't fix NullPointerException and NestedServletException as well as 404 issue for the shortest path

I updated my repository again. I tried to solve the issue coming from addRoute method but I still got a wrong expectation.

I followed this steps to handle with this issue.

1 ) Add a new city in initializeData ()

MERGE (City4:City{id:$cityId4, name:'İzmir'}) 

2 ) Run the addRoute method but I still get the same issue.

For second issue, I have no idea why $[0]['destination'] is expected as Antalya instead of as Ankara .

I added first route for Ankara and second Route for Antalya.

How can I  fix the issues?

glilienfield
Ninja
Ninja

I just ran the tests for the latest code. All but two tests passed.

1. listRoutesByCity: In this case, the test returns a list of two items. You assumed the order of the items, which does not match the order in which the data was returned. You are only testing for one, but there are actually two results returned that you should test for, as 'Istanbul' has a route to both 'Ankara' and 'Antalya'. When I ran the tests, 'Antalya' mapped to element '0' and 'Ankara' mapped to element '1'. 

2. addRoute: I found on the assertions testing the cities. You only have two of the answers, but there are actually three cities since you updated your initialize script to create three. 

I fixed these two issues, and all tests ran green. 

Screen Shot 2022-06-27 at 11.35.10 PM.png

I updated my repository again. All tests are passed. 

Can you check if they all are defined right?

Thank you for your help.

I updated my repository again.

Is it right?

Have you ever checked my repository yet?

Is my last changes right?

Please let me know.