1515
1616import static org .junit .jupiter .api .Assertions .*;
1717
18+ @ Order (10 )
1819@ TestMethodOrder (MethodOrderer .OrderAnnotation .class )
1920@ SpringBootTest (webEnvironment = SpringBootTest .WebEnvironment .DEFINED_PORT )
2021public class CustomerApiTest {
21- public static final String FAARQUARDT_NAME = "Lord Faarquardt" ;
22- public static final Customer FAARQUARDT_CASTLE = new Customer (FAARQUARDT_NAME , "Castle" );
23- public static final Customer FAARQUARDT_HOUSE = new Customer ( FAARQUARDT_NAME , "House" );
22+ public static final String FAARQUARDT_NAME = "Lord Faarquardt" ;
23+ public static final Customer FAARQUARDT = new Customer (FAARQUARDT_NAME ,
24+ List . of ( DepartmentApiTest . CASTLE , DepartmentApiTest . HOUSE ) );
2425
2526 @ Autowired private CustomerRepository customers ;
2627 @ Autowired private TestRestTemplate rest ;
@@ -34,18 +35,31 @@ void contextLoads() {
3435 @ Test
3536 @ Order (10 )
3637 void createCustomers () {
37- for (var customer : List .of (FAARQUARDT_CASTLE , FAARQUARDT_HOUSE )) {
38- var response = rest .postForEntity ("http://localhost:8080/api/customers" , customer , Void .class );
39- assertEquals (201 , response .getStatusCode ().value (), "status code mismatch" );
38+ for (var customer : List .of (FAARQUARDT )) {
39+ var response = rest .postForEntity ("http://localhost:8080/api/customers/create" ,
40+ customer .getName (),
41+ Void .class );
42+ assertEquals (200 , response .getStatusCode ().value (), "status code mismatch" );
4043
41- var result = customers .findById (customer .key ());
44+ var result = customers .findById (customer .getName ());
4245 assertTrue (result .isPresent (), "customer not found" );
4346 assertEquals (customer , result .get ());
4447 }
4548 }
4649
4750 @ Test
4851 @ Order (11 )
52+ void applyDepartments () {
53+ for (var customer : List .of (FAARQUARDT ))
54+ for (var department : customer .getDepartments ()) {
55+ var response = rest .postForEntity ("http://localhost:8080/api/customers/%s/departments" .formatted (
56+ customer .getName ()), department .getName (), Void .class );
57+ assertEquals (200 , response .getStatusCode ().value (), "status code mismatch" );
58+ }
59+ }
60+
61+ @ Test
62+ @ Order (21 )
4963 void fetchNames () {
5064 var response = rest .getForEntity ("http://localhost:8080/api/customers/names" , String [].class );
5165 assertTrue (response .getStatusCode ().is2xxSuccessful (), "request unsuccessful" );
@@ -57,7 +71,7 @@ void fetchNames() {
5771 }
5872
5973 @ Test
60- @ Order (12 )
74+ @ Order (22 )
6175 void fetchDepartments () {
6276 var response = rest .getForEntity ("http://localhost:8080/api/customers/%s/departments" .formatted (FAARQUARDT_NAME ),
6377 String [].class );
0 commit comments