メインコンテンツへスキップ

Spring Data Neo4jでテストする

·147 文字·1 分
技術 SpringBoot Spring Data Spring Data Neo4j Kotlin Neo4j JUnit5
目次

ほぼメモ

依存関係
#

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")

}

結合テスト
#

@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も使えると思う