From 6833b2b0c51cc3696480f69f88bd255b4c62c206 Mon Sep 17 00:00:00 2001 From: usbharu <i@usbharu.dev> Date: Thu, 15 May 2025 00:55:04 +0900 Subject: [PATCH] =?UTF-8?q?Spring=20Data=20Neo4J=E3=81=AE=E8=A8=98?= =?UTF-8?q?=E4=BA=8B=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- content/posts/2025-05-15/index.md | 129 ++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 content/posts/2025-05-15/index.md diff --git a/content/posts/2025-05-15/index.md b/content/posts/2025-05-15/index.md new file mode 100644 index 0000000..8273f78 --- /dev/null +++ b/content/posts/2025-05-15/index.md @@ -0,0 +1,129 @@ +--- +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も使えると思う \ No newline at end of file