diff --git a/README.md b/README.md index 00343d3..768bb3d 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,14 @@ A simple REST application with Kotlin ./gradlew clean build ``` +## Testing Application + +```console +./gradlew clean test +``` + +Minimum coverage is 80%, and report in _build/jacocoHtml/index.html_ + ## Running Application ```console diff --git a/build.gradle b/build.gradle index fe95e65..aaa90dc 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { mainClassName = 'br.com.dev.shoppingcart.AppKt' group = 'br.com.dev' -version = '2.1.2' +version = '2.1.3' repositories { mavenCentral() diff --git a/src/main/kotlin/br/com/dev/shoppingcart/domain/repository/ProductRepository.kt b/src/main/kotlin/br/com/dev/shoppingcart/domain/repository/ProductRepository.kt index c956f16..4671dfb 100644 --- a/src/main/kotlin/br/com/dev/shoppingcart/domain/repository/ProductRepository.kt +++ b/src/main/kotlin/br/com/dev/shoppingcart/domain/repository/ProductRepository.kt @@ -5,7 +5,7 @@ import br.com.dev.shoppingcart.web.dto.ProductDTO class ProductRepository(private val products: MutableList) { - fun findProductById(productId: Long) = products.firstOrNull { + fun findProductById(productId: Long): Product? = products.firstOrNull { it.id == productId } diff --git a/src/test/kotlin/br/com/dev/shoppingcart/domain/repository/CartRepositoryTest.kt b/src/test/kotlin/br/com/dev/shoppingcart/domain/repository/CartRepositoryTest.kt index 8c118f6..a4b2ef1 100644 --- a/src/test/kotlin/br/com/dev/shoppingcart/domain/repository/CartRepositoryTest.kt +++ b/src/test/kotlin/br/com/dev/shoppingcart/domain/repository/CartRepositoryTest.kt @@ -6,7 +6,6 @@ import br.com.dev.shoppingcart.domain.model.Product import io.mockk.impl.annotations.MockK import io.mockk.junit5.MockKExtension import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.ExtendWith @@ -15,20 +14,12 @@ internal class CartRepositoryTest { private lateinit var cut: CartRepository - @MockK - private lateinit var carts: MutableList - @MockK private lateinit var cartProducts: MutableList @MockK private lateinit var products: MutableList - @BeforeEach - fun setUp() { - this.cut = CartRepository(carts, cartProducts, products) - } - @Test fun `given an user id if it exists must return a list of carts`() { // arrange diff --git a/src/test/kotlin/br/com/dev/shoppingcart/domain/repository/ProductRepositoryTest.kt b/src/test/kotlin/br/com/dev/shoppingcart/domain/repository/ProductRepositoryTest.kt new file mode 100644 index 0000000..f517a97 --- /dev/null +++ b/src/test/kotlin/br/com/dev/shoppingcart/domain/repository/ProductRepositoryTest.kt @@ -0,0 +1,70 @@ +package br.com.dev.shoppingcart.domain.repository + +import br.com.dev.shoppingcart.domain.model.Product +import br.com.dev.shoppingcart.mocks.ProductMock +import br.com.dev.shoppingcart.web.dto.ProductDTO +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +internal class ProductRepositoryTest { + + private lateinit var cut: ProductRepository + + @Test + fun `given a product id when a product with that id exists must return it`() { + // arrange + this.cut = ProductRepository(ProductMock.getListWithThreeProducts()) + + // act + val product = this.cut.findProductById(1) + + // assert + assertThat(product).isNotNull + assertThat(product?.id).isEqualTo(1) + } + + @Test + fun `given a product id when a product with that id does not exists must return null`() { + // arrange + this.cut = ProductRepository(ProductMock.getListWithThreeProducts()) + + // act + val product = this.cut.findProductById(100) + + // assert + assertThat(product).isNull() + } + + @Test + fun `given an empty list of products when adding a new one must be of size one and id 1`() { + // arrange + val products = mutableListOf() + this.cut = ProductRepository(products) + + // act + val product = + this.cut.saveProduct(ProductDTO(name = "Test", price = 100.0, description = "", category = "test")) + + // assert + assertThat(product).isNotNull + assertThat(product.id).isEqualTo(1) + + assertThat(products).size().isEqualTo(1) + } + + @Test + fun `given a list of products with one element when adding a new one must be of size two`() { + // arrange + val products = ProductMock.getListWithOneProduct() + + this.cut = ProductRepository(products) + + // act + val product = + this.cut.saveProduct(ProductDTO(name = "Test", price = 100.0, description = "", category = "test")) + + // assert + assertThat(product).isNotNull + assertThat(products).size().isEqualTo(2) + } +} \ No newline at end of file diff --git a/src/test/kotlin/br/com/dev/shoppingcart/mocks/ProductMock.kt b/src/test/kotlin/br/com/dev/shoppingcart/mocks/ProductMock.kt index 9947eb5..05289a1 100644 --- a/src/test/kotlin/br/com/dev/shoppingcart/mocks/ProductMock.kt +++ b/src/test/kotlin/br/com/dev/shoppingcart/mocks/ProductMock.kt @@ -8,9 +8,9 @@ object ProductMock { fun getOneProductWithShorts() = Product(2, "Shorts", 20.00, "", "Vestuário") fun getOneProductWithTenis() = Product(3, "Tênis", 159.00, "", "Vestuário") - fun getListWithOneProduct() = listOf(Product(1, "Camiseta", 100.00, "", "Vestuário")) + fun getListWithOneProduct() = mutableListOf(Product(1, "Camiseta", 100.00, "", "Vestuário")) - fun getListWithThreeProducts() = listOf( + fun getListWithThreeProducts() = mutableListOf( getOneProductWithCamiseta(), getOneProductWithShorts(), getOneProductWithTenis()