add feature tests for ProductService

This commit is contained in:
Denis Savosin
2024-10-01 16:31:18 +07:00
parent 924969a617
commit 0daff4065d
6 changed files with 87 additions and 2 deletions

View File

@@ -38,6 +38,9 @@ dependencies {
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
testImplementation("org.mockito.kotlin:mockito-kotlin:5.4.0")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.testcontainers:junit-jupiter")
testImplementation("org.testcontainers:testcontainers")
testImplementation("org.testcontainers:postgresql")
}
kotlin {

View File

@@ -11,7 +11,7 @@ import org.springframework.data.relational.core.mapping.Table
import java.time.OffsetDateTime
import java.util.*
@Table(value = "product", schema = "public")
@Table(value = "product")
@Serializable
data class Product(
@Id

View File

@@ -8,7 +8,7 @@ spring:
password: postgres
driver-class-name: org.postgresql.Driver
hikari:
schema: demo
schema: public
flyway: #flyway automatically uses the datasource from the application to connect to the DB
enabled: true # enables flyway database migration
locations: classpath:db/migration/structure, classpath:db/migration/data # the location where flyway should look for migration scripts

View File

@@ -0,0 +1,19 @@
package com.example.demo
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.data.jdbc.DataJdbcTest
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.test.context.ActiveProfiles
import org.testcontainers.junit.jupiter.Testcontainers
@ActiveProfiles("feature")
@DataJdbcTest
@Testcontainers(disabledWithoutDocker = false)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@EnableJdbcRepositories
class BaseFeatureTest {
@Autowired
lateinit var jdbcTemplate: JdbcTemplate
}

View File

@@ -0,0 +1,53 @@
package com.example.demo.services
import com.example.demo.BaseFeatureTest
import com.example.demo.models.Product
import com.example.demo.provider.ProductRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.ContextConfiguration
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
import kotlin.test.assertFalse
@ContextConfiguration(classes = [ProductRepository::class, ProductServiceImpl::class])
class ProductServiceImplTest: BaseFeatureTest() {
@Autowired
private lateinit var productService: ProductServiceImpl
@Autowired
private lateinit var productRepository: ProductRepository
@Test
fun createFindDelete_success() {
val name = "new-product-name"
val price = 33333.toLong()
val description = "some-description"
var product: Product? = null
try {
product = productService.create(name = name, price = price, description = description)
assertNotNull(product.id)
assertEquals(name, product.name)
assertEquals(price, product.price)
assertEquals(333.33, product.getPriceDouble())
val dbProduct = productService.findByGuid(product.guid)
assertNotNull(dbProduct)
assertEquals(product.id, dbProduct.id)
assertFalse(dbProduct.isDeleted())
val deletedProduct = productService.delete(product.guid)
assertNotNull(deletedProduct)
assertEquals(product.id, deletedProduct.id)
assertNotNull(deletedProduct.deletedAt)
assertTrue(deletedProduct.isDeleted())
} finally {
val id = product?.id
if (id != null) {
productRepository.deleteById(id)
}
}
}
}

View File

@@ -0,0 +1,10 @@
---
spring:
datasource:
url: jdbc:tc:postgresql:14-alpine:///test
hikari:
maximum-pool-size: 2
driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
jpa:
hibernate:
ddl-auto: create