官术网_书友最值得收藏!

Test cases

All the tests are put in the test module with the same package name of the code. Mainly the test cases are written for the Controller, but you can even write for Repository for which you'll need to mock the database using Mockito or some other mocking library.

The MockMvc class is the entry point for performing server-side tests. We can perform HTTP requests such as GET, POST, and others on the URLs to test the REST endpoints.

MessageControllerTests: Test cases for the MessageController are written in human readable names using ` while writing function names.

Consider the following example:

    @RunWith(SpringRunner::class)
@SpringBootTest
class MessageControllerTests {

@Autowired lateinit var context: WebApplicationContext
@Autowired lateinit var messageRepository: MessageRepository
@Autowired lateinit var mapper: ObjectMapper

/**
* Entry point to server side tests
*/
lateinit var mockMvc: MockMvc

@Before
fun setup() {
messageRepository.deleteAll()
mockMvc = webAppContextSetup(this.context).build()
}

@Test
fun `Create new message`() {
val message = Message("""We have some dummy message over here
to
perform some testing and I have started to write test
cases for my code"""
.trimMargin()
, Point(0.0, 0.0))
mockMvc.perform(post("/message")
.content(mapper.writeValueAsString(message))
.contentType(APPLICATION_JSON_UTF8))
.andExpect(status().isCreated)
}

@Test
fun `Get all messages`() {
messageRepository.insert(Message("I love Kotlin"))
messageRepository.insert(Message("I love this book",
Point(0.0, 0.0)))
mockMvc.perform(get("/message")
.accept(APPLICATION_JSON_UTF8)).andExpect(status().isOk)
}

@Test
fun `Find messages in the specified region`() {
messageRepository.insert(Message("I love Kotlin", Point(0.0,
0.0)))
messageRepository.insert(Message("I love this book",
Point(1.0, 1.0)))
mockMvc.perform(get("/message/bbox/{xMin},{yMin},{xMax},
{yMax}"
, -1, -1, 2, 2)
.accept(APPLICATION_JSON_UTF8))
.andExpect(status().isOk)
}

@Test
fun `Subscribe to the message`() {
mockMvc.perform(get("/message/subscribe"))
.andExpect(status().isOk)
}

}
主站蜘蛛池模板: 朔州市| 溧水县| 西峡县| 阿拉善盟| 奉化市| 安西县| 石渠县| 比如县| 集安市| 遂溪县| 余江县| 乌鲁木齐县| 钟山县| 镇远县| 仙桃市| 当雄县| 盐亭县| 南丰县| 修武县| 杭州市| 泸州市| 晋州市| 开化县| 威信县| 固阳县| 黄山市| 驻马店市| 崇文区| 泾源县| 崇左市| 昭苏县| 麻栗坡县| 花莲市| 新巴尔虎右旗| 孟州市| 神农架林区| 长春市| 开平市| 长沙县| 视频| 迭部县|