Skip to content
Permalink
Browse files

junit5 before/after all example (#445)

  • Loading branch information...
MykolaGolubyev committed Oct 27, 2019
1 parent 77f73e1 commit fa70af550cb5c92d92fd0e7569fef3ef910a9e43
@@ -37,7 +37,7 @@ class TableDataExtension {
use(TableBuildCategory) {
Closure tableDataCodeToRun = tableDataCode.clone() as Closure
tableDataCodeToRun.delegate = new TableBuildDelegate()
tableDataCodeToRun.resolveStrategy = Closure.DELEGATE_FIRST
tableDataCodeToRun.resolveStrategy = Closure.OWNER_FIRST

tableDataCodeToRun.call()
}
@@ -8,6 +8,15 @@ Groovy: :include-file: com/example/tests/junit5/CustomerCrudSeparatedGroovyTest.
Java: :include-file: com/example/tests/junit5/CustomerCrudSeparatedJavaTest.java {commentsType: "inline"}
```

# BeforeAll/AfterAll

Use `@BeforeAll` and `@AfterAll` to prepare and cleanup resources required for multiple test methods.

```tabs
Groovy: :include-file: com/example/tests/junit5/CustomerQueryGroovyTest.groovy {commentsType: "inline"}
Java: :include-file: com/example/tests/junit5/CustomerQueryJavaTest.java {commentsType: "inline"}
```

# Maven Import

:include-file: maven/junit5-dep.xml
@@ -211,6 +211,10 @@ public void post(String url, Map<String, Object> requestBody, HttpResponseValida
post(url, HttpHeader.EMPTY, new JsonRequestBody(requestBody), new HttpResponseValidatorIgnoringReturn(validator));
}

public void post(String url, Map<String, Object> requestBody) {
post(url, HttpHeader.EMPTY, new JsonRequestBody(requestBody), EMPTY_RESPONSE_VALIDATOR);
}

public <E> E post(String url, HttpQueryParams queryParams, HttpResponseValidatorWithReturn validator) {
return post(url, queryParams, EmptyRequestBody.INSTANCE, validator);
}
@@ -0,0 +1,57 @@
package com.example.tests.junit5

import com.twosigma.webtau.junit5.WebTau
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test

import static com.twosigma.webtau.WebTauDsl.http

@WebTau
@DisplayName("customer query")
class CustomerQueryGroovyTest {
private static def id1 // keep track of created ids to assert and cleanup later
private static def id2
private static def id3

@BeforeAll
@DisplayName("create customers")
static void createCustomers() {
id1 = createCustomer("CQ_FN1", "CQ_LN1")
id2 = createCustomer("CQ_FN1", "CQ_LN2")
id3 = createCustomer("CQ_FN2", "CQ_LN2")
}

@Test
@DisplayName("query by first name")
void queryByFirstName() {
http.get("/customers/search/first-name", [name: "CQ_FN1"]) {
_embedded.customers.should == ["*id" | "firstName" | "lastName"] { // sstar(*) marks key column so assertion is order agnostic
__________________________________
id1 | "CQ_FN1" | "CQ_LN1"
id2 | "CQ_FN1" | "CQ_LN2" }
}
}

@Test
@DisplayName("query by last name")
void queryByLastName() {
http.get("/customers/search/last-name", [name: "CQ_LN2"]) {
_embedded.customers.should == ["*id" | "firstName" | "lastName"] {
__________________________________
id2 | "CQ_FN1" | "CQ_LN2"
id3 | "CQ_FN2" | "CQ_LN2" }
}
}

@AfterAll
@DisplayName("clean up")
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 }
}
}
@@ -0,0 +1,66 @@
package com.example.tests.junit5;

import com.twosigma.webtau.junit5.WebTau;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Map;
import java.util.stream.Stream;

import static com.twosigma.webtau.WebTauDsl.*;

@WebTau
@DisplayName("customer query")
class CustomerQueryJavaTest {
private static Integer id1; // keep track of created ids to assert and cleanup later
private static Integer id2;
private static Integer id3;

@BeforeAll
@DisplayName("create customers")
static void createCustomers() {
id1 = createCustomer("CQ_FN1", "CQ_LN1");
id2 = createCustomer("CQ_FN1", "CQ_LN2");
id3 = createCustomer("CQ_FN2", "CQ_LN2");
}

@Test
@DisplayName("query by first name")
void queryByFirstName() {
http.get("/customers/search/first-name", http.query("name", "CQ_FN1"), (header, body) -> {
body.get("_embedded.customers").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
@DisplayName("query by last name")
void queryByLastName() {
http.get("/customers/search/last-name", http.query("name", "CQ_LN2"), (header, body) -> {
body.get("_embedded.customers").should(equal(table("*id", "firstName", "lastName",
________________________________,
id2, "CQ_FN1" , "CQ_LN2",
id3, "CQ_FN2" , "CQ_LN2")));
});
}

@AfterAll
@DisplayName("clean up")
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 = aMapOf(
"firstName", firstName,
"lastName", lastName);

return http.post("/customers", payload, ((header, body) -> {
return body.get("id");
}));
}
}
@@ -3,11 +3,24 @@
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource;

@Api(tags = "customer")
@RepositoryRestResource(collectionResourceRel = "customers", path = "customers")
public interface CustomerRepository extends JpaRepository<Customer, Long> {
@ApiOperation("find all customers ordered by last name")
Iterable<Customer> findAllByOrderByLastName();

@ApiOperation("find all customers ordered by first name")
Iterable<Customer> findAllByOrderByFirstName();

@ApiOperation("find customers by first name")
@RestResource(path = "first-name")
Iterable<Customer> findByFirstName(@Param("name") String name);

@ApiOperation("find customers by last name")
@RestResource(path = "last-name")
Iterable<Customer> findByLastName(@Param("name") String name);
}

0 comments on commit fa70af5

Please sign in to comment.
You can’t perform that action at this time.