Skip to content

Commit

Permalink
Revert mock removal
Browse files Browse the repository at this point in the history
  • Loading branch information
anotheroneofthese committed Aug 8, 2024
1 parent cbed9ee commit d0a2109
Showing 1 changed file with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package no.nb.mlt.wls.product.controller

import com.ninjasquad.springmockk.MockkBean
import io.mockk.every
import io.mockk.junit5.MockKExtension
import no.nb.mlt.wls.EnableTestcontainers
import no.nb.mlt.wls.core.data.Environment.NONE
Expand All @@ -22,11 +24,16 @@ import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWeb
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.security.test.context.support.WithMockUser
import org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.csrf
import org.springframework.test.web.reactive.server.WebTestClient
import org.springframework.test.web.reactive.server.expectBody
import org.springframework.web.client.HttpClientErrorException
import org.springframework.web.server.ServerErrorException
import java.net.URI

@EnableTestcontainers
@TestInstance(PER_CLASS)
Expand All @@ -35,9 +42,11 @@ import org.springframework.test.web.reactive.server.expectBody
@EnableMongoRepositories("no.nb.mlt.wls")
@SpringBootTest(webEnvironment = RANDOM_PORT)
class ProductControllerTest(
@Autowired val repository: ProductRepository,
@Autowired val synqProductService: SynqProductService
@Autowired val repository: ProductRepository
) {
@MockkBean
private lateinit var synqProductService: SynqProductService

private lateinit var webTestClient: WebTestClient

@BeforeEach
Expand All @@ -55,6 +64,10 @@ class ProductControllerTest(
@Test
@WithMockUser
fun `createProduct with valid payload creates product`() {
every {
synqProductService.createProduct(any())
} returns ResponseEntity.created(URI.create("")).build()

webTestClient
.mutateWith(csrf())
.post()
Expand Down Expand Up @@ -111,6 +124,10 @@ class ProductControllerTest(
@Test
@WithMockUser
fun `createProduct where SynQ says it's a duplicate returns OK`() {
every {
synqProductService.createProduct(any())
} returns ResponseEntity.ok().build()

webTestClient
.mutateWith(csrf())
.post()
Expand All @@ -124,6 +141,16 @@ class ProductControllerTest(
@Test
@WithMockUser
fun `createProduct handles SynQ error`() {
every {
synqProductService.createProduct(any())
}.throws(
ServerErrorException(
"Failed to create product in SynQ, the storage system responded with " +
"error code: '1002' and error text: 'Unknown product category TEST.'",
HttpClientErrorException(HttpStatus.NOT_FOUND, "Not found")
)
)

webTestClient
.mutateWith(csrf())
.post()
Expand Down

0 comments on commit d0a2109

Please sign in to comment.