Skip to content

Commit

Permalink
fix: adiciona teste no repositorio de produto
Browse files Browse the repository at this point in the history
  • Loading branch information
flflima committed Apr 1, 2021
1 parent 4c03c37 commit 32a1d87
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 13 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import br.com.dev.shoppingcart.web.dto.ProductDTO

class ProductRepository(private val products: MutableList<Product>) {

fun findProductById(productId: Long) = products.firstOrNull {
fun findProductById(productId: Long): Product? = products.firstOrNull {
it.id == productId
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -15,20 +14,12 @@ internal class CartRepositoryTest {

private lateinit var cut: CartRepository

@MockK
private lateinit var carts: MutableList<Cart>

@MockK
private lateinit var cartProducts: MutableList<CartProduct>

@MockK
private lateinit var products: MutableList<Product>

@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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Product>()
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)
}
}
4 changes: 2 additions & 2 deletions src/test/kotlin/br/com/dev/shoppingcart/mocks/ProductMock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 32a1d87

Please sign in to comment.