Better architecture - Lab 2
1. Changing the RestrictionsTest
- shouldQueryCpfWithoutRestriction()
test
- Open the
RestrictionsTest
located in thecom.workshop.restriction
package in thesrc/test/java
folder - Remove the inheritance of the
BaseApiConfiguration
- Comment the current code created in the
shouldQueryCpfWithoutRestriction()
test - Create a
RestrictionsApiService
within the namerestrictionsService
- Call the method
restrictionsService.queryCpf()
, using any CPF (without a restriction) - Associate its response to a boolean
- Add an assertion using the
isTrue()
method from AssertJ - Run the test
Expected results
- Green test execution
Solution
Click to see...
2. Changing the RestrictionsTest
- shouldReturnRestriction()
test
- In the
RestrictionsTest
, comment the current code created in theshouldReturnRestriction()
test - Create a
RestrictionsApiService
within the namerestrictionsService
- Call the method
restrictionsService.queryCpfWithRestriction()
, using any CPF (without a restriction) - Associate its response to the
MessageV1
class fromcom.eliasnogueira.credit.model
package (auto-generated from the OpenAPI spec) - Add an assertion using the
contains()
method from AssertJ, where it's necessary to use thegetMessage()
method from theMessageV1
return - Run the test
Expected results
- Green test execution where the following verifications will be performed successfully
Solution
Click to see...
Tip
You can remove the duplication of the RestrictionsApiService
adding it as a private global variable in the test.
3. Cleaning up BaseApiConfiguration
class
- Open the
BaseApiConfiguration
located in thecom.workshop
package in thesrc/test/java
folder - Remove the usage of:
RestAssured.baseURI = "http://localhost";
RestAssured.basePath = "/api/v1";
RestAssured.port= 8088;
- Add (again) the inheritance of the
BaseApiConfiguration
to theRestrictionsTest
- Run the tests
Expected results
- Green test execution where the following verifications will be performed successfully
- All the request and response logs showing in the console
Solution
Click to see...
class RestrictionsTest extends BaseApiConfiguration {
private RestrictionsApiService restrictionsServices = new RestrictionsApiService();
@Test
void shouldQueryCpfWithoutRestriction() {
boolean isDeleted = restrictionsServices.queryCpf("1234567890");
Assertions.assertThat(isDeleted).isTrue();
}
@Test
void shouldReturnRestriction() {
MessageV1 message = restrictionsServices.queryCpfWithRestriction("60094146012");
Assertions.assertThat(message.getMessage()).contains("60094146012");
}
}