Home > OS >  How to mock repository so it doesn't instert or pull data from DB
How to mock repository so it doesn't instert or pull data from DB

Time:11-17

So my issue is that in my SpringBoot REST application im testing my RestController. The problem is that i don't know how to mock the repository so it doesn't get or puts data into the DB. I'm using Kotlin and Mockk for mocking

Here is my Repository

@Repository
interface StandingOrderRepository: CrudRepository<StandingOrder, Int> {

    fun findByNameAndVariableSymbol(name: String, variableSymbol: String): List<StandingOrder>

    fun findByValidFromBetween(fromDate: String, toDate: String): List<StandingOrder>

    fun findByValidFromAfter(fromDate: String) : List<StandingOrder>
}

And here is my Test

    @SpringBootTest
    @AutoConfigureMockMvc
    internal class StandingOrderResourceTest {
    
        @Autowired
        lateinit var mockMvc: MockMvc
        @Autowired
        lateinit var objectMapper: ObjectMapper
    
        private val standingOrderMapper = mockk<StandingOrderMapper>()
        private val standingOrderRepository = mockk<StandingOrderRepository>()
        private val standingOrderServiceImpl = mockk<StandingOrderServiceImpl>()
        private val standingOrderResource = StandingOrderResource(standingOrderServiceImpl)
    
    
        val baseUrl = "/api"
    
        @Nested
        @DisplayName("GetStandingOrders()")
        @TestInstance(TestInstance.Lifecycle.PER_CLASS)
        inner class GetStandingOrders {
            @Test
            fun `should return all StandingOrders`() {
                standingOrderResource.getStandingOrders()
                mockMvc.get(baseUrl)
                    .andDo { print() }
                    .andExpect {
                        status { isOk() }
                        content { contentType(MediaType.APPLICATION_JSON)}
                    }
                //standingOrderResource.getStandingOrders() shouldBe listOf(standingOrderDto)
            }
        }
}

The problem is if i Make a API call or invoke the mocked repository it still gets actual data from DB

CodePudding user response:

In your test code you should try to use method whenever() from org.mockito.kotlin for stubbing StandingOrderRepository's method call. For example your code for stubbing will looks something like this

whenever(standingOrderRepository.findByNameAndVariableSymbol(any(),any())).thenReturn(listOf(StandingOrder(...)))

UPD: So you use Mockk, then you shuold use method every instead whenever from mockito.

CodePudding user response:

So this is how i made it work maybe the issue was on my side how i was trying to use it @Anton Tokmakov was correct here is how i did it

@SpringBootTest
@AutoConfigureMockMvc
@ExtendWith(SpringExtension::class)
internal class StandingOrderResourceTest @Autowired constructor(
    val mockMvc: MockMvc,
    val objectMapper: ObjectMapper,
) {

    @MockkBean
    private lateinit var standingOrderResource: StandingOrderResource

    
    @Nested
    @DisplayName("GetStandingOrders()")
    @TestInstance(TestInstance.Lifecycle.PER_CLASS)
    inner class GetStandingOrders {
        @Test
        fun `should return all StandingOrders`() {
            every { standingOrderResource.getStandingOrders() } returns
                    listOf(standingOrderDto1, standingOrderDto2)
            mockMvc.get(baseUrl)
                .andDo { print() }
                .andExpect {
                    status { isOk() }
                    content { contentType(MediaType.APPLICATION_JSON)}
                }
                .andExpect {
                    jsonPath("\$..[0]", match(MockMvcResultMatchers.content().json(Gson().toJson(
                        listOf(
                            standingOrderDto1,
                            standingOrderDto2
                        )), false)))
                }
        }
    }
  • Related