From 0daff4065d01adb1a9eaea74397bce9ad8daecae Mon Sep 17 00:00:00 2001 From: Denis Savosin Date: Tue, 1 Oct 2024 16:31:18 +0700 Subject: [PATCH] add feature tests for ProductService --- build.gradle.kts | 3 ++ .../kotlin/com/example/demo/models/Product.kt | 2 +- src/main/resources/application.yml | 2 +- .../com/example/demo/BaseFeatureTest.kt | 19 +++++++ .../demo/services/ProductServiceImplTest.kt | 53 +++++++++++++++++++ src/test/resources/application_feature.yml | 10 ++++ 6 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 src/test/kotlin/com/example/demo/BaseFeatureTest.kt create mode 100644 src/test/kotlin/com/example/demo/services/ProductServiceImplTest.kt create mode 100644 src/test/resources/application_feature.yml diff --git a/build.gradle.kts b/build.gradle.kts index 6ba44cb..5dae183 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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 { diff --git a/src/main/kotlin/com/example/demo/models/Product.kt b/src/main/kotlin/com/example/demo/models/Product.kt index 7219888..5fe138e 100644 --- a/src/main/kotlin/com/example/demo/models/Product.kt +++ b/src/main/kotlin/com/example/demo/models/Product.kt @@ -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 diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index e44291e..c4d7a1e 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -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 diff --git a/src/test/kotlin/com/example/demo/BaseFeatureTest.kt b/src/test/kotlin/com/example/demo/BaseFeatureTest.kt new file mode 100644 index 0000000..a8e4569 --- /dev/null +++ b/src/test/kotlin/com/example/demo/BaseFeatureTest.kt @@ -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 +} \ No newline at end of file diff --git a/src/test/kotlin/com/example/demo/services/ProductServiceImplTest.kt b/src/test/kotlin/com/example/demo/services/ProductServiceImplTest.kt new file mode 100644 index 0000000..e533ce4 --- /dev/null +++ b/src/test/kotlin/com/example/demo/services/ProductServiceImplTest.kt @@ -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) + } + + } + } +} \ No newline at end of file diff --git a/src/test/resources/application_feature.yml b/src/test/resources/application_feature.yml new file mode 100644 index 0000000..792a5b3 --- /dev/null +++ b/src/test/resources/application_feature.yml @@ -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