Request and Response Specification - Lab 1
1. Creating a generic request specification
Steps
- Create a package
specs
inside thecom.workshop
atsrc/main/java
- Create a class called
SharedRequestSpecs
in thecom.workshop.specs
package - Create a
public static
method calledcpfPathParameter
adding the parameterString cpf
to it - Inside the method build a request specification add the path parameter to it, fix the
cpf
parameter name, and add the method parameter as its value
Tips
The method in step 3 will look like this:
The content of the method in step 4 will look like this:
Expected results
- Just the class creation 😊
Solution
Click to see...
2. Modify the tests in the RestrictionsTest
class
Steps
- Open the
RestrictionsTest
class located atsrc/test/java
in thecom.workshop.restriction
package - In both tests:
- replace the
pathParam()
method by thespec()
method - use the
cpfPathParameter()
from theSharedRequestSpecs
class - don't forget to keep the same data (
cpf
) in both tests
- replace the
- Run the tests
Expected results
- Green test execution where the following verifications will be performed successfully
- status code
- response body
Solution
Click to see...
class RestrictionsTest extends BaseApiConfiguration {
@Test
@DisplayName("Should query CPF without restriction")
void shouldQueryCpfWithoutRestriction() {
given()
.spec(SharedRequestSpecs.cpfPathParameter("1234567890"))
.when()
.get("/restrictions/{cpf}")
.then()
.statusCode(HttpStatus.SC_NOT_FOUND);
}
@Test
void shouldReturnRestriction() {
given()
.spec(SharedRequestSpecs.cpfPathParameter("62648716050"))
.when()
.get("/restrictions/{cpf}")
.then()
.statusCode(HttpStatus.SC_OK)
.body("message", CoreMatchers.is("CPF 62648716050 has a restriction"));
}
}