Skip to content

Commit

Permalink
Use AbstractSpringBootNoDirtyContextTest and @SpykBean (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
jactor-rises authored May 20, 2024
2 parents 37ae3ba + 57bd0a1 commit 6ffdd71
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 262 deletions.
4 changes: 4 additions & 0 deletions persistence/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ dependencies {
runtimeOnly("org.flywaydb:flyway-core:9.22.0")
runtimeOnly("com.h2database:h2:2.2.224")
}

tasks.test {
jvmArgs("--add-opens", "java.base/java.lang.reflect=ALL-UNNAMED")
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package com.github.jactor.persistence

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.boot.test.web.server.LocalServerPort
import org.springframework.transaction.annotation.Transactional
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.jactor.persistence.repository.AddressRepository
import com.github.jactor.persistence.repository.BlogEntryRepository
import com.github.jactor.persistence.repository.BlogRepository
import com.github.jactor.persistence.repository.GuestBookEntryRepository
import com.github.jactor.persistence.repository.GuestBookRepository
import com.github.jactor.persistence.repository.PersonRepository
import com.github.jactor.persistence.repository.UserRepository
import com.github.jactor.persistence.service.BlogService
import com.github.jactor.persistence.service.GuestBookService
import com.ninjasquad.springmockk.SpykBean
import jakarta.persistence.EntityManager

/**
Expand All @@ -18,17 +26,29 @@ import jakarta.persistence.EntityManager
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Transactional
internal abstract class AbstractSpringBootNoDirtyContextTest {
abstract class AbstractSpringBootNoDirtyContextTest {

@Autowired
protected lateinit var testRestTemplate: TestRestTemplate

@Autowired
protected lateinit var addressRepository: AddressRepository

@Autowired
protected lateinit var blogRepository: BlogRepository

@Autowired
protected lateinit var blogEntryRepository: BlogEntryRepository

@Autowired
protected lateinit var entityManager: EntityManager

@Autowired
protected lateinit var guestBookRepository: GuestBookRepository

@Autowired
protected lateinit var guestBookEntryRepository: GuestBookEntryRepository

@Autowired
protected lateinit var objectMapper: ObjectMapper

Expand All @@ -38,6 +58,21 @@ internal abstract class AbstractSpringBootNoDirtyContextTest {
@Autowired
protected lateinit var userRepository: UserRepository

@SpykBean
protected lateinit var blogServiceSpyk: BlogService

@SpykBean
protected lateinit var guestBookServiceSpyk: GuestBookService

@SpykBean
protected lateinit var personRepositorySpyk: PersonRepository

@SpykBean
protected lateinit var userRepositorySpyk: UserRepository

@LocalServerPort
protected val port = 0

@Value("\${server.servlet.context-path}")
protected lateinit var contextPath: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,13 @@ package com.github.jactor.persistence.controller

import java.util.UUID
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.boot.test.web.server.LocalServerPort
import org.springframework.core.ParameterizedTypeReference
import org.springframework.http.HttpEntity
import org.springframework.http.HttpMethod
import org.springframework.http.HttpStatus
import org.springframework.test.context.junit.jupiter.SpringExtension
import com.github.jactor.persistence.JactorPersistence
import com.github.jactor.persistence.AbstractSpringBootNoDirtyContextTest
import com.github.jactor.persistence.dto.BlogDto
import com.github.jactor.persistence.dto.BlogEntryDto
import com.github.jactor.persistence.service.BlogService
import com.ninjasquad.springmockk.MockkBean
import assertk.assertAll
import assertk.assertThat
import assertk.assertions.isEqualTo
Expand All @@ -28,21 +18,7 @@ import assertk.assertions.isNull
import io.mockk.every
import io.mockk.verify

@ExtendWith(SpringExtension::class)
@SpringBootTest(classes = [JactorPersistence::class], webEnvironment = WebEnvironment.RANDOM_PORT)
internal class BlogControllerTest {
@LocalServerPort
private val port = 0

@Value("\${server.servlet.context-path}")
private lateinit var contextPath: String

@MockkBean
private lateinit var blogServiceMock: BlogService

@Autowired
private lateinit var testRestTemplate: TestRestTemplate

internal class BlogControllerTest: AbstractSpringBootNoDirtyContextTest() {
@Test
fun `should build full path`() {
assertThat(buildFullPath("/somewhere")).isEqualTo("http://localhost:$port/jactor-persistence/somewhere")
Expand All @@ -51,7 +27,7 @@ internal class BlogControllerTest {
@Test
fun `should find a blog`() {
val uuid = UUID.randomUUID().also {
every { blogServiceMock.find(it) } returns BlogDto()
every { blogServiceSpyk.find(it) } returns BlogDto()
}

val blogResponse = testRestTemplate.getForEntity(buildFullPath("/blog/$uuid"), BlogDto::class.java)
Expand All @@ -65,7 +41,7 @@ internal class BlogControllerTest {
@Test
fun `should not find a blog`() {
val uuid = UUID.randomUUID().also {
every { blogServiceMock.find(it) } returns null
every { blogServiceSpyk.find(it) } returns null
}

val blogResponse = testRestTemplate.getForEntity(buildFullPath("/blog/$uuid"), BlogDto::class.java)
Expand All @@ -79,7 +55,7 @@ internal class BlogControllerTest {
@Test
fun `should find a blog entry`() {
val uuid = UUID.randomUUID().also {
every { blogServiceMock.findEntryBy(it) } returns BlogEntryDto()
every { blogServiceSpyk.findEntryBy(it) } returns BlogEntryDto()
}

val blogEntryResponse = testRestTemplate.getForEntity(
Expand All @@ -96,7 +72,7 @@ internal class BlogControllerTest {
@Test
fun `should not find a blog entry`() {
val uuid = UUID.randomUUID().also {
every { blogServiceMock.findEntryBy(it) } returns null
every { blogServiceSpyk.findEntryBy(it) } returns null
}

val blogEntryResponse = testRestTemplate.getForEntity(
Expand All @@ -112,7 +88,7 @@ internal class BlogControllerTest {

@Test
fun `should not find blogs by title`() {
every { blogServiceMock.findBlogsBy("Anything") } returns emptyList()
every { blogServiceSpyk.findBlogsBy("Anything") } returns emptyList()

val blogResponse = testRestTemplate.exchange(
buildFullPath("/blog/title/Anything"), HttpMethod.GET, null, typeIsListOfBlogs()
Expand All @@ -126,7 +102,7 @@ internal class BlogControllerTest {

@Test
fun `should find blogs by title`() {
every { blogServiceMock.findBlogsBy("Anything") } returns listOf(BlogDto())
every { blogServiceSpyk.findBlogsBy("Anything") } returns listOf(BlogDto())

val blogResponse =
testRestTemplate.exchange(buildFullPath("/blog/title/Anything"), HttpMethod.GET, null, typeIsListOfBlogs())
Expand All @@ -144,7 +120,7 @@ internal class BlogControllerTest {
@Test
fun `should not find blog entries by blog id`() {
val uuid = UUID.randomUUID().also {
every { blogServiceMock.findEntriesForBlog(it) } returns emptyList()
every { blogServiceSpyk.findEntriesForBlog(it) } returns emptyList()
}

val blogEntriesResponse = testRestTemplate.exchange(
Expand All @@ -160,7 +136,7 @@ internal class BlogControllerTest {
@Test
fun `should find blog entries by blog id`() {
val uuid = UUID.randomUUID().also {
every { blogServiceMock.findEntriesForBlog(it) } returns listOf(BlogEntryDto())
every { blogServiceSpyk.findEntriesForBlog(it) } returns listOf(BlogEntryDto())
}

val blogEntriesResponse = testRestTemplate.exchange(
Expand All @@ -182,7 +158,7 @@ internal class BlogControllerTest {
val blogDto = BlogDto()
blogDto.id = UUID.randomUUID()

every { blogServiceMock.saveOrUpdate(blogDto) } returns blogDto
every { blogServiceSpyk.saveOrUpdate(blogDto) } returns blogDto

val blogResponse = testRestTemplate.exchange(
buildFullPath("/blog/${blogDto.id}"), HttpMethod.PUT, HttpEntity(blogDto),
Expand All @@ -194,7 +170,7 @@ internal class BlogControllerTest {
assertThat(blogResponse.body).isNotNull()
}

verify { blogServiceMock.saveOrUpdate(blogDto) }
verify { blogServiceSpyk.saveOrUpdate(blogDto) }
}

@Test
Expand All @@ -203,7 +179,7 @@ internal class BlogControllerTest {
val createdDto = BlogDto()
createdDto.id = UUID.randomUUID()

every { blogServiceMock.saveOrUpdate(blogDto) } returns createdDto
every { blogServiceSpyk.saveOrUpdate(blogDto) } returns createdDto

val blogResponse = testRestTemplate.exchange(
buildFullPath("/blog"), HttpMethod.POST, HttpEntity(blogDto),
Expand All @@ -216,15 +192,15 @@ internal class BlogControllerTest {
assertThat(blogResponse.body?.id).isEqualTo(createdDto.id)
}

verify { blogServiceMock.saveOrUpdate(blogDto) }
verify { blogServiceSpyk.saveOrUpdate(blogDto) }
}

@Test
fun `should persist changes to existing blog entry`() {
val blogEntryDto = BlogEntryDto()
blogEntryDto.id = UUID.randomUUID()

every { blogServiceMock.saveOrUpdate(blogEntryDto) } returns blogEntryDto
every { blogServiceSpyk.saveOrUpdate(blogEntryDto) } returns blogEntryDto

val blogEntryResponse = testRestTemplate.exchange(
buildFullPath("/blog/entry/${blogEntryDto.id}"),
Expand All @@ -236,7 +212,7 @@ internal class BlogControllerTest {
assertThat(blogEntryResponse.body).isNotNull()
}

verify { blogServiceMock.saveOrUpdate(blogEntryDto) }
verify { blogServiceSpyk.saveOrUpdate(blogEntryDto) }
}

@Test
Expand All @@ -245,7 +221,7 @@ internal class BlogControllerTest {
val createdDto = BlogEntryDto()
createdDto.id = UUID.randomUUID()

every { blogServiceMock.saveOrUpdate(blogEntryDto) } returns createdDto
every { blogServiceSpyk.saveOrUpdate(blogEntryDto) } returns createdDto

val blogEntryResponse = testRestTemplate.exchange(
buildFullPath("/blog/entry"), HttpMethod.POST, HttpEntity(blogEntryDto), BlogEntryDto::class.java
Expand All @@ -257,7 +233,7 @@ internal class BlogControllerTest {
assertThat(blogEntryResponse.body?.id).isEqualTo(createdDto.id)
}

verify { blogServiceMock.saveOrUpdate(blogEntryDto) }
verify { blogServiceSpyk.saveOrUpdate(blogEntryDto) }
}

private fun buildFullPath(url: String): String {
Expand Down
Loading

0 comments on commit 6ffdd71

Please sign in to comment.