Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
10 changed files
with
282 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,4 +1,5 @@ | |||
target | target | ||
.idea/ | |||
*.iws | *.iws | ||
*.ipr | *.ipr | ||
*.iml | *.iml | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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.
82 changes: 82 additions & 0 deletions
82
awaitility-java8/src/test/java/com/jayway/awaitility/AwaitilityJava8Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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())); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
awaitility/src/main/java/com/jayway/awaitility/core/AssertionCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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; | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters