1
1
package com.rsginer.hackathoon.banks.controller
2
2
3
+ import com.fasterxml.jackson.databind.ObjectMapper
4
+ import com.rsginer.hackathoon.banks.model.Bank
3
5
import org.junit.jupiter.api.Assertions.*
4
6
import org.junit.jupiter.api.DisplayName
5
7
import org.junit.jupiter.api.Nested
@@ -11,16 +13,17 @@ import org.springframework.boot.test.context.SpringBootTest
11
13
import org.springframework.http.MediaType
12
14
import org.springframework.test.web.servlet.MockMvc
13
15
import org.springframework.test.web.servlet.get
16
+ import org.springframework.test.web.servlet.post
14
17
15
18
@SpringBootTest
16
19
@AutoConfigureMockMvc
17
- internal class BankControllerTest {
18
-
19
- @Autowired
20
- lateinit var mockMvc : MockMvc
20
+ internal class BankControllerTest @Autowired constructor(
21
+ val mockMvc : MockMvc ,
22
+ val objectMapper : ObjectMapper
23
+ ) {
21
24
22
25
@Nested
23
- @DisplayName(" getBanks() " )
26
+ @DisplayName(" GET /api/banks " )
24
27
@TestInstance(TestInstance .Lifecycle .PER_CLASS )
25
28
inner class GetBanks {
26
29
@Test
@@ -41,12 +44,10 @@ internal class BankControllerTest {
41
44
}
42
45
}
43
46
}
44
-
45
-
46
47
}
47
48
48
49
@Nested
49
- @DisplayName(" getBank() " )
50
+ @DisplayName(" GET /api/banks/{accountNumber} " )
50
51
@TestInstance(TestInstance .Lifecycle .PER_CLASS )
51
52
inner class GetBank {
52
53
@Test
@@ -74,6 +75,39 @@ internal class BankControllerTest {
74
75
}
75
76
}
76
77
}
77
-
78
+
79
+ @Nested
80
+ @DisplayName(" POST /api/banks" )
81
+ @TestInstance(TestInstance .Lifecycle .PER_CLASS )
82
+ inner class PostNewBank {
83
+
84
+ @Test
85
+ fun `should add new bank` () {
86
+ val newBank = Bank (" 54321" , 34.0 , 2 )
87
+
88
+ mockMvc.post(" /api/banks" ) {
89
+ contentType = MediaType .APPLICATION_JSON
90
+ content = objectMapper.writeValueAsString(newBank)
91
+ }
92
+ .andDo { print () }
93
+ .andExpect {
94
+ status { isCreated() }
95
+ jsonPath(" $.accountNumber" ) { value(" 54321" ) }
96
+ jsonPath(" $.trust" ) { value(" 34.0" ) }
97
+ }
98
+ }
99
+
100
+ @Test
101
+ fun `should return BAD REQUEST if accountNumber already exist` () {
102
+ val invalidBank = Bank (" 12345" , 20.0 , 2 )
103
+
104
+ mockMvc.post(" /api/banks" ) {
105
+ contentType = MediaType .APPLICATION_JSON
106
+ content = objectMapper.writeValueAsString(invalidBank)
107
+ }
108
+ .andDo { print () }
109
+ .andExpect { status { isBadRequest() } }
110
+ }
111
+ }
78
112
79
113
}
0 commit comments