mirror of
https://github.com/Dannecron/spring-boot-demo.git
synced 2025-12-26 00:32:34 +03:00
add feature tests for ProductService
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
19
src/test/kotlin/com/example/demo/BaseFeatureTest.kt
Normal file
19
src/test/kotlin/com/example/demo/BaseFeatureTest.kt
Normal 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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
10
src/test/resources/application_feature.yml
Normal file
10
src/test/resources/application_feature.yml
Normal 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
|
||||
Reference in New Issue
Block a user