|
85 | 85 | </div><section style="max-width: 640px; margin-left: auto; margin-right: auto;"> |
86 | 86 | <article> |
87 | 87 | <header><h1>HTTP Calls Separation</h1></header> |
88 | | -<p>Groovy One of the benefits of separating one CRUD scenario into multiple is to be able to run one test at a time. In order to make each test runnable independently we will use createLazyResource . package scenarios.rest.springboot import static org.testingisdocumenting.webtau.WebTauGroovyDsl.* def customerPayload = [firstName: "FN", lastName: "LN"] def customer = createLazyResource("customer") { // lazy resource to be created on the first access def id = http.post("/customers", customerPayload) { return id } return new Customer(id: id, url: "/customers/${id}") // definition is below } scenario("customer create") { customer.id.should != null // accessing resource for the first time will trigger POST (in this example) } scenario("customer read") { http.get(customer.url) { // convenient re-use of url defined above body.should == customerPayload } } scenario("customer update") { def changedLastName = "NLN" http.put(customer.url, [*:customerPayload, lastName: changedLastName]) { lastName.should == changedLastName } http.get(customer.url) { lastName.should == changedLastName } } scenario("customer delete") { http.delete(customer.url) { statusCode.should == 204 } http.get(customer.url) { statusCode.should == 404 } } package scenarios.rest.springboot class Customer { Number id String url // store url of the created entity } Note: to run one scenario at a time use sscenario (additional s in front). groovy-standalone-runner/selective-run Read more Java One of the benefits of separating one CRUD @Test into multiple is to be able to run one test at a time. In order to make each test runnable independently we will leverage BeforeAll , AfterAll , and TestMethodOrder . package com.example.tests.junit5; import org.testingisdocumenting.webtau.http.request.HttpRequestBody; import org.testingisdocumenting.webtau.junit5.WebTau; import org.junit.jupiter.api.*; import static org.testingisdocumenting.webtau.WebTauDsl.*; @WebTau @TestMethodOrder(MethodOrderer.OrderAnnotation.class) // forcing methods execution order @DisplayName("customer CRUD") public class CustomerCrudSeparatedJavaTest { private static final HttpRequestBody customerPayload = http.json( "firstName", "FN", "lastName", "LN"); private static final HttpRequestBody changedCustomerPayload = http.json( "firstName", "FN", "lastName", "NLN"); private static int id; @BeforeAll @DisplayName("create customer") // optional friendly name for reporting purposes public static void createCustomer() { id = http.post("/customers", customerPayload, ((header, body) -> { return body.get("id"); })); actual(id).shouldNot(equal(0)); } @Test @Order(1) @DisplayName("read customer") public void read() { http.get("/customers/" + id, ((header, body) -> { body.should(equal(customerPayload)); })); } @Test @Order(2) // order dependence saves from creating customer on every test @DisplayName("update customer") public void update() { http.put("/customers/" + id, changedCustomerPayload, ((header, body) -> { body.should(equal(changedCustomerPayload)); })); http.get("/customers/" + id, ((header, body) -> { body.should(equal(changedCustomerPayload)); })); } @Test @Order(3) // but you can still run each method independently @DisplayName("delete customer") public void delete() { http.delete("/customers/" + id, ((header, body) -> { header.statusCode.should(equal(204)); })); http.get("/customers/" + id, ((header, body) -> { header.statusCode.should(equal(404)); })); id = -1; // marking as deleted to let cleanup step know that no delete is required } @AfterAll public static void cleanup() { // optional (since we create new ids all the time) step to keep your environment clean if (id == -1) { return; } http.delete("/customers/" + id); } }</p> |
| 88 | +<p>Groovy One of the benefits of separating one CRUD scenario into multiple is to be able to run one test at a time. In order to make each test runnable independently we will use createLazyResource . package scenarios.rest.springboot import static org.testingisdocumenting.webtau.WebTauGroovyDsl.* def customerPayload = [firstName: "FN", lastName: "LN"] def customer = createLazyResource("customer") { // lazy resource to be created on the first access def id = http.post("/customers", customerPayload) { return id } return new Customer(id: id, url: "/customers/${id}") // definition is below } scenario("customer create") { customer.id.should != null // accessing resource for the first time will trigger POST (in this example) } scenario("customer read") { http.get(customer.url) { // convenient re-use of url defined above body.should == customerPayload } } scenario("customer update") { def changedLastName = "NLN" http.put(customer.url, [*:customerPayload, lastName: changedLastName]) { lastName.should == changedLastName } http.get(customer.url) { lastName.should == changedLastName } } scenario("customer delete") { http.delete(customer.url) { statusCode.should == 204 } http.get(customer.url) { statusCode.should == 404 } } package scenarios.rest.springboot class Customer { Number id String url // store url of the created entity } Note: to run one scenario at a time use sscenario (additional s in front). groovy-standalone-runner/selective-run Read more Java One of the benefits of separating one CRUD @Test into multiple is to be able to run one test at a time. In order to make each test runnable independently we will leverage BeforeAll , AfterAll , and TestMethodOrder . package com.example.tests.junit5; import org.testingisdocumenting.webtau.http.request.HttpRequestBody; import org.testingisdocumenting.webtau.junit5.WebTau; import org.junit.jupiter.api.*; import static org.testingisdocumenting.webtau.WebTauDsl.*; @WebTau // annotation to enable rich console output and html reporting @TestMethodOrder(MethodOrderer.OrderAnnotation.class) // forcing methods execution order @DisplayName("customer CRUD") public class CustomerCrudSeparatedJavaTest { private static final HttpRequestBody customerPayload = http.json( "firstName", "FN", "lastName", "LN"); private static final HttpRequestBody changedCustomerPayload = http.json( "firstName", "FN", "lastName", "NLN"); private static int id; @BeforeAll @DisplayName("create customer") // optional friendly name for reporting purposes public static void createCustomer() { id = http.post("/customers", customerPayload, ((header, body) -> { return body.get("id"); })); actual(id).shouldNot(equal(0)); } @Test @Order(1) @DisplayName("read customer") public void read() { http.get("/customers/" + id, ((header, body) -> { body.should(equal(customerPayload)); })); } @Test @Order(2) // order dependence saves from creating customer on every test @DisplayName("update customer") public void update() { http.put("/customers/" + id, changedCustomerPayload, ((header, body) -> { body.should(equal(changedCustomerPayload)); })); http.get("/customers/" + id, ((header, body) -> { body.should(equal(changedCustomerPayload)); })); } @Test @Order(3) // but you can still run each method independently @DisplayName("delete customer") public void delete() { http.delete("/customers/" + id, ((header, body) -> { header.statusCode.should(equal(204)); })); http.get("/customers/" + id, ((header, body) -> { header.statusCode.should(equal(404)); })); id = -1; // marking as deleted to let cleanup step know that no delete is required } @AfterAll public static void cleanup() { // optional (since we create new ids all the time) step to keep your environment clean if (id == -1) { return; } http.delete("/customers/" + id); } }</p> |
89 | 89 | </article> |
90 | 90 |
|
91 | 91 | <article> |
|
230 | 230 | } ] |
231 | 231 | }, { |
232 | 232 | "lang" : "java", |
233 | | - "snippet" : "package com.example.tests.junit5;\n\nimport org.testingisdocumenting.webtau.http.request.HttpRequestBody;\nimport org.testingisdocumenting.webtau.junit5.WebTau;\nimport org.junit.jupiter.api.*;\n\nimport static org.testingisdocumenting.webtau.WebTauDsl.*;\n\n@WebTau\n@TestMethodOrder(MethodOrderer.OrderAnnotation.class) // forcing methods execution order\n@DisplayName(\"customer CRUD\")\npublic class CustomerCrudSeparatedJavaTest {\n private static final HttpRequestBody customerPayload = http.json(\n \"firstName\", \"FN\",\n \"lastName\", \"LN\");\n\n private static final HttpRequestBody changedCustomerPayload = http.json(\n \"firstName\", \"FN\",\n \"lastName\", \"NLN\");\n\n private static int id;\n\n @BeforeAll\n @DisplayName(\"create customer\") // optional friendly name for reporting purposes\n public static void createCustomer() {\n id = http.post(\"/customers\", customerPayload, ((header, body) -> {\n return body.get(\"id\");\n }));\n\n actual(id).shouldNot(equal(0));\n }\n\n @Test\n @Order(1)\n @DisplayName(\"read customer\")\n public void read() {\n http.get(\"/customers/\" + id, ((header, body) -> {\n body.should(equal(customerPayload));\n }));\n }\n\n @Test\n @Order(2) // order dependence saves from creating customer on every test\n @DisplayName(\"update customer\")\n public void update() {\n http.put(\"/customers/\" + id, changedCustomerPayload, ((header, body) -> {\n body.should(equal(changedCustomerPayload));\n }));\n\n http.get(\"/customers/\" + id, ((header, body) -> {\n body.should(equal(changedCustomerPayload));\n }));\n }\n\n @Test\n @Order(3) // but you can still run each method independently\n @DisplayName(\"delete customer\")\n public void delete() {\n http.delete(\"/customers/\" + id, ((header, body) -> {\n header.statusCode.should(equal(204));\n }));\n\n http.get(\"/customers/\" + id, ((header, body) -> {\n header.statusCode.should(equal(404));\n }));\n\n id = -1; // marking as deleted to let cleanup step know that no delete is required\n }\n\n @AfterAll\n public static void cleanup() { // optional (since we create new ids all the time) step to keep your environment clean\n if (id == -1) {\n return;\n }\n\n http.delete(\"/customers/\" + id);\n }\n}", |
| 233 | + "snippet" : "package com.example.tests.junit5;\n\nimport org.testingisdocumenting.webtau.http.request.HttpRequestBody;\nimport org.testingisdocumenting.webtau.junit5.WebTau;\nimport org.junit.jupiter.api.*;\n\nimport static org.testingisdocumenting.webtau.WebTauDsl.*;\n\n@WebTau // annotation to enable rich console output and html reporting\n@TestMethodOrder(MethodOrderer.OrderAnnotation.class) // forcing methods execution order\n@DisplayName(\"customer CRUD\")\npublic class CustomerCrudSeparatedJavaTest {\n private static final HttpRequestBody customerPayload = http.json(\n \"firstName\", \"FN\",\n \"lastName\", \"LN\");\n\n private static final HttpRequestBody changedCustomerPayload = http.json(\n \"firstName\", \"FN\",\n \"lastName\", \"NLN\");\n\n private static int id;\n\n @BeforeAll\n @DisplayName(\"create customer\") // optional friendly name for reporting purposes\n public static void createCustomer() {\n id = http.post(\"/customers\", customerPayload, ((header, body) -> {\n return body.get(\"id\");\n }));\n\n actual(id).shouldNot(equal(0));\n }\n\n @Test\n @Order(1)\n @DisplayName(\"read customer\")\n public void read() {\n http.get(\"/customers/\" + id, ((header, body) -> {\n body.should(equal(customerPayload));\n }));\n }\n\n @Test\n @Order(2) // order dependence saves from creating customer on every test\n @DisplayName(\"update customer\")\n public void update() {\n http.put(\"/customers/\" + id, changedCustomerPayload, ((header, body) -> {\n body.should(equal(changedCustomerPayload));\n }));\n\n http.get(\"/customers/\" + id, ((header, body) -> {\n body.should(equal(changedCustomerPayload));\n }));\n }\n\n @Test\n @Order(3) // but you can still run each method independently\n @DisplayName(\"delete customer\")\n public void delete() {\n http.delete(\"/customers/\" + id, ((header, body) -> {\n header.statusCode.should(equal(204));\n }));\n\n http.get(\"/customers/\" + id, ((header, body) -> {\n header.statusCode.should(equal(404));\n }));\n\n id = -1; // marking as deleted to let cleanup step know that no delete is required\n }\n\n @AfterAll\n public static void cleanup() { // optional (since we create new ids all the time) step to keep your environment clean\n if (id == -1) {\n return;\n }\n\n http.delete(\"/customers/\" + id);\n }\n}", |
234 | 234 | "commentsType" : "inline", |
235 | 235 | "type" : "Snippet" |
236 | 236 | } ] |
|
285 | 285 | }, { |
286 | 286 | "fit" : true, |
287 | 287 | "imageSrc" : "/webtau/doc-artifacts/reports/report-crud-separated-http-calls.png", |
288 | | - "timestamp" : 1671558351421, |
| 288 | + "timestamp" : 1677178900589, |
289 | 289 | "shapes" : [ ], |
290 | 290 | "width" : 1310.0, |
291 | 291 | "height" : 802.0, |
292 | 292 | "type" : "AnnotatedImage" |
293 | 293 | } ] |
294 | 294 | } ], |
295 | | - "lastModifiedTime" : 1671557712797, |
| 295 | + "lastModifiedTime" : 1677178162240, |
296 | 296 | "tocItem" : { |
297 | 297 | "chapterTitle" : "HTTP", |
298 | 298 | "pageTitle" : "CRUD Separated", |
|
0 commit comments