Skip to content

Commit

Permalink
Add support for using AssertJ assertions
Browse files Browse the repository at this point in the history
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
szpak committed Mar 29, 2014
1 parent 846ac63 commit a845000
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,4 +1,5 @@
target target
.idea/
*.iws *.iws
*.ipr *.ipr
*.iml *.iml
Expand Down
9 changes: 0 additions & 9 deletions awaitility-groovy/pom.xml
Expand Up @@ -75,15 +75,6 @@
</execution> </execution>
</executions> </executions>
</plugin> </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> </plugins>
</build> </build>
</project> </project>
64 changes: 64 additions & 0 deletions 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>
Empty file.
@@ -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()));
}
}
4 changes: 3 additions & 1 deletion awaitility-scala/pom.xml
Expand Up @@ -26,7 +26,7 @@
<dependency> <dependency>
<groupId>com.jayway.awaitility</groupId> <groupId>com.jayway.awaitility</groupId>
<artifactId>awaitility</artifactId> <artifactId>awaitility</artifactId>
<version>${version}</version> <version>${project.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>


Expand All @@ -37,6 +37,7 @@
<plugin> <plugin>
<groupId>org.scala-tools</groupId> <groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId> <artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<executions> <executions>
<execution> <execution>
<goals> <goals>
Expand All @@ -55,6 +56,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId> <artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration> <configuration>
<downloadSources>true</downloadSources> <downloadSources>true</downloadSources>
<buildcommands> <buildcommands>
Expand Down
19 changes: 10 additions & 9 deletions awaitility/pom.xml
Expand Up @@ -5,7 +5,6 @@
<artifactId>awaitility-parent</artifactId> <artifactId>awaitility-parent</artifactId>
<version>1.5.1-SNAPSHOT</version> <version>1.5.1-SNAPSHOT</version>
</parent> </parent>
<groupId>com.jayway.awaitility</groupId>
<artifactId>awaitility</artifactId> <artifactId>awaitility</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<url>http://github.com/jayway/awaitility</url> <url>http://github.com/jayway/awaitility</url>
Expand All @@ -16,14 +15,16 @@
</properties> </properties>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId>
<artifactId>maven-compiler-plugin</artifactId> <executions>
<configuration> <execution>
<source>1.5</source> <goals>
<target>1.5</target> <goal>test-jar</goal>
</configuration> </goals>
</plugin> </execution>
</executions>
</plugin>
</plugins> </plugins>
</build> </build>
<dependencies> <dependencies>
Expand Down
@@ -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;
}
}
Expand Up @@ -377,6 +377,44 @@ public <T> T until(final Callable<T> supplier, final Matcher<? super T> matcher)
return until(new CallableHamcrestCondition<T>(supplier, matcher, generateConditionSettings())); return until(new CallableHamcrestCondition<T>(supplier, matcher, generateConditionSettings()));
} }


/**
* 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 * Await until a Atomic variable has a value matching the specified
* {@link Matcher}. E.g. * {@link Matcher}. E.g.
Expand Down
24 changes: 24 additions & 0 deletions pom.xml
Expand Up @@ -59,6 +59,7 @@
<plugins> <plugins>
<plugin> <plugin>
<artifactId>maven-release-plugin</artifactId> <artifactId>maven-release-plugin</artifactId>
<version>2.5</version>
<configuration> <configuration>
<autoVersionSubmodules>true</autoVersionSubmodules> <autoVersionSubmodules>true</autoVersionSubmodules>
<useReleaseProfile>false</useReleaseProfile> <useReleaseProfile>false</useReleaseProfile>
Expand All @@ -70,7 +71,26 @@
<mavenExecutorId>forked-path</mavenExecutorId> <mavenExecutorId>forked-path</mavenExecutorId>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins> </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> </build>
<profiles> <profiles>
<profile> <profile>
Expand All @@ -80,6 +100,7 @@
<!-- We want to sign the artifact, the POM, and all attached artifacts --> <!-- We want to sign the artifact, the POM, and all attached artifacts -->
<plugin> <plugin>
<artifactId>maven-gpg-plugin</artifactId> <artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<configuration> <configuration>
<useAgent>false</useAgent> <useAgent>false</useAgent>
</configuration> </configuration>
Expand All @@ -97,12 +118,14 @@
<plugin> <plugin>
<inherited>true</inherited> <inherited>true</inherited>
<artifactId>maven-deploy-plugin</artifactId> <artifactId>maven-deploy-plugin</artifactId>
<version>2.8.1</version>
<configuration> <configuration>
<updateReleaseInfo>true</updateReleaseInfo> <updateReleaseInfo>true</updateReleaseInfo>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-source-plugin</artifactId> <artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions> <executions>
<execution> <execution>
<id>attach-sources</id> <id>attach-sources</id>
Expand Down Expand Up @@ -148,5 +171,6 @@
<module>awaitility</module> <module>awaitility</module>
<module>awaitility-scala</module> <module>awaitility-scala</module>
<module>awaitility-groovy</module> <module>awaitility-groovy</module>
<module>awaitility-java8</module>
</modules> </modules>
</project> </project>

0 comments on commit a845000

Please sign in to comment.