Added a couple of tests for ImageController

This commit is contained in:
neviyn 2021-05-14 11:16:33 +01:00
parent 8d0a5217a5
commit a3bcade36b

View File

@ -0,0 +1,52 @@
package uk.co.neviyn.booru
import org.junit.jupiter.api.Test
import org.mockito.Mockito
import org.mockito.Mockito.doReturn
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.data.domain.PageImpl
import org.springframework.data.domain.PageRequest
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
@SpringBootTest
@AutoConfigureMockMvc
internal class ImageControllerTest {
@Autowired
lateinit var mockMvc: MockMvc
@MockBean
lateinit var imageRepository: ImageRepository
@Test
fun `get an empty gallery page`(){
mockMvc.perform(MockMvcRequestBuilders.get("/gallery"))
.andExpect(MockMvcResultMatchers.status().isOk)
.andExpect(MockMvcResultMatchers.view().name("gallery"))
.andExpect(MockMvcResultMatchers.model().attribute("imagePage", null))
.andExpect(MockMvcResultMatchers.model().attribute("commonTags", null))
.andExpect(MockMvcResultMatchers.model().attribute("pageNumber", null))
}
@Test
fun `get a gallery page with a single page of images`() {
val images = listOf(
Image(id = 1, filename = "image1.jpg"),
Image(id = 2, filename = "image2.jpg")
)
val page = PageImpl(images)
doReturn(page).`when`(imageRepository).findAll(Mockito.any(PageRequest::class.java))
mockMvc.perform(MockMvcRequestBuilders.get("/gallery"))
.andExpect(MockMvcResultMatchers.status().isOk)
.andExpect(MockMvcResultMatchers.view().name("gallery"))
.andExpect(MockMvcResultMatchers.model().attribute("imagePage", page))
.andExpect(MockMvcResultMatchers.model().attribute("pageNumber", 1))
}
}