Skip to content
Permalink
Browse files

improved support for juni4 before/after + docs (#437)

  • Loading branch information...
MykolaGolubyev committed Oct 17, 2019
1 parent af17399 commit 369c0c44f7479b9f2f7c380df5701af81f274b67
@@ -4,8 +4,17 @@ You can use webtau `http.` and `browser.` methods as in a junit 4 tests, but to
`@RunWith(WebTauRunner.class)`

```tabs
Groovy: :include-file: com/example/tests/junit4/CustomerCrudSingleGroovyTest.groovy
Java: :include-file: com/example/tests/junit4/CustomerCrudSingleJavaTest.java
Groovy: :include-file: com/example/tests/junit4/CustomerCrudSingleGroovyTest.groovy {title: "CRUD test in a single method", commentsType: "inline"}
Java: :include-file: com/example/tests/junit4/CustomerCrudSingleJavaTest.java {title: "CRUD test in a single method", commentsType: "inline"}
```

# Before/After

Use `@Before`, `@After` standard `JUnit 4` annotations to implement init and cleanup logic for each test.

```tabs
Groovy: :include-file: com/example/tests/junit4/CustomerCrudBeforeAfterGroovyTest.groovy {title: "CRUD test with @Before and @After annotations", commentsType: "inline"}
Java: :include-file: com/example/tests/junit4/CustomerCrudBeforeAfterJavaTest.java {title: "CRUD test with @Before and @After annotations", commentsType: "inline"}
```

# Maven Import
@@ -22,15 +22,14 @@
import com.twosigma.webtau.utils.UrlUtils;

public class DynamicPortBaseUrlConfig implements HttpConfiguration {

public static final String SPRING_BOOT_EXAMPLE_URL_PREFIX = "/customers";
private static final String SPRING_BOOT_EXAMPLE_URL_PREFIX = "/customers";
private static final String DEFAULT_SPRINGBOOT_APP_PORT = "8080";

@Override
public String fullUrl(String url) {
if (url.contains("/customers")) {
String port = System.getProperty("springboot.http.port", "8080");
int prefixIdx = url.indexOf(SPRING_BOOT_EXAMPLE_URL_PREFIX);
return "http://localhost:" + port + url.substring(prefixIdx);
return "http://localhost:" + getSpringbootAppPort() + url.substring(prefixIdx);
}

if (UrlUtils.isFull(url)) {
@@ -44,4 +43,13 @@ public String fullUrl(String url) {
public HttpHeader fullHeader(String fullUrl, String passedUrl, HttpHeader given) {
return given;
}

private String getSpringbootAppPort() {
String port = System.getProperty("springboot.http.port", DEFAULT_SPRINGBOOT_APP_PORT);
if (port == null || port.isEmpty()) {
return DEFAULT_SPRINGBOOT_APP_PORT;
}

return port;
}
}
@@ -0,0 +1,54 @@
package com.example.tests.junit4

import com.twosigma.webtau.junit4.WebTauRunner
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

import static com.twosigma.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
}
}
}
@@ -4,9 +4,9 @@ import com.twosigma.webtau.junit4.WebTauRunner
import org.junit.Test
import org.junit.runner.RunWith

import static com.twosigma.webtau.WebTauDsl.*
import static com.twosigma.webtau.WebTauGroovyDsl.* // convenient single import for DSL methods and props like http

@RunWith(WebTauRunner.class)
@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']
@@ -0,0 +1,67 @@
package com.example.tests.junit4;

import com.twosigma.webtau.junit4.WebTauRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Map;

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

@RunWith(WebTauRunner.class)
public class CustomerCrudBeforeAfterJavaTest {
private Map<String, Object> customerPayload = createCustomerPayload();
private Map<String, Object> changedCustomerPayload = createChangedCustomerPayload();
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));
}));
}

private Map<String, Object> createCustomerPayload() {
return aMapOf(
"firstName", "FN",
"lastName", "LN");
}

private Map<String, Object> createChangedCustomerPayload() {
Map<String, Object> payload = createCustomerPayload();
payload.put("lastName", "NLN");

return payload;
}
}
@@ -6,11 +6,9 @@

import java.util.Map;

import static com.twosigma.webtau.Ddjt.aMapOf;
import static com.twosigma.webtau.WebTauDsl.equal;
import static com.twosigma.webtau.WebTauDsl.http;
import static com.twosigma.webtau.WebTauDsl.*; // convenient single import for DSL methods and props like http and equal, aMapOf, etc

@RunWith(WebTauRunner.class)
@RunWith(WebTauRunner.class) // runner is required to have this test to be a part of generated html report
public class CustomerCrudSingleJavaTest {
private Map<String, Object> customerPayload = createCustomerPayload();
private Map<String, Object> changedCustomerPayload = createChangedCustomerPayload();
@@ -4,7 +4,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;

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

@RunWith(WebTauRunner.class)
public class TodoListJavaIT {
@@ -4,7 +4,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;

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

@RunWith(WebTauRunner.class)
public class WeatherJavaIT {
@@ -22,10 +22,12 @@
import com.twosigma.webtau.report.ReportTestEntry;
import com.twosigma.webtau.reporter.StepReporters;
import com.twosigma.webtau.reporter.TestResultPayloadExtractors;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;

import java.util.concurrent.atomic.AtomicInteger;

@@ -37,29 +39,30 @@ public WebTauRunner(Class<?> klass) throws InitializationError {
}

@Override
protected Statement methodInvoker(FrameworkMethod method, Object test) {
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
JavaBasedTest javaBasedTest = new JavaBasedTest(
method.getName() + idGenerator.incrementAndGet(),
method.getName());

ReportTestEntry reportTestEntry = javaBasedTest.getReportTestEntry();
reportTestEntry.setClassName(method.getDeclaringClass().getCanonicalName());

Statement runStatement = super.methodInvoker(method, test);
return new Statement() {
notifier.addListener(new RunListener() {
@Override
public void evaluate() throws Throwable {
beforeTestRun(javaBasedTest);
try {
runStatement.evaluate();
} catch (Throwable e) {
reportTestEntry.setException(e);
throw e;
} finally {
afterTestRun(javaBasedTest);
}
public void testFailure(Failure failure) {
reportTestEntry.setExceptionIfNotSet(failure.getException());
}
};
});

beforeTestRun(javaBasedTest);
try {
super.runChild(method, notifier);
} catch (Throwable e) {
reportTestEntry.setExceptionIfNotSet(e);
throw e;
} finally {
afterTestRun(javaBasedTest);
}
}

private void beforeTestRun(JavaBasedTest javaBasedTest) {
@@ -13,7 +13,7 @@ class CustomerCrudSeparatedGroovyTest {
lastName : 'LN']
private static def changedCustomerPayload = [*: customerPayload, lastName: 'NLN']

private static def id;
private static def id

@BeforeAll
static void createCustomer() {
@@ -138,6 +138,12 @@ public void setException(Throwable exception) {
this.exception = exception;
}

public void setExceptionIfNotSet(Throwable exception) {
if (this.exception == null) {
setException(exception);
}
}

public void setRan(boolean ran) {
isRan = ran;
}

0 comments on commit 369c0c4

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