- Kotlin Blueprints
- Ashish Belagali Hardik Trivedi Akshay Chordiya
- 244字
- 2021-07-02 21:50:19
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)
}
}
推薦閱讀
- HTML5+CSS3+JavaScript從入門到精通:上冊(微課精編版·第2版)
- 計算思維與算法入門
- 神經(jīng)網(wǎng)絡(luò)編程實戰(zhàn):Java語言實現(xiàn)(原書第2版)
- Practical DevOps
- 零基礎(chǔ)學(xué)單片機C語言程序設(shè)計
- RISC-V體系結(jié)構(gòu)編程與實踐(第2版)
- SQL Server與JSP動態(tài)網(wǎng)站開發(fā)
- 新一代SDN:VMware NSX 網(wǎng)絡(luò)原理與實踐
- 持續(xù)輕量級Java EE開發(fā):編寫可測試的代碼
- 編程改變生活:用Python提升你的能力(進階篇·微課視頻版)
- IoT Projects with Bluetooth Low Energy
- MySQL 8從零開始學(xué)(視頻教學(xué)版)
- Python Programming for Arduino
- 零基礎(chǔ)PHP從入門到精通
- 每個人的Python:數(shù)學(xué)、算法和游戲編程訓(xùn)練營