Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JUnit 5 Expect extension #706

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions extensions/junit5/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.google.truth.extensions</groupId>
<artifactId>truth-extensions-parent</artifactId>
<version>HEAD-SNAPSHOT</version>
</parent>
<artifactId>truth-junit5-extension</artifactId>
<name>Truth Extension for JUnit Jupiter</name>
<description>
An extension for JUnit Jupiter to use soft assertions with Truth test assertion framework
</description>
<dependencies>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-testkit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.opentest4j</groupId>
<artifactId>opentest4j</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.google.common.truth.junit5;

import com.google.common.truth.FailureStrategy;
import com.google.common.truth.StandardSubjectBuilder;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.opentest4j.MultipleFailuresError;

import java.util.ArrayList;
import java.util.List;

/**
* An <tt>Extension</tt> that provides a {@link StandardSubjectBuilder} parameter to test methods.
*
* <p>Assertion failures on the given <tt>StandardSubjectBuilder</tt> will not immediately fail;
* instead, failures will be reported at the <em>end</em> of the test.
*
* <p>Usage:
*
* <pre>
* @ExtendWith(Expect.class) // or system property junit.jupiter.extensions.autodetection.enabled=true
* ...
* @Test
* public void test(StandardSubjectBuilder expect) {
* ...
* }
* </pre>
*/
public class Expect implements Extension, ParameterResolver, AfterEachCallback {
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
return parameterContext.getParameter().getType() == StandardSubjectBuilder.class;
}

@Override
public StandardSubjectBuilder resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
ExtensionContext.Store store = extensionContext.getStore(ExtensionContext.Namespace.create(Expect.class));
return store.getOrComputeIfAbsent(extensionContext.getUniqueId(), key -> new Holder(), Holder.class).get();
}

@Override
public void afterEach(ExtensionContext context) {
ExtensionContext.Store store = context.getStore(ExtensionContext.Namespace.create(Expect.class));
Holder holder = store.get(context.getUniqueId(), Holder.class);
List<AssertionError> failures = null;
if (holder != null) {
synchronized (holder) {
failures = holder.failures;
if (failures == null) {
throw new IllegalStateException();
}
holder.failures = null;
}
}
if (failures != null && !failures.isEmpty()) {
throw failures.size() == 1 ? failures.get(0) : new MultipleFailuresError(null, failures);
}
}

private static class Holder implements FailureStrategy, ExtensionContext.Store.CloseableResource {
private List<AssertionError> failures = new ArrayList<>();

private StandardSubjectBuilder get() {
return StandardSubjectBuilder.forCustomFailureStrategy(this);
}

@Override
public synchronized void fail(AssertionError failure) {
failures.add(failure);
}

@Override
public synchronized void close() {
if (failures != null) {
throw new IllegalStateException();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.google.common.truth.junit5.Expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.google.common.truth.junit5;

import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import static org.junit.platform.testkit.engine.EventConditions.event;
import static org.junit.platform.testkit.engine.EventConditions.finishedWithFailure;
import static org.junit.platform.testkit.engine.EventConditions.test;

import com.google.common.truth.StandardSubjectBuilder;
import org.assertj.core.api.Condition;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.testkit.engine.EngineTestKit;
import org.junit.platform.testkit.engine.Events;
import org.opentest4j.MultipleFailuresError;

public class ExpectTest {
@Test
public void testExtension() {
Events events = EngineTestKit.engine("junit-jupiter")
.selectors(selectClass(TestCase.class))
.execute()
.testEvents()
.assertStatistics(stats -> stats.started(3).failed(2));
events.succeeded().assertEventsMatchExactly(event(test("testSuccess")));
events.failed().assertEventsMatchExactly(
event(test("testOneFailure"), finishedWithFailure(new Condition(t -> t instanceof AssertionError && !(t instanceof MultipleFailuresError), "AssertionError"))),
event(test("testTwoFailures"), finishedWithFailure(new Condition(MultipleFailuresError.class::isInstance, "MultipleFailuresError"))));
}

@ExtendWith(Expect.class)
@TestMethodOrder(OrderAnnotation.class)
static class TestCase {
@Test
@Order(1)
public void testSuccess(StandardSubjectBuilder expect) {
expect.that(0).isEqualTo(0);
expect.that(1).isEqualTo(1);
}

@Test
@Order(2)
@ExtendWith(Expect.class)
public void testOneFailure(StandardSubjectBuilder expect) {
expect.that(0).isEqualTo(1);
expect.that(1).isEqualTo(1);
}

@Test
@Order(3)
@ExtendWith(Expect.class)
public void testTwoFailures(StandardSubjectBuilder expect) {
expect.that(0).isEqualTo(1);
expect.that(1).isEqualTo(0);
}
}
}
1 change: 1 addition & 0 deletions extensions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
<module>re2j</module>
<module>liteproto</module>
<module>proto</module>
<module>junit5</module>
</modules>
</project>
22 changes: 21 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-testkit</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.opentest4j</groupId>
<artifactId>opentest4j</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-user</artifactId>
Expand Down Expand Up @@ -322,7 +342,7 @@
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
Expand Down