22
33import com .bobocode .config .RootConfig ;
44import com .bobocode .config .WebConfig ;
5+ import com .bobocode .dao .AccountDao ;
56import com .bobocode .dao .impl .InMemoryAccountDao ;
67import com .bobocode .model .Account ;
78import com .bobocode .web .controller .AccountRestController ;
8- import org .junit .jupiter .api .BeforeEach ;
9- import org .junit .jupiter .api .Test ;
9+ import org .junit .jupiter .api .*;
1010import org .springframework .beans .factory .annotation .Autowired ;
1111import org .springframework .http .MediaType ;
1212import org .springframework .test .context .junit .jupiter .web .SpringJUnitWebConfig ;
1616import org .springframework .web .bind .annotation .RestController ;
1717import org .springframework .web .context .WebApplicationContext ;
1818
19- import static org . hamcrest . MatcherAssert . assertThat ;
20- import static org . hamcrest . Matchers . arrayContaining ;
21- import static org .hamcrest . Matchers . arrayWithSize ;
19+ import java . lang . reflect . Constructor ;
20+
21+ import static org .assertj . core . api . AssertionsForClassTypes . assertThat ;
2222import static org .hamcrest .Matchers .hasItems ;
23- import static org .hamcrest .core .IsNull .notNullValue ;
24- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .delete ;
25- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
26- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
27- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .put ;
23+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
24+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
2825import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
2926import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
3027
3128@ SpringJUnitWebConfig (classes = {RootConfig .class , WebConfig .class })
29+ @ TestMethodOrder (MethodOrderer .OrderAnnotation .class )
3230class AccountRestControllerTest {
3331 @ Autowired
3432 private WebApplicationContext applicationContext ;
@@ -45,47 +43,38 @@ void setup() {
4543 }
4644
4745 @ Test
48- void testAccountRestControllerAnnotation () {
46+ @ Order (1 )
47+ @ DisplayName ("AccountRestController is marked as @RestController" )
48+ void accountRestControllerAnnotation () {
4949 RestController restController = AccountRestController .class .getAnnotation (RestController .class );
5050
51- assertThat (restController , notNullValue () );
51+ assertNotNull (restController );
5252 }
5353
5454 @ Test
55- void testAccountRestControllerRequestMapping () {
55+ @ Order (2 )
56+ @ DisplayName ("AccountRestController is annotated and mapped with @RequestMapping" )
57+ void accountRestControllerRequestMapping () {
5658 RequestMapping requestMapping = AccountRestController .class .getAnnotation (RequestMapping .class );
5759
58- assertThat (requestMapping , notNullValue () );
59- assertThat (requestMapping .value (), arrayWithSize ( 1 ) );
60- assertThat (requestMapping .value (), arrayContaining ("/accounts" ) );
60+ assertNotNull (requestMapping );
61+ assertThat (requestMapping .value (). length ). isEqualTo ( 1 );
62+ assertThat (requestMapping .value ()). contains ("/accounts" );
6163 }
6264
6365 @ Test
64- void testHttpStatusCodeOnCreate () throws Exception {
65- mockMvc .perform (
66- post ("/accounts" )
67- .contentType (MediaType .APPLICATION_JSON )
68- .content ("{\" firstName\" :\" Johnny\" , \" lastName\" :\" Boy\" , \" email\" :\" jboy@gmail.com\" }" ))
69- .andExpect (status ().isCreated ());
70- }
71-
72- @ Test
73- void testCreateAccountReturnsAssignedId () throws Exception {
74- mockMvc .perform (
75- post ("/accounts" )
76- .contentType (MediaType .APPLICATION_JSON )
77- .content ("{\" firstName\" :\" Johnny\" , \" lastName\" :\" Boy\" , \" email\" :\" jboy@gmail.com\" }" ))
78- .andExpect (jsonPath ("$.id" ).value (1L ));
79- }
66+ @ Order (3 )
67+ @ DisplayName ("AccountDao is injected using constructor" )
68+ void accountDaoInjection () throws NoSuchMethodException {
69+ Constructor <AccountRestController > constructor = AccountRestController .class .getConstructor ();
8070
81- @ Test
82- void testGetAccountsResponseStatusCode () throws Exception {
83- mockMvc .perform (get ("/accounts" ).accept (MediaType .APPLICATION_JSON_UTF8 ))
84- .andExpect (status ().isOk ());
71+ assertThat (constructor .getParameterTypes ()).contains (AccountDao .class );
8572 }
8673
8774 @ Test
88- void testGetAllAccounts () throws Exception {
75+ @ Order (4 )
76+ @ DisplayName ("Getting all accounts is implemented" )
77+ void getAllAccounts () throws Exception {
8978 Account account1 = create ("Johnny" , "Boy" , "jboy@gmail.com" );
9079 Account account2 = create ("Okko" , "Bay" , "obay@gmail.com" );
9180 accountDao .save (account1 );
@@ -96,16 +85,18 @@ void testGetAllAccounts() throws Exception {
9685 .andExpect (jsonPath ("$.[*].email" ).value (hasItems ("jboy@gmail.com" , "obay@gmail.com" )));
9786 }
9887
99- private Account create ( String firstName , String lastName , String email ) {
100- Account account = new Account ();
101- account . setFirstName ( firstName );
102- account . setLastName ( lastName );
103- account . setEmail ( email );
104- return account ;
88+ @ Test
89+ @ Order ( 5 )
90+ @ DisplayName ( "Getting all accounts response status is OK" )
91+ void getAccountsResponseStatusCode () throws Exception {
92+ mockMvc . perform ( get ( "/accounts" ). accept ( MediaType . APPLICATION_JSON ))
93+ . andExpect ( status (). isOk ()) ;
10594 }
10695
10796 @ Test
108- void testGetById () throws Exception {
97+ @ Order (6 )
98+ @ DisplayName ("Getting account by Id with path variable is implemented" )
99+ void getById () throws Exception {
109100 Account account = create ("Johnny" , "Boy" , "jboy@gmail.com" );
110101 accountDao .save (account );
111102
@@ -118,7 +109,39 @@ void testGetById() throws Exception {
118109 }
119110
120111 @ Test
121- void testRemoveAccount () throws Exception {
112+ @ Order (7 )
113+ @ DisplayName ("Creating account returns corresponding HTTP status" )
114+ void httpStatusCodeOnCreate () throws Exception {
115+ mockMvc .perform (
116+ post ("/accounts" )
117+ .contentType (MediaType .APPLICATION_JSON )
118+ .content ("{\" firstName\" :\" Johnny\" , \" lastName\" :\" Boy\" , \" email\" :\" jboy@gmail.com\" }" ))
119+ .andExpect (status ().isCreated ());
120+ }
121+
122+ @ Test
123+ @ Order (8 )
124+ @ DisplayName ("Creating account returns assigned Id" )
125+ void createAccountReturnsAssignedId () throws Exception {
126+ mockMvc .perform (
127+ post ("/accounts" )
128+ .contentType (MediaType .APPLICATION_JSON )
129+ .content ("{\" firstName\" :\" Johnny\" , \" lastName\" :\" Boy\" , \" email\" :\" jboy@gmail.com\" }" ))
130+ .andExpect (jsonPath ("$.id" ).value (1L ));
131+ }
132+
133+ private Account create (String firstName , String lastName , String email ) {
134+ Account account = new Account ();
135+ account .setFirstName (firstName );
136+ account .setLastName (lastName );
137+ account .setEmail (email );
138+ return account ;
139+ }
140+
141+ @ Test
142+ @ Order (9 )
143+ @ DisplayName ("Removing account is implemented" )
144+ void removeAccount () throws Exception {
122145 Account account = create ("Johnny" , "Boy" , "jboy@gmail.com" );
123146 accountDao .save (account );
124147
@@ -127,14 +150,14 @@ void testRemoveAccount() throws Exception {
127150 }
128151
129152 @ Test
130- void testUpdateAccount () throws Exception {
153+ @ Order (10 )
154+ @ DisplayName ("Updating account is implemented" )
155+ void updateAccount () throws Exception {
131156 Account account = create ("Johnny" , "Boy" , "jboy@gmail.com" );
132157 accountDao .save (account );
133158
134159 mockMvc .perform (put (String .format ("/accounts/%d" , account .getId ())).contentType (MediaType .APPLICATION_JSON )
135160 .content (String .format ("{\" id\" :\" %d\" , \" firstName\" :\" Johnny\" , \" lastName\" :\" Boy\" , \" email\" :\" johnny.boy@gmail.com\" }" , account .getId ())))
136161 .andExpect (status ().isNoContent ());
137162 }
138-
139-
140163}
0 commit comments