|
20 | 20 |
|
21 | 21 | import static org.assertj.core.api.Assertions.*; |
22 | 22 |
|
23 | | -import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.AfterAll; |
| 24 | +import org.junit.jupiter.api.BeforeAll; |
24 | 25 | import org.junit.jupiter.api.Test; |
| 26 | +import org.neo4j.driver.springframework.boot.test.autoconfigure.Neo4jTestHarnessAutoConfiguration; |
25 | 27 | import org.neo4j.harness.Neo4j; |
| 28 | +import org.neo4j.harness.Neo4jBuilders; |
26 | 29 | import org.springframework.beans.factory.annotation.Autowired; |
| 30 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
27 | 31 | 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; |
28 | 36 |
|
29 | 37 | /** |
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. |
33 | 43 | */ |
34 | 44 | @SpringBootTest |
35 | | -public class MoviesServiceAlt3Test { |
| 45 | +@EnableAutoConfiguration(exclude = { Neo4jTestHarnessAutoConfiguration.class }) |
| 46 | +@ContextConfiguration(initializers = { MoviesServiceAlt3Test.Initializer.class }) |
| 47 | +class MoviesServiceAlt3Test { |
36 | 48 |
|
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 | + } |
39 | 63 |
|
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(); |
47 | 67 | } |
48 | 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 | + } |
| 77 | + |
| 78 | + @Autowired |
| 79 | + private MoviesService moviesService; |
| 80 | + |
49 | 81 | @Test |
50 | 82 | void shouldRetrieveMovieTitles() { |
51 | 83 |
|
|
0 commit comments