Skip to content
This repository was archived by the owner on Sep 1, 2023. It is now read-only.

Commit fc208fd

Browse files
Fix type and ordering of examples.
1 parent 166cf9b commit fc208fd

File tree

3 files changed

+62
-62
lines changed

3 files changed

+62
-62
lines changed

examples/testing-with-neo4j-harness/README.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ To use Neo4j 4.0 as in the following examples, please add this to your build fil
7777
Be aware that the type of the 4.0 test harness is `org.neo4j.harness.Neo4j`.
7878
The following examples use 4.0 (`org.neo4j.harness.Neo4j`) but are applicable to `ServerControlls`, too.
7979

80-
=== Starting up the Neo4j harness and making Spring aware
80+
=== Starting up the Neo4j harness and making Spring aware of it
8181

8282
There many different options.
8383
The main question is always: *How to make Spring Boot aware that it should use different configuration properties?*
8484

8585
[[option1]]
8686
==== Option 1: Add the embedded server as a Spring bean (recommended approach)
8787

88-
This is shown have this as `MoviesServiceAlt2Test`.
88+
The recommended approach is shown in `MoviesServiceAlt1Test`:
8989

9090
[source,java]
9191
[[simple-example]]

examples/testing-with-neo4j-harness/src/test/java/org/neo4j/doc/driver/springframework/boot/simple/MoviesServiceAlt2Test.java

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,64 +20,32 @@
2020

2121
import static org.assertj.core.api.Assertions.*;
2222

23-
import org.junit.jupiter.api.AfterAll;
24-
import org.junit.jupiter.api.BeforeAll;
23+
import org.junit.jupiter.api.BeforeEach;
2524
import org.junit.jupiter.api.Test;
26-
import org.neo4j.driver.springframework.boot.test.autoconfigure.Neo4jTestHarnessAutoConfiguration;
2725
import org.neo4j.harness.Neo4j;
28-
import org.neo4j.harness.Neo4jBuilders;
2926
import org.springframework.beans.factory.annotation.Autowired;
30-
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3127
import org.springframework.boot.test.context.SpringBootTest;
32-
import org.springframework.boot.test.util.TestPropertyValues;
33-
import org.springframework.context.ApplicationContextInitializer;
34-
import org.springframework.context.ConfigurableApplicationContext;
35-
import org.springframework.test.context.ContextConfiguration;
3628

3729
/**
38-
* This variant uses a custom {@link ApplicationContextInitializer} that modifies Springs configuration properties
39-
* with the help of {@link TestPropertyValues}. Thus, the autoconfiguration of the driver is kept and all other things
40-
* are as you'd expect in production.
41-
* <p>If you don't like that setup, look at {@link MoviesServiceAlt1Test}. Here, we expose the embedded server as a Spring Bean
42-
* and don't do the manual connection setting.
30+
* Example on how one could use the automatically provided test harness instance.
31+
*
32+
* @author Michael J. Simons
4333
*/
4434
@SpringBootTest
45-
@EnableAutoConfiguration(exclude = { Neo4jTestHarnessAutoConfiguration.class })
46-
@ContextConfiguration(initializers = { MoviesServiceAlt2Test.Initializer.class })
47-
class MoviesServiceAlt2Test {
48-
49-
private static Neo4j embeddedDatabaseServer;
50-
51-
@BeforeAll
52-
static void initializeNeo4j() {
53-
54-
embeddedDatabaseServer = Neo4jBuilders.newInProcessBuilder()
55-
.withDisabledServer() // No need for http
56-
.withFixture(""
57-
+ "CREATE (TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real World'})\n"
58-
+ "CREATE (TheMatrixReloaded:Movie {title:'The Matrix Reloaded', released:2003, tagline:'Free your mind'})\n"
59-
+ "CREATE (TheMatrixRevolutions:Movie {title:'The Matrix Revolutions', released:2003, tagline:'Everything that has a beginning has an end'})\n"
60-
)
61-
.build();
62-
}
63-
64-
@AfterAll
65-
static void closeNeo4j() {
66-
embeddedDatabaseServer.close();
67-
}
68-
69-
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
70-
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
71-
TestPropertyValues.of(
72-
"org.neo4j.driver.uri=" + embeddedDatabaseServer.boltURI().toString(),
73-
"org.neo4j.driver.authentication.password="
74-
).applyTo(configurableApplicationContext.getEnvironment());
75-
}
76-
}
35+
public class MoviesServiceAlt2Test {
7736

7837
@Autowired
7938
private MoviesService moviesService;
8039

40+
@BeforeEach
41+
void prepareDatabase(@Autowired Neo4j neo4j) {
42+
neo4j.defaultDatabaseService().executeTransactionally(""
43+
+ "CREATE (TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real World'})\n"
44+
+ "CREATE (TheMatrixReloaded:Movie {title:'The Matrix Reloaded', released:2003, tagline:'Free your mind'})\n"
45+
+ "CREATE (TheMatrixRevolutions:Movie {title:'The Matrix Revolutions', released:2003, tagline:'Everything that has a beginning has an end'})\n"
46+
);
47+
}
48+
8149
@Test
8250
void shouldRetrieveMovieTitles() {
8351

examples/testing-with-neo4j-harness/src/test/java/org/neo4j/doc/driver/springframework/boot/simple/MoviesServiceAlt3Test.java

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,64 @@
2020

2121
import static org.assertj.core.api.Assertions.*;
2222

23-
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.AfterAll;
24+
import org.junit.jupiter.api.BeforeAll;
2425
import org.junit.jupiter.api.Test;
26+
import org.neo4j.driver.springframework.boot.test.autoconfigure.Neo4jTestHarnessAutoConfiguration;
2527
import org.neo4j.harness.Neo4j;
28+
import org.neo4j.harness.Neo4jBuilders;
2629
import org.springframework.beans.factory.annotation.Autowired;
30+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2731
import org.springframework.boot.test.context.SpringBootTest;
32+
import org.springframework.boot.test.util.TestPropertyValues;
33+
import org.springframework.context.ApplicationContextInitializer;
34+
import org.springframework.context.ConfigurableApplicationContext;
35+
import org.springframework.test.context.ContextConfiguration;
2836

2937
/**
30-
* Example on how one could use the automatically provided test harness instance.
31-
*
32-
* @author Michael J. Simons
38+
* This variant uses a custom {@link ApplicationContextInitializer} that modifies Springs configuration properties
39+
* with the help of {@link TestPropertyValues}. Thus, the autoconfiguration of the driver is kept and all other things
40+
* are as you'd expect in production.
41+
* <p>If you don't like that setup, look at {@link MoviesServiceAlt1Test}. Here, we expose the embedded server as a Spring Bean
42+
* and don't do the manual connection setting.
3343
*/
3444
@SpringBootTest
35-
public class MoviesServiceAlt3Test {
45+
@EnableAutoConfiguration(exclude = { Neo4jTestHarnessAutoConfiguration.class })
46+
@ContextConfiguration(initializers = { MoviesServiceAlt3Test.Initializer.class })
47+
class MoviesServiceAlt3Test {
3648

37-
@Autowired
38-
private MoviesService moviesService;
49+
private static Neo4j embeddedDatabaseServer;
50+
51+
@BeforeAll
52+
static void initializeNeo4j() {
53+
54+
embeddedDatabaseServer = Neo4jBuilders.newInProcessBuilder()
55+
.withDisabledServer() // No need for http
56+
.withFixture(""
57+
+ "CREATE (TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real World'})\n"
58+
+ "CREATE (TheMatrixReloaded:Movie {title:'The Matrix Reloaded', released:2003, tagline:'Free your mind'})\n"
59+
+ "CREATE (TheMatrixRevolutions:Movie {title:'The Matrix Revolutions', released:2003, tagline:'Everything that has a beginning has an end'})\n"
60+
)
61+
.build();
62+
}
3963

40-
@BeforeEach
41-
void prepareDatabase(@Autowired Neo4j neo4j) {
42-
neo4j.defaultDatabaseService().executeTransactionally(""
43-
+ "CREATE (TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real World'})\n"
44-
+ "CREATE (TheMatrixReloaded:Movie {title:'The Matrix Reloaded', released:2003, tagline:'Free your mind'})\n"
45-
+ "CREATE (TheMatrixRevolutions:Movie {title:'The Matrix Revolutions', released:2003, tagline:'Everything that has a beginning has an end'})\n"
46-
);
64+
@AfterAll
65+
static void closeNeo4j() {
66+
embeddedDatabaseServer.close();
4767
}
4868

69+
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
70+
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
71+
TestPropertyValues.of(
72+
"org.neo4j.driver.uri=" + embeddedDatabaseServer.boltURI().toString(),
73+
"org.neo4j.driver.authentication.password="
74+
).applyTo(configurableApplicationContext.getEnvironment());
75+
}
76+
}
77+
78+
@Autowired
79+
private MoviesService moviesService;
80+
4981
@Test
5082
void shouldRetrieveMovieTitles() {
5183

0 commit comments

Comments
 (0)