|
84 | 84 | <div></div> |
85 | 85 | </div><section style="max-width: 640px; margin-left: auto; margin-right: auto;"> |
86 | 86 | <article> |
87 | | -<header><h1>Lazy Resource</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.junit5.WebTau; import org.junit.jupiter.api.*; import java.util.Map; import static org.testingisdocumenting.webtau.WebTauDsl.*; @WebTau @TestMethodOrder(MethodOrderer.OrderAnnotation.class) // forcing methods execution order @DisplayName("customer CRUD") public class CustomerCrudSeparatedJavaTest { private static final Map<String, ?> customerPayload = aMapOf( "firstName", "FN", "lastName", "LN" ); private static final Map<String, ?> changedCustomerPayload = aMapOf( customerPayload, "lastName", "NLN"); private static int id; @BeforeAll @DisplayName("create customer") // optional friendly name for reporting purposes 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") 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") 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") 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 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> |
| 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> |
89 | 89 | </article> |
90 | 90 |
|
91 | 91 | <article> |
|
108 | 108 | "docMeta" : { |
109 | 109 | "viewOn" : { |
110 | 110 | "link" : "https://github.com/testingisdocumenting/webtau/tree/master/webtau-docs/znai", |
111 | | - "title" : "View On GitHub" |
| 111 | + "title" : "View Markdown" |
112 | 112 | }, |
113 | 113 | "id" : "webtau", |
114 | 114 | "title" : "WebTau", |
115 | 115 | "type" : "Guide", |
116 | 116 | "previewEnabled" : false, |
117 | | - "allowedGroups" : [ "admin" ] |
| 117 | + "support" : { |
| 118 | + "link" : "https://github.com/testingisdocumenting/webtau/discussions" |
| 119 | + } |
118 | 120 | }, |
119 | 121 | "page" : { |
120 | 122 | "type" : "Page", |
121 | 123 | "content" : [ { |
122 | | - "id" : "lazy-resource", |
| 124 | + "id" : "http-calls-separation", |
123 | 125 | "type" : "Section", |
124 | | - "title" : "Lazy Resource", |
| 126 | + "title" : "HTTP Calls Separation", |
125 | 127 | "content" : [ { |
126 | 128 | "tabsContent" : [ { |
127 | 129 | "name" : "Groovy", |
|
225 | 227 | "type" : "SimpleText" |
226 | 228 | } ] |
227 | 229 | }, { |
228 | | - "snippet" : "package com.example.tests.junit5;\n\nimport org.testingisdocumenting.webtau.junit5.WebTau;\nimport org.junit.jupiter.api.*;\n\nimport java.util.Map;\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 Map<String, ?> customerPayload = aMapOf(\n \"firstName\", \"FN\",\n \"lastName\", \"LN\" );\n\n private static final Map<String, ?> changedCustomerPayload = aMapOf(\n customerPayload,\n \"lastName\", \"NLN\");\n\n private static int id;\n\n @BeforeAll\n @DisplayName(\"create customer\") // optional friendly name for reporting purposes\n 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 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 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 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 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}", |
| 230 | + "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}", |
229 | 231 | "commentsType" : "inline", |
230 | 232 | "type" : "Snippet", |
231 | 233 | "lang" : "java" |
|
281 | 283 | }, { |
282 | 284 | "fit" : true, |
283 | 285 | "shapes" : [ ], |
284 | | - "width" : 1200.0, |
| 286 | + "width" : 1310.0, |
285 | 287 | "imageSrc" : "/webtau/doc-artifacts/reports/report-crud-separated-http-calls.png", |
286 | 288 | "type" : "AnnotatedImage", |
287 | 289 | "height" : 802.0, |
288 | | - "timestamp" : 1653578886932 |
| 290 | + "timestamp" : 1658328867830 |
289 | 291 | } ] |
290 | 292 | } ], |
291 | | - "lastModifiedTime" : 1653578199518, |
| 293 | + "lastModifiedTime" : 1658328071382, |
292 | 294 | "tocItem" : { |
293 | 295 | "sectionTitle" : "HTTP", |
294 | 296 | "pageTitle" : "CRUD Separated", |
|
297 | 299 | "fileName" : "CRUD-separated", |
298 | 300 | "viewOnRelativePath" : null, |
299 | 301 | "pageSectionIdTitles" : [ { |
300 | | - "title" : "Lazy Resource", |
301 | | - "id" : "lazy-resource" |
| 302 | + "title" : "HTTP Calls Separation", |
| 303 | + "id" : "http-calls-separation" |
302 | 304 | }, { |
303 | 305 | "title" : "Report", |
304 | 306 | "id" : "report" |
|
0 commit comments