WebTau Runner

Note: Limited features development for JUnit4 runner. Please consider migration to junit5/getting-started JUnit5You can use WebTau http. , graphql. , browser. , cli. , db. methods as in a junit 4 tests, but to enable reporting you need to use @RunWith(WebTauRunner.class) Groovy package com.example.tests.junit4 import org.testingisdocumenting.webtau.junit4.WebTauRunner import org.junit.Test import org.junit.runner.RunWith import static org.testingisdocumenting.webtau.WebTauGroovyDsl.* // convenient single import for DSL methods and props like http @RunWith(WebTauRunner) // runner is required to have this test to be a part of generated html report class CustomerCrudSingleGroovyTest { private def customerPayload = [firstName: 'FN', lastName: 'LN'] private def changedCustomerPayload = [*:customerPayload, lastName: 'NLN'] @Test void crud() { def id = http.post("/customers", customerPayload) { return id } http.get("/customers/$id") { body.should == customerPayload } http.put("/customers/$id", changedCustomerPayload) { body.should == changedCustomerPayload } http.get("/customers/$id") { body.should == changedCustomerPayload } http.delete("/customers/$id") { statusCode.should == 204 } http.get("/customers/$id") { statusCode.should == 404 } } } Java package com.example.tests.junit4; import org.testingisdocumenting.webtau.http.request.HttpRequestBody; import org.testingisdocumenting.webtau.junit4.WebTauRunner; import org.junit.Test; import org.junit.runner.RunWith; import static org.testingisdocumenting.webtau.WebTauDsl.*; // convenient single import for DSL methods and props like http and equal, mapOf, etc @RunWith(WebTauRunner.class) // runner is required to have this test to be a part of generated html report public class CustomerCrudSingleJavaTest { private final HttpRequestBody customerPayload = http.json( "firstName", "FN", "lastName", "LN"); private final HttpRequestBody changedCustomerPayload = http.json( "firstName", "FN", "lastName", "NLN"); @Test public void crud() { int id = http.post("/customers", customerPayload, ((header, body) -> { return body.get("id"); })); http.get("/customers/" + id, ((header, body) -> { body.should(equal(customerPayload)); })); http.put("/customers/" + id, changedCustomerPayload, ((header, body) -> { body.should(equal(changedCustomerPayload)); })); http.get("/customers/" + id, ((header, body) -> { body.should(equal(changedCustomerPayload)); })); http.delete("/customers/" + id, ((header, body) -> { header.statusCode.should(equal(204)); })); http.get("/customers/" + id, ((header, body) -> { header.statusCode.should(equal(404)); })); } }

Before/After

Use @Before , @After standard JUnit 4 annotations to implement init and cleanup logic for each test. Groovy package com.example.tests.junit4 import org.testingisdocumenting.webtau.junit4.WebTauRunner import org.junit.After import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import static org.testingisdocumenting.webtau.WebTauGroovyDsl.* @RunWith(WebTauRunner.class) class CustomerCrudBeforeAfterGroovyTest { private def customerPayload = [firstName: 'FN', lastName: 'LN'] private def changedCustomerPayload = [*:customerPayload, lastName: 'NLN'] private int customerId @Before void "create customer"() { customerId = http.post("/customers", customerPayload) { return id // We deliberately named field as "customerId" to avoid conflict with response field. Alternatively you can use body.id to avoid the conflict } } @Test void "query customer"() { http.get("/customers/$customerId") { body.should == customerPayload } } @Test void "update customer"() { http.put("/customers/$customerId", changedCustomerPayload) { body.should == changedCustomerPayload } http.get("/customers/$customerId") { body.should == changedCustomerPayload } } @After void "delete customer"() { http.delete("/customers/$customerId") { statusCode.should == 204 } http.get("/customers/$customerId") { statusCode.should == 404 } } } Java package com.example.tests.junit4; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.testingisdocumenting.webtau.http.request.HttpRequestBody; import org.testingisdocumenting.webtau.junit4.WebTauRunner; import static org.testingisdocumenting.webtau.WebTauDsl.*; @RunWith(WebTauRunner.class) public class CustomerCrudBeforeAfterJavaTest { private final HttpRequestBody customerPayload = http.json( "firstName", "FN", "lastName", "LN"); private final HttpRequestBody changedCustomerPayload = http.json( "firstName", "FN", "lastName", "NLN"); private int customerId; @Before public void createCustomer() { customerId = http.post("/customers", customerPayload, ((header, body) -> { return body.get("id"); })); } @Test public void queryCustomer() { http.get("/customers/" + customerId, ((header, body) -> { body.should(equal(customerPayload)); })); } @Test public void updateCustomer() { http.put("/customers/" + customerId, changedCustomerPayload, ((header, body) -> { body.should(equal(changedCustomerPayload)); })); http.get("/customers/" + customerId, ((header, body) -> { body.should(equal(changedCustomerPayload)); })); } @After public void deleteCustomer() { http.delete("/customers/" + customerId, (header, body) -> { header.statusCode.should(equal(204)); }); http.get("/customers/" + customerId, ((header, body) -> { header.statusCode.should(equal(404)); })); } }

BeforeClass/AfterClass

Use @BeforeClass and @AfterClass to prepare and cleanup resources required for multiple test methods. Groovy package com.example.tests.junit4 import org.testingisdocumenting.webtau.junit4.WebTauRunner import org.junit.AfterClass import org.junit.BeforeClass import org.junit.Test import org.junit.runner.RunWith import static org.testingisdocumenting.webtau.WebTauGroovyDsl.* @RunWith(WebTauRunner.class) class CustomerQueryGroovyTest { private static def id1 // keep track of created ids to assert and cleanup later private static def id2 private static def id3 @BeforeClass static void createCustomers() { id1 = createCustomer("CQ_FN1", "CQ_LN1") id2 = createCustomer("CQ_FN1", "CQ_LN2") id3 = createCustomer("CQ_FN2", "CQ_LN2") } @Test void queryByFirstName() { http.get("/customers/search/first-name", [name: "CQ_FN1"]) { body.should == ["*id" | "firstName" | "lastName"] { // star(*) marks key column so assertion is order agnostic __________________________________ id1 | "CQ_FN1" | "CQ_LN1" id2 | "CQ_FN1" | "CQ_LN2" } } } @Test void queryByLastName() { http.get("/customers/search/last-name", [name: "CQ_LN2"]) { body.should == ["*id" | "firstName" | "lastName"] { __________________________________ id2 | "CQ_FN1" | "CQ_LN2" id3 | "CQ_FN2" | "CQ_LN2" } } } @AfterClass static void cleanup() { [id1, id2, id3].each { http.delete("/customers/$it") } } private static def createCustomer(String firstName, String lastName) { return http.post("/customers", [firstName: firstName, lastName: lastName]) { id } } } Java package com.example.tests.junit4; import org.testingisdocumenting.webtau.junit4.WebTauRunner; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import java.util.Map; import java.util.stream.Stream; import static org.testingisdocumenting.webtau.WebTauDsl.*; @RunWith(WebTauRunner.class) public class CustomerQueryJavaTest { private static Integer id1; // keep track of created ids to assert and cleanup later private static Integer id2; private static Integer id3; @BeforeClass public static void createCustomers() { id1 = createCustomer("CQ_FN1", "CQ_LN1"); id2 = createCustomer("CQ_FN1", "CQ_LN2"); id3 = createCustomer("CQ_FN2", "CQ_LN2"); } @Test public void queryByFirstName() { http.get("/customers/search/first-name", http.query("name", "CQ_FN1"), (header, body) -> { body.should(equal(table("*id", "firstName", "lastName", // star(*) marks key column so assertion is order agnostic ________________________________, id1, "CQ_FN1" , "CQ_LN1", id2, "CQ_FN1" , "CQ_LN2"))); }); } @Test public void queryByLastName() { http.get("/customers/search/last-name", http.query("name", "CQ_LN2"), (header, body) -> { body.should(equal(table("*id", "firstName", "lastName", ________________________________, id2, "CQ_FN1" , "CQ_LN2", id3, "CQ_FN2" , "CQ_LN2"))); }); } @AfterClass public static void cleanup() { Stream.of(id1, id2, id3).forEach(id -> http.delete("/customers/" + id)); } private static int createCustomer(String firstName, String lastName) { Map<String, Object> payload = map( "firstName", firstName, "lastName", lastName); return http.post("/customers", payload, ((header, body) -> { return body.get("id"); })); } }

Maven Import

<dependency> <groupId>org.testingisdocumenting.webtau</groupId> <artifactId>webtau-junit4</artifactId> <version>2.2</version> </dependency>