16
16
17
17
## Overview
18
18
19
- This project provides a framework for structuring unit tests following the AAA (Arrange, Act,
20
- Assert) pattern with a focus on clarity and readability. The library offers a fluent API that guides
21
- the developer through each phase, ensuring that only contextually relevant methods are available at
22
- each step. This design choice enhances the ease of writing, maintaining, and understanding tests.
23
- The key goal is to provide a clean separation of concerns within the test, allowing developers to
24
- express their test logic fluently and naturally.
19
+ A fluent testing framework for Spring's MockMvc that enforces the Arrange-Act-Assert (AAA)
20
+ pattern and reduces boilerplate in controller tests.
21
+ The library guides developers through each testing phase with a strongly-typed,
22
+ step-by-step API, ensuring a consistent and intuitive test structure. Common tasks such as
23
+ request setup, ObjectMapper-based serialization, and response assertions are fully abstracted,
24
+ allowing developers to focus on the test logic itself rather than technical overhead.
25
+
26
+ <img src =" images/aaa-mockmvc-example1.png " alt =" aaa-mockmvc-example " />
25
27
26
28
___
27
29
38
40
## Installation
39
41
40
42
To include AAAMockMvc in the project, add the following dependency to the ` pom.xml ` .
41
- The sources can also be downloaded directly within the IDE (e.g., IntelliJ IDEA) to access the
43
+ The sources can also be downloaded directly within the IDE (e.g. IntelliJ IDEA) to access the
42
44
documentation of the classes.
43
45
44
46
``` xml
45
47
46
48
<dependency >
47
49
<groupId >io.github.co-mmer</groupId >
48
50
<artifactId >aaa-mockmvc</artifactId >
49
- <version >1.4.1 </version >
51
+ <version >1.4.2 </version >
50
52
<scope >test</scope >
51
53
</dependency >
52
54
@@ -109,7 +111,7 @@ to `AAAMockMvc`.
109
111
public class AAAMockMvcConfig {
110
112
111
113
@Bean
112
- AAAMockMvc aaaMockMvc (WebApplicationContext context , ObjectMapper objectMapper ) {
114
+ public AAAMockMvc aaaMockMvc (WebApplicationContext context , ObjectMapper objectMapper ) {
113
115
return new AAAMockMvc (context, objectMapper);
114
116
}
115
117
@@ -141,7 +143,7 @@ The framework will use a default ObjectMapper (`new ObjectMapper()`).
141
143
public class AAAMockMvcConfig {
142
144
143
145
@Bean
144
- AAAMockMvc aaaMockMvc (MockMvc mockMvc ) {
146
+ public AAAMockMvc aaaMockMvc (MockMvc mockMvc ) {
145
147
return new AAAMockMvc (mockMvc);
146
148
}
147
149
@@ -177,7 +179,7 @@ specific configurations.
177
179
public class AAAMockMvcConfig {
178
180
179
181
@Bean
180
- AAAMockMvc aaaMockMvc (WebApplicationContext context , ObjectMapper objectMapper ) {
182
+ public AAAMockMvc aaaMockMvc (WebApplicationContext context , ObjectMapper objectMapper ) {
181
183
return new AAAMockMvc (context, objectMapper);
182
184
}
183
185
@@ -304,22 +306,18 @@ In the provided library, every test follows the AAA structure using the followin
304
306
305
307
## Example
306
308
307
- - [ Registration Scenario] ( #registration-scenario )
308
- - [ Test Implementation] ( #test-implementation )
309
-
310
- This example demonstrates a typical registration process, where the user's email address must first
309
+ This example demonstrates a registration process, where the user's email address must first
311
310
be verified by sending a code. After the email verification, the user can proceed with the
312
- registration, including providing the verification code.
313
-
314
- The process is split into two main steps:
311
+ registration, including providing the verification code. The process is split into two main steps:
315
312
316
313
1 . ** Email Verification** : A verification code is sent to the provided email address.
317
314
2 . ** Registration** : After receiving the verification code, the user sends it back to the backend
318
315
for validation, allowing them to complete their registration.
319
316
320
- Below are the necessary code snippets to represent this scenario:
317
+ ### Main Code
321
318
322
- ## Registration Scenario
319
+ <details >
320
+ <summary >Implementation of Registration Scenario</summary >
323
321
324
322
### 1. Verification Response Model
325
323
@@ -414,22 +412,19 @@ public class RegistrationController {
414
412
415
413
```
416
414
417
- ## Test Implementation
415
+ </ details >
418
416
419
- The table below shows the same test scenario written in two different ways. On the left side, the
420
- test is written using ** MockMvc** , which is the traditional way to perform HTTP tests in Spring. On
421
- the right side, the same test is written using the ** AAA-Mock** framework, which is an open-source
422
- testing framework designed to make the test code more readable and concise.
417
+ ### Test Code
423
418
424
- It is important to note that in both examples, the use of helper private methods has been
425
- intentionally avoided to keep the tests straightforward and focused on the core testing logic. This
426
- ensures clarity and helps demonstrate the difference between the two testing approaches .
419
+ The following examples demonstrate the same test scenario written in two different ways. First, the
420
+ test is implemented using MockMvc, the traditional approach for performing HTTP tests in Spring.
421
+ This is followed by the same test written with AAAMockMvc .
427
422
428
- < table >
429
- < tr >
430
- <td>
423
+ To keep the tests straightforward and focused on the core testing logic, the use of private helper
424
+ methods has been intentionally avoided. This ensures clarity and helps highlight the differences
425
+ between the two testing approaches.
431
426
432
- ** Test with MockMvc**
427
+ ### Test with MockMvc
433
428
434
429
``` java
435
430
@@ -441,24 +436,25 @@ private ObjectMapper objectMapper;
441
436
442
437
@Test
443
438
@SneakyThrows
444
- void GIVEN_registration_with_valid_verification_WHEN_createRegistration_THEN_return_status_201 () {
439
+ void GIVEN_valid_code_WHEN_registration_THEN_status_201 () {
445
440
446
441
var verificationRequest = VerificationRequest . builder()
447
442
.firstname(FIRSTNAME )
448
443
.lastname(LASTNAME )
449
444
.mail(EMAIL )
450
445
.build();
451
446
452
- var verificationResponseJson = mockMvc. perform(
453
- MockMvcRequestBuilders . post(POST_CREATE_VERIFICATION )
454
- .contentType(MediaType . APPLICATION_JSON )
455
- .content(objectMapper. writeValueAsString(verificationRequest)))
447
+ var content = MockMvcRequestBuilders . post(SEND_MAIL_VERIFICATION )
448
+ .contentType(MediaType . APPLICATION_JSON )
449
+ .content(objectMapper. writeValueAsString(verificationRequest));
450
+
451
+ var verificationResponseJson = mockMvc. perform(content)
456
452
.andReturn()
457
453
.getResponse()
458
454
.getContentAsString();
459
455
460
- var verificationResponse = objectMapper. readValue(verificationResponseJson,
461
- VerificationResponse . class);
456
+ var verificationResponse = objectMapper. readValue(
457
+ verificationResponseJson, VerificationResponse . class);
462
458
463
459
var registrationRequest = RegistrationRequest . builder()
464
460
.verification(VerificationResponse . builder()
@@ -468,24 +464,22 @@ void GIVEN_registration_with_valid_verification_WHEN_createRegistration_THEN_ret
468
464
.build())
469
465
.build();
470
466
471
- mockMvc. perform(MockMvcRequestBuilders . post(POST_CREATE_REGISTRATION )
472
- .contentType(MediaType . APPLICATION_JSON )
473
- .content(objectMapper. writeValueAsString(registrationRequest)))
474
- .andExpect(status(). isCreated());
467
+ content = MockMvcRequestBuilders . post(POST_CREATE_REGISTRATION )
468
+ .contentType(MediaType . APPLICATION_JSON )
469
+ .content(objectMapper. writeValueAsString(registrationRequest));
470
+
471
+ mockMvc. perform(content). andExpect(status(). isCreated());
475
472
}
476
473
477
474
```
478
475
479
- </td >
480
- <td>
481
-
482
- ** Test with AAA-MockMvc**
476
+ ### Test with AAAMockMvc
483
477
484
478
``` java
485
479
486
480
@Test
487
481
@SneakyThrows
488
- void GIVEN_registration_with_valid_verification_WHEN_createRegistration_THEN_return_status_201 () {
482
+ void GIVEN_valid_code_WHEN_registration_THEN_status_201 () {
489
483
490
484
var verificationRequest = VerificationRequest . builder()
491
485
.firstname(FIRSTNAME )
@@ -495,7 +489,7 @@ void GIVEN_registration_with_valid_verification_WHEN_createRegistration_THEN_ret
495
489
496
490
var verificationResponse = post()
497
491
.arrange()
498
- .arrangeUrl(POST_CREATE_VERIFICATION )
492
+ .arrangeUrl(SEND_VERIFICATION )
499
493
.arrangeBody()
500
494
.arrangeJson(verificationRequest)
501
495
.act()
@@ -514,7 +508,7 @@ void GIVEN_registration_with_valid_verification_WHEN_createRegistration_THEN_ret
514
508
515
509
post()
516
510
.arrange()
517
- .arrangeUrl(POST_CREATE_REGISTRATION )
511
+ .arrangeUrl(CREATE_REGISTRATION )
518
512
.arrangeBody()
519
513
.arrangeJson(registrationRequest)
520
514
.act()
@@ -526,10 +520,6 @@ void GIVEN_registration_with_valid_verification_WHEN_createRegistration_THEN_ret
526
520
527
521
```
528
522
529
- </td >
530
- </tr >
531
- </table >
532
-
533
523
---
534
524
535
525
## License
0 commit comments