88import org .springframework .boot .test .context .SpringBootTest ;
99import org .springframework .boot .test .web .client .TestRestTemplate ;
1010
11+ import java .util .Arrays ;
1112import java .util .List ;
1213
1314import static org .junit .jupiter .api .Assertions .*;
@@ -18,17 +19,41 @@ public class CustomerApiTest {
1819 public static final Customer FAARQUARDT_CASTLE = new Customer (FAARQUARDT_NAME , "Castle" );
1920 public static final Customer FAARQUARDT_HOUSE = new Customer (FAARQUARDT_NAME , "House" );
2021
21- @ Autowired
22- private CustomerRepository customers ;
23- @ Autowired
24- private TestRestTemplate rest ;
22+ @ Autowired private CustomerRepository customers ;
23+ @ Autowired private TestRestTemplate rest ;
2524
2625 @ Test
2726 @ Order (0 )
2827 void contextLoads () {
2928 assertNotNull (customers );
3029 }
3130
31+ @ Test
32+ @ Order (1 )
33+ void fetchNames () {
34+ var response = rest .getForEntity ("http://localhost:8080/api/customers/names" , String [].class );
35+ assertTrue (response .getStatusCode ().is2xxSuccessful (), "request unsuccessful" );
36+
37+ var data = response .getBody ();
38+ assertNotNull (data );
39+ assertEquals (1 , data .length , "wrong array length" );
40+ assertEquals (FAARQUARDT_NAME , data [0 ], "customer name mismatch" );
41+ }
42+
43+ @ Test
44+ @ Order (2 )
45+ void fetchDepartments () {
46+ var response = rest .getForEntity ("http://localhost:8080/api/customers/%s/departments" .formatted (FAARQUARDT_NAME ),
47+ String [].class );
48+ assertTrue (response .getStatusCode ().is2xxSuccessful (), "request unsuccessful" );
49+
50+ var data = response .getBody ();
51+ assertNotNull (data );
52+ assertEquals (2 , data .length , "wrong array length" );
53+ assertNotEquals (-1 , Arrays .binarySearch (data , "Castle" ), "department not found" );
54+ assertNotEquals (-1 , Arrays .binarySearch (data , "House" ), "department not found" );
55+ }
56+
3257 @ Test
3358 @ Order (10 )
3459 void createCustomers () {
0 commit comments