Skip to content
Browse files

Add support for using AssertJ assertions

Those assertions are passed as Runnable which became compact
with Java 8.
By the way explicit versions for Maven plugins were added.
  • Loading branch information...
1 parent 846ac63 commit a8450004fc6990582a445ef0deb76ee4b38e0acd @szpak szpak committed Mar 29, 2014
View
1 .gitignore
@@ -1,4 +1,5 @@
target
+.idea/
*.iws
*.ipr
*.iml
View
9 awaitility-groovy/pom.xml
@@ -75,15 +75,6 @@
</execution>
</executions>
</plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.5.1</version>
- <configuration>
- <source>1.5</source>
- <target>1.5</target>
- </configuration>
- </plugin>
</plugins>
</build>
</project>
View
64 awaitility-java8/pom.xml
@@ -0,0 +1,64 @@
+<?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/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <artifactId>awaitility-parent</artifactId>
+ <groupId>com.jayway.awaitility</groupId>
+ <version>1.5.1-SNAPSHOT</version>
+ </parent>
+ <artifactId>awaitility-java8</artifactId>
+ <name>Awaitility support for Java 8</name>
+ <description>Simplifies Awaitility usage with Java 8</description>
+ <packaging>jar</packaging>
+
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.8</source>
+ <target>1.8</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <artifactId>maven-install-plugin</artifactId>
+ <version>2.5.1</version>
+ <configuration>
+ <!-- There is no need to install empty jar -->
+ <skip>true</skip>
+ </configuration>
+ </plugin>
+
+ </plugins>
+ </build>
+ <dependencies>
+ <dependency>
+ <groupId>com.jayway.awaitility</groupId>
+ <artifactId>awaitility</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>com.jayway.awaitility</groupId>
+ <artifactId>awaitility</artifactId>
+ <version>${project.version}</version>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.assertj</groupId>
+ <artifactId>assertj-core</artifactId>
+ <version>1.6.0</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.10</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+</project>
View
0 awaitility-java8/src/main/java/.gitkeep
No changes.
View
82 awaitility-java8/src/test/java/com/jayway/awaitility/AwaitilityJava8Test.java
@@ -0,0 +1,82 @@
+package com.jayway.awaitility;
+
+import com.jayway.awaitility.classes.Asynch;
+import com.jayway.awaitility.classes.FakeRepository;
+import com.jayway.awaitility.classes.FakeRepositoryImpl;
+import com.jayway.awaitility.core.ConditionTimeoutException;
+import org.assertj.core.api.Assertions;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static com.jayway.awaitility.Awaitility.await;
+import static com.jayway.awaitility.Awaitility.with;
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static org.hamcrest.Matchers.endsWith;
+import static org.hamcrest.Matchers.startsWith;
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Tests for await().until(Runnable) using AssertionCondition.
+ *
+ * @author Marcin Zajączkowski, 2014-03-28
+ */
+public class AwaitilityJava8Test {
+
+ private FakeRepository fakeRepository;
+
+ @Rule
+ public ExpectedException exception = ExpectedException.none();
+
+ @Before
+ public void setup() {
+ fakeRepository = new FakeRepositoryImpl();
+ Awaitility.reset();
+ }
+
+ @Test(timeout = 2000)
+ public void awaitAssertJAssertionAsLambda() {
+ new Asynch(fakeRepository).perform();
+ await().untilPass(() -> Assertions.assertThat(fakeRepository.getValue()).isEqualTo(1));
+ }
+
+ @SuppressWarnings("Convert2Lambda")
+ @Test(timeout = 2000)
+ public void awaitAssertJAssertionAsAnonymousClass() {
+ new Asynch(fakeRepository).perform();
+ await().untilPass(new Runnable() {
+ @Override
+ public void run() {
+ Assertions.assertThat(fakeRepository.getValue()).isEqualTo(1);
+ }
+ });
+ }
+
+ @Test(timeout = 2000)
+ public void awaitAssertJAssertionDisplaysOriginalErrorMessageAndTimeoutWhenConditionTimeoutExceptionOccurs() {
+ exception.expect(ConditionTimeoutException.class);
+ exception.expectMessage(startsWith(AwaitilityJava8Test.class.getName()));
+ exception.expectMessage(endsWith("expected:<[1]> but was:<[0]> within 120 milliseconds."));
+
+ new Asynch(fakeRepository).perform();
+ with().pollInterval(10, MILLISECONDS).then().await().atMost(120, MILLISECONDS).untilPass(
+ () -> Assertions.assertThat(fakeRepository.getValue()).isEqualTo(1));
+ }
+
+ @Test(timeout = 2000)
+ public void awaitJUnitAssertionAsLambda() {
+ new Asynch(fakeRepository).perform();
+ await().untilPass(() -> assertEquals(1, fakeRepository.getValue()));
+ }
+
+ @Test(timeout = 2000)
+ public void awaitJUnitAssertionDisplaysOriginalErrorMessageAndTimeoutWhenConditionTimeoutExceptionOccurs() {
+ exception.expect(ConditionTimeoutException.class);
+ exception.expectMessage(startsWith(AwaitilityJava8Test.class.getName()));
+ exception.expectMessage(endsWith("expected:<1> but was:<0> within 120 milliseconds."));
+
+ with().pollInterval(10, MILLISECONDS).then().await().atMost(120, MILLISECONDS).untilPass(
+ () -> assertEquals(1, fakeRepository.getValue()));
+ }
+}
View
4 awaitility-scala/pom.xml
@@ -26,7 +26,7 @@
<dependency>
<groupId>com.jayway.awaitility</groupId>
<artifactId>awaitility</artifactId>
- <version>${version}</version>
+ <version>${project.version}</version>
</dependency>
</dependencies>
@@ -37,6 +37,7 @@
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
+ <version>2.15.2</version>
<executions>
<execution>
<goals>
@@ -55,6 +56,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
+ <version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<buildcommands>
View
19 awaitility/pom.xml
@@ -5,7 +5,6 @@
<artifactId>awaitility-parent</artifactId>
<version>1.5.1-SNAPSHOT</version>
</parent>
- <groupId>com.jayway.awaitility</groupId>
<artifactId>awaitility</artifactId>
<packaging>jar</packaging>
<url>http://github.com/jayway/awaitility</url>
@@ -16,14 +15,16 @@
</properties>
<build>
<plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.5</source>
- <target>1.5</target>
- </configuration>
- </plugin>
+ <plugin>
+ <artifactId>maven-jar-plugin</artifactId>
+ <executions>
+ <execution>
+ <goals>
+ <goal>test-jar</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
</plugins>
</build>
<dependencies>
View
60 awaitility/src/main/java/com/jayway/awaitility/core/AssertionCondition.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2014 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jayway.awaitility.core;
+
+import java.util.concurrent.Callable;
+
+/**
+ * Condition implementation which takes an executable assertion which should throw {@link AssertionError} on assertion failure.
+ *
+ * @since 1.6.0
+ *
+ * @author Marcin Zajączkowski, 2014-03-28
+ */
+public class AssertionCondition implements Condition<Void> {
+
+ private final ConditionAwaiter conditionAwaiter;
+
+ private String lastExceptionMessage;
+
+ public AssertionCondition(final Runnable supplier, ConditionSettings settings) {
+ if (supplier == null) {
+ throw new IllegalArgumentException("You must specify a supplier (was null).");
+ }
+ Callable<Boolean> callable = new Callable<Boolean>() {
+ public Boolean call() throws Exception {
+ try {
+ supplier.run();
+ return true;
+ } catch (AssertionError e) {
+ lastExceptionMessage = e.getMessage();
+ return false;
+ }
+ }
+ };
+ conditionAwaiter = new ConditionAwaiter(callable, settings) {
+ @Override
+ protected String getTimeoutMessage() {
+ return supplier.getClass().getName() + " " + lastExceptionMessage;
+ }
+ };
+ }
+
+ public Void await() {
+ conditionAwaiter.await();
+ return null;
+ }
+}
View
38 awaitility/src/main/java/com/jayway/awaitility/core/ConditionFactory.java
@@ -378,6 +378,44 @@ public ConditionFactory dontCatchUncaughtExceptions() {
}
/**
+ * Await until a {@link Runnable} supplier execution passes (ends without throwing an exception). E.g. with Java 8:
+ * </p>
+ * <pre>
+ * await().untilPass(() -> Assertions.assertThat(personRepository.size()).isEqualTo(6));
+ * </pre>
+ * or
+ * <pre>
+ * await().untilPass(() -> assertEquals(6, personRepository.size()));
+ * </pre>
+ *
+ * This method is intended to benefit from lambda expressions introduced in Java 8. It allows to use standard AssertJ/FEST Assert assertions
+ * (by the way also standard JUnit/TestNG assertions) to test asynchronous calls and systems.
+ *
+ * {@link AssertionError} instances thrown by the supplier are treated as an assertion failure and proper error message is propagated on timeout.
+ * Other exceptions are rethrown immediately as an execution errors.
+ *
+ * Why technically it is completely valid to use plain Runnable class in Java 7 code, the resulting expression is very verbose and can decrease
+ * the readability of the test case, e.g.
+ * </p>
+ * <pre>
+ * await().untilPass(new Runnable() {
+ * <verbatim>@Override</verbatim>
+ * public void run() {
+ * Assertions.assertThat(personRepository.size()).isEqualTo(6);
+ * }
+ * });
+ * </pre>
+ *
+ * @param supplier the supplier that is responsible for executing the assertion and throwing AssertionError on failure.
+ * @throws ConditionTimeoutException If condition was not fulfilled within the given time period.
+ *
+ * @since 1.6.0
+ */
+ public void untilPass(final Runnable supplier) {
+ until(new AssertionCondition(supplier, generateConditionSettings()));
+ }
+
+ /**
* Await until a Atomic variable has a value matching the specified
* {@link Matcher}. E.g.
* <p/>
View
24 pom.xml
@@ -59,6 +59,7 @@
<plugins>
<plugin>
<artifactId>maven-release-plugin</artifactId>
+ <version>2.5</version>
<configuration>
<autoVersionSubmodules>true</autoVersionSubmodules>
<useReleaseProfile>false</useReleaseProfile>
@@ -70,7 +71,26 @@
<mavenExecutorId>forked-path</mavenExecutorId>
</configuration>
</plugin>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ </plugin>
</plugins>
+ <pluginManagement>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>3.1</version>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <artifactId>maven-jar-plugin</artifactId>
+ <version>2.4</version>
+ </plugin>
+ </plugins>
+ </pluginManagement>
</build>
<profiles>
<profile>
@@ -80,6 +100,7 @@
<!-- We want to sign the artifact, the POM, and all attached artifacts -->
<plugin>
<artifactId>maven-gpg-plugin</artifactId>
+ <version>1.5</version>
<configuration>
<useAgent>false</useAgent>
</configuration>
@@ -97,12 +118,14 @@
<plugin>
<inherited>true</inherited>
<artifactId>maven-deploy-plugin</artifactId>
+ <version>2.8.1</version>
<configuration>
<updateReleaseInfo>true</updateReleaseInfo>
</configuration>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
+ <version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
@@ -148,5 +171,6 @@
<module>awaitility</module>
<module>awaitility-scala</module>
<module>awaitility-groovy</module>
+ <module>awaitility-java8</module>
</modules>
</project>

0 comments on commit a845000

Please sign in to comment.
Something went wrong with that request. Please try again.