--- author: usbharu draft: false categories: - 技術 date: 2025-05-15T00:48:13+09:00 tags: - SpringBoot - Spring Data - Spring Data Neo4j - Kotlin - Neo4j - JUnit5 keywords: - SpringBoot - Spring Data - Spring Data Neo4j - Kotlin - Neo4j - JUnit5 title: Spring Data Neo4jでテストする relpermalink: posts/2025-05-15/ url: posts/2025-05-15/ decription: Spring Data Neo4jでテストする --- ほぼメモ ## 依存関係 ```kotlin configurations { compileOnly { extendsFrom(configurations.annotationProcessor.get()) } all{ exclude(group = "org.slf4j",module ="slf4j-nop") } } dependencies { implementation("org.springframework.boot:spring-boot-starter-actuator") implementation("org.springframework.boot:spring-boot-starter-data-neo4j") implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server") implementation("org.springframework.boot:spring-boot-starter-web") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation("org.jetbrains.kotlin:kotlin-reflect") developmentOnly("org.springframework.boot:spring-boot-devtools") runtimeOnly("io.micrometer:micrometer-registry-prometheus") annotationProcessor("org.springframework.boot:spring-boot-configuration-processor") testImplementation("org.springframework.boot:spring-boot-starter-test") testImplementation("org.springframework.security:spring-security-test") testImplementation("org.neo4j.test:neo4j-harness:2025.04.0"){ exclude(group = "org.neo4j",module="neo4j-slf4j-provider") } testImplementation("org.jetbrains.kotlin:kotlin-test-junit5") testRuntimeOnly("org.junit.platform:junit-platform-launcher") implementation("org.slf4j:slf4j-api:2.0.16") runtimeOnly("ch.qos.logback:logback-classic:1.5.16") } ``` ## 結合テスト ```kotlin @SpringBootTest @AutoConfigureMockMvc @AutoConfigureDataNeo4j class TagControllerTest { @Autowired private lateinit var mockMvc: MockMvc @Test fun tagの作成() { mockMvc .post("/tags") { with(csrf()) contentType = MediaType.APPLICATION_JSON //language=json content = """[ { "id": "test-id", "name": "test-name", "locale": "ja-JP", "parent": "test-parent", "relations": {} }] """.trimIndent() } .andDo { print() } .andExpect { status { is2xxSuccessful() } } } companion object { private lateinit var embeddedDatabaseServer: Neo4j @BeforeAll @JvmStatic fun initializeNeo4j() { embeddedDatabaseServer = Neo4jBuilders.newInProcessBuilder() .withDisabledServer() .build() } @DynamicPropertySource @JvmStatic fun neo4jProperties(registry: DynamicPropertyRegistry) { registry.add("spring.neo4j.uri", Supplier { embeddedDatabaseServer.boltURI() }) registry.add("spring.neo4j.authentication.username", Supplier { "neo4j" }) registry.add("spring.neo4j.authentication.password", Supplier { null }) } @AfterAll @JvmStatic fun stopNeo4j() { embeddedDatabaseServer.close() } } } ``` 試してないけどSpring Data Neo4jのRepositoryも使えると思う