44import static org .hamcrest .CoreMatchers .containsString ;
55import static org .hamcrest .core .IsNot .not ;
66
7+ import org .junit .jupiter .api .MethodOrderer ;
8+ import org .junit .jupiter .api .Order ;
79import org .junit .jupiter .api .Test ;
810
911import io .quarkus .test .junit .QuarkusTest ;
12+ import org .junit .jupiter .api .TestMethodOrder ;
1013
1114@ QuarkusTest
15+ @ TestMethodOrder (MethodOrderer .OrderAnnotation .class )
1216class FruitResourceTest {
17+ private static String newFruit = "Lemon" ;
18+ private static String updatedFruit = "Pitaya" ;
1319
1420 @ Test
15- public void testListAllFruits () {
21+ @ Order (1 )
22+ public void testAddFruit () {
1623 //List all, should have all 4 fruits the database has initially:
1724 given ()
1825 .when ().get ("/fruits" )
@@ -24,22 +31,113 @@ public void testListAllFruits() {
2431 containsString ("Pomelo" ),
2532 containsString ("Lychee" ));
2633
27- //Delete the Kiwi:
34+ // Add new fruit
35+ given ()
36+ .when ().contentType ("application/json" )
37+ .body ("{\" name\" :\" " + newFruit + "\" }" )
38+ .post ("/fruits" )
39+ .then ()
40+ .statusCode (201 );
41+
42+ //List all, should have 4 fruits the database has initially and 1 added fruit:
43+ given ()
44+ .when ().get ("/fruits" )
45+ .then ()
46+ .statusCode (200 )
47+ .body (
48+ containsString ("Kiwi" ),
49+ containsString ("Durian" ),
50+ containsString ("Pomelo" ),
51+ containsString ("Lychee" ),
52+ containsString (newFruit ));
53+ }
54+
55+ @ Test
56+ @ Order (2 )
57+ public void testUpdateFruit () {
58+ //List all, should have 4 fruits the database has initially and 1 added fruit:
59+ given ()
60+ .when ().get ("/fruits" )
61+ .then ()
62+ .statusCode (200 )
63+ .body (
64+ containsString ("Kiwi" ),
65+ containsString ("Durian" ),
66+ containsString ("Pomelo" ),
67+ containsString ("Lychee" ),
68+ containsString (newFruit ));
69+
70+ // Update name of added fruit
71+ given ()
72+ .when ().contentType ("application/json" )
73+ .body ("{\" id\" :5, \" name\" :\" " + updatedFruit + "\" }" )
74+ .put ("/fruits/5" )
75+ .then ()
76+ .statusCode (200 );
77+
78+ //List all, should have 4 fruits the database has initially and 1 updated fruit:
79+ given ()
80+ .when ().get ("/fruits" )
81+ .then ()
82+ .statusCode (200 )
83+ .body (
84+ containsString ("Kiwi" ),
85+ containsString ("Durian" ),
86+ containsString ("Pomelo" ),
87+ containsString ("Lychee" ),
88+ containsString (updatedFruit ));
89+ }
90+
91+ @ Test
92+ @ Order (3 )
93+ public void testListAllFruits () {
94+ //List all, should have 5 fruits the database has initially:
95+ given ()
96+ .when ().get ("/fruits" )
97+ .then ()
98+ .statusCode (200 )
99+ .body (
100+ containsString ("Kiwi" ),
101+ containsString ("Durian" ),
102+ containsString ("Pomelo" ),
103+ containsString ("Lychee" ),
104+ containsString (updatedFruit ));
105+
106+ //Delete the Pitaya:
28107 given ()
29- .when ().delete ("/fruits/1 " )
108+ .when ().delete ("/fruits/5 " )
30109 .then ()
31110 .statusCode (204 );
32111
33- //List all, Kiwi should be missing now:
112+ //List all, Pitaya should be missing now:
34113 given ()
35114 .when ().get ("/fruits" )
36115 .then ()
37116 .statusCode (200 )
38117 .body (
39- not ( containsString ("Kiwi" ) ),
118+ containsString ("Kiwi" ),
40119 containsString ("Durian" ),
41120 containsString ("Pomelo" ),
42- containsString ("Lychee" ));
121+ containsString ("Lychee" ),
122+ not (containsString (updatedFruit )));
123+ }
124+
125+ @ Test
126+ public void testNotFoundForDelete () {
127+ given ()
128+ .when ().delete ("/fruits/3939" )
129+ .then ()
130+ .statusCode (404 );
131+ }
132+
133+ @ Test
134+ public void testNotFoundForUpdate () {
135+ given ()
136+ .when ().contentType ("application/json" )
137+ .body ("{\" id\" :3939, \" name\" :\" " + updatedFruit + "\" }" )
138+ .put ("/fruits/3939" )
139+ .then ()
140+ .statusCode (404 );
43141 }
44142
45143}
0 commit comments