From 06d2af98dfa8f8be0d9afa41b75fb3adac27dcd5 Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Sat, 17 Jun 2017 19:46:42 +0200 Subject: [PATCH 01/31] Start to develop the 0.4 version. --- pom.xml | 2 +- test-as-you-think-core/pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index c75c5cd..72c64ec 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.github.xapn test-as-you-think-project - 0.3 + 0.4-SNAPSHOT pom ${project.groupId}:${project.artifactId}:${project.version}:${project.packaging} The TestAsYouThink project aims to provide tooling to improve test code quality and to make testing diff --git a/test-as-you-think-core/pom.xml b/test-as-you-think-core/pom.xml index 8f6bcca..b8aae87 100644 --- a/test-as-you-think-core/pom.xml +++ b/test-as-you-think-core/pom.xml @@ -5,7 +5,7 @@ com.github.xapn test-as-you-think-project - 0.3 + 0.4-SNAPSHOT test-as-you-think-core From 436dfef94c91c5ce102d60edd747fa851cd98016 Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Tue, 20 Jun 2017 02:19:13 +0200 Subject: [PATCH 02/31] It should fail given a too slow void method. --- .../java/testasyouthink/GivenWhenThenDsl.java | 2 ++ .../testasyouthink/ThenWithoutResultStep.java | 26 +++++++++++++++++++ .../ThenSutRepliesWithinTimeLimitTest.java | 25 ++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java diff --git a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java index e2e6ab1..f501b02 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java @@ -202,6 +202,8 @@ interface ThenWithoutResult<$SystemUnderTest> { Consumer<$SystemUnderTest> thenStep); AndThenWithoutResult<$SystemUnderTest> then(BooleanSupplier thenStep); + + AndThenWithoutResult<$SystemUnderTest> thenSutRepliesWithin(long timeLimit); } interface AndThenWithoutResult<$SystemUnderTest> { diff --git a/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java b/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java index 59fab03..9eaae1b 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java @@ -25,6 +25,12 @@ import testasyouthink.GivenWhenThenDsl.VerificationStage.AndThenWithoutResult; import testasyouthink.GivenWhenThenDsl.VerificationStage.ThenWithoutResult; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.FutureTask; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.function.BooleanSupplier; import java.util.function.Consumer; @@ -103,4 +109,24 @@ public ThenWithoutResultStep(GivenWhenContext<$SystemUnderTest, Void> context) { thenStep.accept(context.getSystemUnderTest()); return this; } + + @Override + public AndThenWithoutResult<$SystemUnderTest> thenSutRepliesWithin(long timeLimit) { + FutureTask futureTask = new FutureTask<>(() -> context.returnResultOrVoid()); + ExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); + executorService.execute(futureTask); + try { + futureTask.get(timeLimit, TimeUnit.MILLISECONDS); + } catch (InterruptedException exception) { + throw new RuntimeException("Not yet implemented!"); + } catch (ExecutionException exception) { + throw new RuntimeException("Not yet implemented!"); + } catch (TimeoutException exception) { + throw new AssertionError("test timed out after " + timeLimit + " milliseconds", exception); + } finally { + executorService.shutdownNow(); + } + assertThat(executorService.isShutdown()).isTrue(); + return this; + } } diff --git a/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java b/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java new file mode 100644 index 0000000..d05fed9 --- /dev/null +++ b/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java @@ -0,0 +1,25 @@ +package testasyouthink; + +import org.junit.Test; +import testasyouthink.fixture.SystemUnderTest; + +import java.util.concurrent.TimeoutException; + +import static java.lang.Thread.sleep; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static testasyouthink.TestAsYouThink.givenSutClass; + +public class ThenSutRepliesWithinTimeLimitTest { + + @Test + public void should_fail_given_a_too_slow_void_method() { + assertThatThrownBy(() -> givenSutClass(SystemUnderTest.class) + .when(sut -> { + sleep(1000); + }) + .thenSutRepliesWithin(1)) + .isInstanceOf(AssertionError.class) + .hasMessage("test timed out after 1 milliseconds") + .hasCauseInstanceOf(TimeoutException.class); + } +} From 5b66a3ec0a3cf82eaeefdd5e41f2641b579cb3fd Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Tue, 20 Jun 2017 14:36:22 +0200 Subject: [PATCH 03/31] It should fail when the current thread was interrupted while waiting given a void method. --- .../testasyouthink/ThenWithoutResultStep.java | 2 +- .../ThenSutRepliesWithinTimeLimitTest.java | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java b/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java index 9eaae1b..df786f6 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java @@ -118,7 +118,7 @@ public ThenWithoutResultStep(GivenWhenContext<$SystemUnderTest, Void> context) { try { futureTask.get(timeLimit, TimeUnit.MILLISECONDS); } catch (InterruptedException exception) { - throw new RuntimeException("Not yet implemented!"); + throw new AssertionError("the current thread was interrupted while waiting", exception); } catch (ExecutionException exception) { throw new RuntimeException("Not yet implemented!"); } catch (TimeoutException exception) { diff --git a/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java b/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java index d05fed9..739c770 100644 --- a/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java +++ b/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java @@ -1,3 +1,25 @@ +/*- + * #%L + * Test As You Think + * %% + * Copyright (C) 2017 Xavier Pigeon and TestAsYouThink contributors + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * . + * #L% + */ + package testasyouthink; import org.junit.Test; @@ -5,6 +27,7 @@ import java.util.concurrent.TimeoutException; +import static java.lang.Thread.currentThread; import static java.lang.Thread.sleep; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static testasyouthink.TestAsYouThink.givenSutClass; @@ -22,4 +45,19 @@ public void should_fail_given_a_too_slow_void_method() { .hasMessage("test timed out after 1 milliseconds") .hasCauseInstanceOf(TimeoutException.class); } + + @Test + public void should_fail_when_the_current_thread_was_interrupted_while_waiting_given_a_void_method() { + assertThatThrownBy(() -> givenSutClass(SystemUnderTest.class) + .when(sut -> { + currentThread() + .getThreadGroup() + .getParent() + .interrupt(); + }) + .thenSutRepliesWithin(1000)) + .isInstanceOf(AssertionError.class) + .hasMessage("the current thread was interrupted while waiting") + .hasCauseInstanceOf(InterruptedException.class); + } } From 951500297fa2970f6ff679740cfdc211c31603df Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Tue, 20 Jun 2017 14:43:58 +0200 Subject: [PATCH 04/31] It should fail when the computation throws an exception given a void method. --- .../java/testasyouthink/ThenWithoutResultStep.java | 2 +- .../ThenSutRepliesWithinTimeLimitTest.java | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java b/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java index df786f6..267c91a 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java @@ -120,7 +120,7 @@ public ThenWithoutResultStep(GivenWhenContext<$SystemUnderTest, Void> context) { } catch (InterruptedException exception) { throw new AssertionError("the current thread was interrupted while waiting", exception); } catch (ExecutionException exception) { - throw new RuntimeException("Not yet implemented!"); + throw new AssertionError("the computation threw an exception", exception); } catch (TimeoutException exception) { throw new AssertionError("test timed out after " + timeLimit + " milliseconds", exception); } finally { diff --git a/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java b/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java index 739c770..8e613c6 100644 --- a/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java +++ b/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java @@ -24,7 +24,9 @@ import org.junit.Test; import testasyouthink.fixture.SystemUnderTest; +import testasyouthink.function.CheckedConsumer; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeoutException; import static java.lang.Thread.currentThread; @@ -60,4 +62,16 @@ public void should_fail_when_the_current_thread_was_interrupted_while_waiting_gi .hasMessage("the current thread was interrupted while waiting") .hasCauseInstanceOf(InterruptedException.class); } + + @Test + public void should_fail_when_the_computation_throws_an_exception_given_a_void_method() { + assertThatThrownBy(() -> givenSutClass(SystemUnderTest.class) + .when((CheckedConsumer) sut -> { + throw new Exception("unexpected exception"); + }) + .thenSutRepliesWithin(1000)) + .isInstanceOf(AssertionError.class) + .hasMessage("the computation threw an exception") + .hasCauseInstanceOf(ExecutionException.class); + } } From f018560c5773e5fa5f921a1e66a657ad444775ce Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Tue, 20 Jun 2017 16:58:41 +0200 Subject: [PATCH 05/31] Refactor to separated matters. --- .../main/java/testasyouthink/Assertion.java | 60 +++++++++++++++++++ .../main/java/testasyouthink/ThenStep.java | 2 +- .../testasyouthink/ThenWithoutResultStep.java | 24 +------- 3 files changed, 64 insertions(+), 22 deletions(-) create mode 100644 test-as-you-think-core/src/main/java/testasyouthink/Assertion.java diff --git a/test-as-you-think-core/src/main/java/testasyouthink/Assertion.java b/test-as-you-think-core/src/main/java/testasyouthink/Assertion.java new file mode 100644 index 0000000..20caf1c --- /dev/null +++ b/test-as-you-think-core/src/main/java/testasyouthink/Assertion.java @@ -0,0 +1,60 @@ +/*- + * #%L + * Test As You Think + * %% + * Copyright (C) 2017 Xavier Pigeon and TestAsYouThink contributors + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * . + * #L% + */ + +package testasyouthink; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.FutureTask; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +class Assertion { + + private Runnable runnable; + + Assertion assertThat(Runnable runnable) { + this.runnable = runnable; + return this; + } + + void spendAtMost(long timeLimit) { + FutureTask futureTask = new FutureTask<>(() -> { + runnable.run(); + return null; + }); + ExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); + executorService.execute(futureTask); + try { + futureTask.get(timeLimit, TimeUnit.MILLISECONDS); + } catch (InterruptedException exception) { + throw new AssertionError("the current thread was interrupted while waiting", exception); + } catch (ExecutionException exception) { + throw new AssertionError("the computation threw an exception", exception); + } catch (TimeoutException exception) { + throw new AssertionError("test timed out after " + timeLimit + " milliseconds", exception); + } finally { + executorService.shutdownNow(); + } + } +} diff --git a/test-as-you-think-core/src/main/java/testasyouthink/ThenStep.java b/test-as-you-think-core/src/main/java/testasyouthink/ThenStep.java index d36bc58..0aad144 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/ThenStep.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/ThenStep.java @@ -91,7 +91,7 @@ public void then(BiConsumer<$SystemUnderTest, $Result> thenStep) { public void then(List> thenSteps) { assertThat(thenSteps .stream() - .reduce((predicate, another) -> predicate.and(another)) + .reduce(Predicate::and) .get() .test(context.returnResultOrVoid())).isTrue(); } diff --git a/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java b/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java index 267c91a..89444d8 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java @@ -25,12 +25,6 @@ import testasyouthink.GivenWhenThenDsl.VerificationStage.AndThenWithoutResult; import testasyouthink.GivenWhenThenDsl.VerificationStage.ThenWithoutResult; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.FutureTask; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; import java.util.function.BooleanSupplier; import java.util.function.Consumer; @@ -112,21 +106,9 @@ public ThenWithoutResultStep(GivenWhenContext<$SystemUnderTest, Void> context) { @Override public AndThenWithoutResult<$SystemUnderTest> thenSutRepliesWithin(long timeLimit) { - FutureTask futureTask = new FutureTask<>(() -> context.returnResultOrVoid()); - ExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); - executorService.execute(futureTask); - try { - futureTask.get(timeLimit, TimeUnit.MILLISECONDS); - } catch (InterruptedException exception) { - throw new AssertionError("the current thread was interrupted while waiting", exception); - } catch (ExecutionException exception) { - throw new AssertionError("the computation threw an exception", exception); - } catch (TimeoutException exception) { - throw new AssertionError("test timed out after " + timeLimit + " milliseconds", exception); - } finally { - executorService.shutdownNow(); - } - assertThat(executorService.isShutdown()).isTrue(); + new Assertion() + .assertThat(context::returnResultOrVoid) + .spendAtMost(timeLimit); return this; } } From af859d8642e369978caf2e9f182d99a6f46cd5ff Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Tue, 20 Jun 2017 17:02:01 +0200 Subject: [PATCH 06/31] It should fail given a too slow non-void method. --- .../main/java/testasyouthink/GivenWhenThenDsl.java | 2 ++ .../src/main/java/testasyouthink/ThenStep.java | 8 ++++++++ .../ThenSutRepliesWithinTimeLimitTest.java | 13 +++++++++++++ 3 files changed, 23 insertions(+) diff --git a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java index f501b02..a2effb8 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java @@ -168,6 +168,8 @@ interface Then<$SystemUnderTest, $Result> { AndThen<$SystemUnderTest, $Result> then(Predicate<$Result> thenStep); + AndThen<$SystemUnderTest, $Result> thenSutRepliesWithin(long timeLimit); + void then(List> thenSteps); void then(BiConsumer<$SystemUnderTest, $Result> thenStep); diff --git a/test-as-you-think-core/src/main/java/testasyouthink/ThenStep.java b/test-as-you-think-core/src/main/java/testasyouthink/ThenStep.java index 0aad144..54c3299 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/ThenStep.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/ThenStep.java @@ -150,4 +150,12 @@ public ThenFailureWithExpectedMessage becauseOf(Class expec public void withMessage(String expectedMessage) { assertThat(((Throwable) context.returnResultOrVoid()).getMessage()).isEqualTo(expectedMessage); } + + @Override + public AndThen<$SystemUnderTest, $Result> thenSutRepliesWithin(long timeLimit) { + new Assertion() + .assertThat(context::returnResultOrVoid) + .spendAtMost(timeLimit); + return this; + } } diff --git a/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java b/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java index 8e613c6..e07c137 100644 --- a/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java +++ b/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java @@ -74,4 +74,17 @@ public void should_fail_when_the_computation_throws_an_exception_given_a_void_me .hasMessage("the computation threw an exception") .hasCauseInstanceOf(ExecutionException.class); } + + @Test + public void should_fail_given_a_too_slow_non_void_method() { + assertThatThrownBy(() -> givenSutClass(SystemUnderTest.class) + .when(sut -> { + sleep(1000); + return null; + }) + .thenSutRepliesWithin(1)) + .isInstanceOf(AssertionError.class) + .hasMessage("test timed out after 1 milliseconds") + .hasCauseInstanceOf(TimeoutException.class); + } } From 4b2d7535c719a5accc5b8891ae943bbf1b78423b Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Tue, 20 Jun 2017 18:33:33 +0200 Subject: [PATCH 07/31] Refactor the custom assertion to make it compliant with AssertJ. Source: http://joel-costigliola.github.io/assertj/assertj-core-custom-assertions.html --- .../main/java/testasyouthink/ThenStep.java | 18 +++++------ .../testasyouthink/ThenWithoutResultStep.java | 5 ++-- .../verification/Assertions.java | 30 +++++++++++++++++++ .../RunnableAssert.java} | 19 +++++++----- 4 files changed, 51 insertions(+), 21 deletions(-) create mode 100644 test-as-you-think-core/src/main/java/testasyouthink/verification/Assertions.java rename test-as-you-think-core/src/main/java/testasyouthink/{Assertion.java => verification/RunnableAssert.java} (81%) diff --git a/test-as-you-think-core/src/main/java/testasyouthink/ThenStep.java b/test-as-you-think-core/src/main/java/testasyouthink/ThenStep.java index 54c3299..ad54cfb 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/ThenStep.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/ThenStep.java @@ -27,6 +27,7 @@ import testasyouthink.GivenWhenThenDsl.VerificationStage.ThenFailure; import testasyouthink.GivenWhenThenDsl.VerificationStage.ThenFailureWithExpectedException; import testasyouthink.GivenWhenThenDsl.VerificationStage.ThenFailureWithExpectedMessage; +import testasyouthink.verification.Assertions; import java.util.List; import java.util.function.BiConsumer; @@ -41,19 +42,14 @@ public class ThenStep<$SystemUnderTest, $Result> implements Then<$SystemUnderTes ThenFailureWithExpectedMessage { private final GivenWhenContext<$SystemUnderTest, $Result> context; - private $Result result; ThenStep(GivenWhenContext<$SystemUnderTest, $Result> context) { this.context = context; } - private $Result result() { - return result == null ? result = context.returnResultOrVoid() : result; - } - @Override public AndThen<$SystemUnderTest, $Result> then(Consumer<$Result> thenStep) { - thenStep.accept(result()); + thenStep.accept(context.returnResultOrVoid()); return this; } @@ -64,7 +60,7 @@ public void then(BiConsumer<$SystemUnderTest, $Result> thenStep) { @Override public AndThen<$SystemUnderTest, $Result> then(Runnable thenStep) { - result(); + context.returnResultOrVoid(); thenStep.run(); return this; } @@ -83,7 +79,7 @@ public void then(BiConsumer<$SystemUnderTest, $Result> thenStep) { @Override public AndThen<$SystemUnderTest, $Result> then(Predicate<$Result> thenStep) { - assertThat(thenStep.test(result())).isTrue(); + assertThat(thenStep.test(context.returnResultOrVoid())).isTrue(); return this; } @@ -109,7 +105,7 @@ public void then(Predicate<$Result> thenStepAboutResult, Predicate<$SystemUnderT @Override public AndThen<$SystemUnderTest, $Result> and(Consumer<$Result> thenStep) { - thenStep.accept(result()); + thenStep.accept(context.returnResultOrVoid()); return this; } @@ -153,9 +149,9 @@ public void withMessage(String expectedMessage) { @Override public AndThen<$SystemUnderTest, $Result> thenSutRepliesWithin(long timeLimit) { - new Assertion() + Assertions .assertThat(context::returnResultOrVoid) - .spendAtMost(timeLimit); + .spendsAtMost(timeLimit); return this; } } diff --git a/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java b/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java index 89444d8..278372e 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java @@ -24,6 +24,7 @@ import testasyouthink.GivenWhenThenDsl.VerificationStage.AndThenWithoutResult; import testasyouthink.GivenWhenThenDsl.VerificationStage.ThenWithoutResult; +import testasyouthink.verification.Assertions; import java.util.function.BooleanSupplier; import java.util.function.Consumer; @@ -106,9 +107,9 @@ public ThenWithoutResultStep(GivenWhenContext<$SystemUnderTest, Void> context) { @Override public AndThenWithoutResult<$SystemUnderTest> thenSutRepliesWithin(long timeLimit) { - new Assertion() + Assertions .assertThat(context::returnResultOrVoid) - .spendAtMost(timeLimit); + .spendsAtMost(timeLimit); return this; } } diff --git a/test-as-you-think-core/src/main/java/testasyouthink/verification/Assertions.java b/test-as-you-think-core/src/main/java/testasyouthink/verification/Assertions.java new file mode 100644 index 0000000..12c8b60 --- /dev/null +++ b/test-as-you-think-core/src/main/java/testasyouthink/verification/Assertions.java @@ -0,0 +1,30 @@ +/*- + * #%L + * Test As You Think + * %% + * Copyright (C) 2017 Xavier Pigeon and TestAsYouThink contributors + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * . + * #L% + */ + +package testasyouthink.verification; + +public class Assertions { + + public static RunnableAssert assertThat(Runnable runnable) { + return new RunnableAssert(runnable); + } +} diff --git a/test-as-you-think-core/src/main/java/testasyouthink/Assertion.java b/test-as-you-think-core/src/main/java/testasyouthink/verification/RunnableAssert.java similarity index 81% rename from test-as-you-think-core/src/main/java/testasyouthink/Assertion.java rename to test-as-you-think-core/src/main/java/testasyouthink/verification/RunnableAssert.java index 20caf1c..fdf023c 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/Assertion.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/verification/RunnableAssert.java @@ -20,7 +20,9 @@ * #L% */ -package testasyouthink; +package testasyouthink.verification; + +import org.assertj.core.api.AbstractAssert; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; @@ -29,18 +31,19 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -class Assertion { +public class RunnableAssert extends AbstractAssert { - private Runnable runnable; + RunnableAssert(Runnable actual) { + super(actual, RunnableAssert.class); + } - Assertion assertThat(Runnable runnable) { - this.runnable = runnable; - return this; + public static RunnableAssert assertThat(Runnable actual) { + return new RunnableAssert(actual); } - void spendAtMost(long timeLimit) { + public void spendsAtMost(long timeLimit) { FutureTask futureTask = new FutureTask<>(() -> { - runnable.run(); + actual.run(); return null; }); ExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); From cff0d0e73183e6a75881ecc3cdd1aef3f7edde9f Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Tue, 20 Jun 2017 19:01:35 +0200 Subject: [PATCH 08/31] It should fail given a duration limit for a void method. --- .../java/testasyouthink/GivenWhenThenDsl.java | 3 +++ .../testasyouthink/ThenWithoutResultStep.java | 9 +++++++++ .../verification/RunnableAssert.java | 9 ++++++++- .../ThenSutRepliesWithinTimeLimitTest.java | 15 ++++++++++++++- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java index a2effb8..88f097d 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java @@ -38,6 +38,7 @@ import testasyouthink.function.CheckedTriConsumer; import testasyouthink.function.CheckedTriFunction; +import java.time.Duration; import java.util.List; import java.util.function.BiConsumer; import java.util.function.BiPredicate; @@ -206,6 +207,8 @@ interface ThenWithoutResult<$SystemUnderTest> { AndThenWithoutResult<$SystemUnderTest> then(BooleanSupplier thenStep); AndThenWithoutResult<$SystemUnderTest> thenSutRepliesWithin(long timeLimit); + + AndThenWithoutResult<$SystemUnderTest> thenSutRepliesWithin(Duration duration); } interface AndThenWithoutResult<$SystemUnderTest> { diff --git a/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java b/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java index 278372e..c5681dc 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/ThenWithoutResultStep.java @@ -26,6 +26,7 @@ import testasyouthink.GivenWhenThenDsl.VerificationStage.ThenWithoutResult; import testasyouthink.verification.Assertions; +import java.time.Duration; import java.util.function.BooleanSupplier; import java.util.function.Consumer; @@ -112,4 +113,12 @@ public ThenWithoutResultStep(GivenWhenContext<$SystemUnderTest, Void> context) { .spendsAtMost(timeLimit); return this; } + + @Override + public AndThenWithoutResult<$SystemUnderTest> thenSutRepliesWithin(Duration duration) { + Assertions + .assertThat(context::returnResultOrVoid) + .spendsAtMost(duration); + return this; + } } diff --git a/test-as-you-think-core/src/main/java/testasyouthink/verification/RunnableAssert.java b/test-as-you-think-core/src/main/java/testasyouthink/verification/RunnableAssert.java index fdf023c..94a6e87 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/verification/RunnableAssert.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/verification/RunnableAssert.java @@ -24,6 +24,7 @@ import org.assertj.core.api.AbstractAssert; +import java.time.Duration; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -41,7 +42,7 @@ public static RunnableAssert assertThat(Runnable actual) { return new RunnableAssert(actual); } - public void spendsAtMost(long timeLimit) { + public RunnableAssert spendsAtMost(long timeLimit) { FutureTask futureTask = new FutureTask<>(() -> { actual.run(); return null; @@ -59,5 +60,11 @@ public void spendsAtMost(long timeLimit) { } finally { executorService.shutdownNow(); } + return this; + } + + public RunnableAssert spendsAtMost(Duration duration) { + spendsAtMost(duration.toMillis()); + return this; } } diff --git a/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java b/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java index e07c137..58d1160 100644 --- a/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java +++ b/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java @@ -26,6 +26,7 @@ import testasyouthink.fixture.SystemUnderTest; import testasyouthink.function.CheckedConsumer; +import java.time.Duration; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeoutException; @@ -37,7 +38,7 @@ public class ThenSutRepliesWithinTimeLimitTest { @Test - public void should_fail_given_a_too_slow_void_method() { + public void should_fail_given_a_time_limit_for_a_void_method() { assertThatThrownBy(() -> givenSutClass(SystemUnderTest.class) .when(sut -> { sleep(1000); @@ -48,6 +49,18 @@ public void should_fail_given_a_too_slow_void_method() { .hasCauseInstanceOf(TimeoutException.class); } + @Test + public void should_fail_given_a_duration_limit_for_a_void_method() { + assertThatThrownBy(() -> givenSutClass(SystemUnderTest.class) + .when(sut -> { + sleep(1000); + }) + .thenSutRepliesWithin(Duration.ofMillis(1))) + .isInstanceOf(AssertionError.class) + .hasMessage("test timed out after 1 milliseconds") + .hasCauseInstanceOf(TimeoutException.class); + } + @Test public void should_fail_when_the_current_thread_was_interrupted_while_waiting_given_a_void_method() { assertThatThrownBy(() -> givenSutClass(SystemUnderTest.class) From 48c4595293e3cda59f33d3e04a4dfc65166664e3 Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Tue, 20 Jun 2017 19:05:34 +0200 Subject: [PATCH 09/31] It should fail given a duration limit for a non-void method. --- .../java/testasyouthink/GivenWhenThenDsl.java | 2 ++ .../src/main/java/testasyouthink/ThenStep.java | 9 +++++++++ .../ThenSutRepliesWithinTimeLimitTest.java | 15 ++++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java index 88f097d..1125b36 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java @@ -171,6 +171,8 @@ interface Then<$SystemUnderTest, $Result> { AndThen<$SystemUnderTest, $Result> thenSutRepliesWithin(long timeLimit); + AndThen<$SystemUnderTest, $Result> thenSutRepliesWithin(Duration duration); + void then(List> thenSteps); void then(BiConsumer<$SystemUnderTest, $Result> thenStep); diff --git a/test-as-you-think-core/src/main/java/testasyouthink/ThenStep.java b/test-as-you-think-core/src/main/java/testasyouthink/ThenStep.java index ad54cfb..7f01e42 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/ThenStep.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/ThenStep.java @@ -29,6 +29,7 @@ import testasyouthink.GivenWhenThenDsl.VerificationStage.ThenFailureWithExpectedMessage; import testasyouthink.verification.Assertions; +import java.time.Duration; import java.util.List; import java.util.function.BiConsumer; import java.util.function.BiPredicate; @@ -154,4 +155,12 @@ public void withMessage(String expectedMessage) { .spendsAtMost(timeLimit); return this; } + + @Override + public AndThen<$SystemUnderTest, $Result> thenSutRepliesWithin(Duration duration) { + Assertions + .assertThat(context::returnResultOrVoid) + .spendsAtMost(duration); + return this; + } } diff --git a/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java b/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java index 58d1160..fe6a9f9 100644 --- a/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java +++ b/test-as-you-think-core/src/test/java/testasyouthink/ThenSutRepliesWithinTimeLimitTest.java @@ -89,7 +89,7 @@ public void should_fail_when_the_computation_throws_an_exception_given_a_void_me } @Test - public void should_fail_given_a_too_slow_non_void_method() { + public void should_fail_given_a_time_limit_for_a_non_void_method() { assertThatThrownBy(() -> givenSutClass(SystemUnderTest.class) .when(sut -> { sleep(1000); @@ -100,4 +100,17 @@ public void should_fail_given_a_too_slow_non_void_method() { .hasMessage("test timed out after 1 milliseconds") .hasCauseInstanceOf(TimeoutException.class); } + + @Test + public void should_fail_given_a_duration_limit_for_a_non_void_method() { + assertThatThrownBy(() -> givenSutClass(SystemUnderTest.class) + .when(sut -> { + sleep(1000); + return null; + }) + .thenSutRepliesWithin(Duration.ofMillis(1))) + .isInstanceOf(AssertionError.class) + .hasMessage("test timed out after 1 milliseconds") + .hasCauseInstanceOf(TimeoutException.class); + } } From 4c8cb177a25ef91c30428f6fdc8ba6a9076b9f2e Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Wed, 21 Jun 2017 20:54:14 +0200 Subject: [PATCH 10/31] It should accept a function as a lambda without ambiguity by using an intermediady method. --- pom.xml | 7 +++ test-as-you-think-core/pom.xml | 4 ++ .../java/testasyouthink/TestAsYouThink.java | 6 +++ .../WhenStepAsExpressionLambdaTest.java | 53 +++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java diff --git a/pom.xml b/pom.xml index 72c64ec..36451c1 100644 --- a/pom.xml +++ b/pom.xml @@ -364,6 +364,13 @@ 3.4 test + + + org.mockito + mockito-core + 2.8.47 + test + diff --git a/test-as-you-think-core/pom.xml b/test-as-you-think-core/pom.xml index b8aae87..3e55ca5 100644 --- a/test-as-you-think-core/pom.xml +++ b/test-as-you-think-core/pom.xml @@ -28,5 +28,9 @@ org.easymock easymock + + org.mockito + mockito-core + diff --git a/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java b/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java index 29a09e2..6d5b16c 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java @@ -23,6 +23,7 @@ package testasyouthink; import testasyouthink.GivenWhenThenDsl.PreparationStage.Given; +import testasyouthink.function.CheckedFunction; public class TestAsYouThink { @@ -37,4 +38,9 @@ public class TestAsYouThink { throw new RuntimeException(exception.getMessage()); } } + + public static <$SystemUnderTest, $Result> CheckedFunction<$SystemUnderTest, $Result> asFunction( + CheckedFunction<$SystemUnderTest, $Result> whenStep) { + return whenStep; + } } diff --git a/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java b/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java new file mode 100644 index 0000000..ec6af96 --- /dev/null +++ b/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java @@ -0,0 +1,53 @@ +/*- + * #%L + * Test As You Think + * %% + * Copyright (C) 2017 Xavier Pigeon and TestAsYouThink contributors + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * . + * #L% + */ + +package testasyouthink; + +import org.junit.Test; +import org.mockito.Mockito; +import testasyouthink.fixture.GivenWhenThenDefinition; +import testasyouthink.fixture.SystemUnderTest; + +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static testasyouthink.TestAsYouThink.asFunction; +import static testasyouthink.TestAsYouThink.givenSutClass; + +public class WhenStepAsExpressionLambdaTest { + + @Test + public void should_accept_a_function_as_a_lambda_without_ambiguity_by_using_an_intermediary_method() { + // GIVEN + GivenWhenThenDefinition givenWhenThenDefinitionMock = Mockito.mock(GivenWhenThenDefinition.class); + + // WHEN + givenSutClass(SystemUnderTest.class) + .when(asFunction(sut -> sut.nonVoidMethod())) + .then(result -> { + givenWhenThenDefinitionMock.thenTheActualResultIsInKeepingWithTheExpectedResult(); + }); + + // THEN + verify(givenWhenThenDefinitionMock).thenTheActualResultIsInKeepingWithTheExpectedResult(); + verifyNoMoreInteractions(givenWhenThenDefinitionMock); + } +} From 13a18fe2d61ef392518ed5603cef4dac5b8fb795 Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Wed, 21 Jun 2017 21:47:13 +0200 Subject: [PATCH 11/31] It should accept a function as a lambda without ambiguity by using an alternate when method. --- .../java/testasyouthink/GivenWhenSteps.java | 6 +++ .../java/testasyouthink/GivenWhenThenDsl.java | 3 ++ .../java/testasyouthink/TestAsYouThink.java | 2 +- .../WhenStepAsExpressionLambdaTest.java | 41 ++++++++++++++----- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenSteps.java b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenSteps.java index d7e1beb..d85fc48 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenSteps.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenSteps.java @@ -103,6 +103,12 @@ public class GivenWhenSteps<$SystemUnderTest> implements Given<$SystemUnderTest> return thenStepFactory.createThenStep(preparation, whenStep); } + @Override + public <$Result> Then<$SystemUnderTest, $Result> whenSutReturns( + CheckedFunction<$SystemUnderTest, $Result> whenStep) { + return when(whenStep); + } + @Override public ThenWithoutResult<$SystemUnderTest> when(CheckedConsumer<$SystemUnderTest> whenStep) { return thenStepFactory.createThenStep(preparation, whenStep); diff --git a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java index 1125b36..570ddf2 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java @@ -115,6 +115,9 @@ interface When<$SystemUnderTest> { <$Result> Then<$SystemUnderTest, $Result> when(CheckedFunction<$SystemUnderTest, $Result> whenStep); + <$Result> Then<$SystemUnderTest, $Result> whenSutReturns( + CheckedFunction<$SystemUnderTest, $Result> whenStep); + ThenWithoutResult<$SystemUnderTest> when(CheckedConsumer<$SystemUnderTest> whenStep); ThenFailure whenSutRunsOutsideOperatingConditions(CheckedConsumer<$SystemUnderTest> whenStep); diff --git a/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java b/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java index 6d5b16c..37cf1b9 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java @@ -39,7 +39,7 @@ public class TestAsYouThink { } } - public static <$SystemUnderTest, $Result> CheckedFunction<$SystemUnderTest, $Result> asFunction( + public static <$SystemUnderTest, $Result> CheckedFunction<$SystemUnderTest, $Result> withReturn( CheckedFunction<$SystemUnderTest, $Result> whenStep) { return whenStep; } diff --git a/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java b/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java index ec6af96..5d73244 100644 --- a/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java +++ b/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java @@ -22,32 +22,51 @@ package testasyouthink; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; import testasyouthink.fixture.GivenWhenThenDefinition; import testasyouthink.fixture.SystemUnderTest; +import java.util.function.Consumer; + import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; -import static testasyouthink.TestAsYouThink.asFunction; import static testasyouthink.TestAsYouThink.givenSutClass; +import static testasyouthink.TestAsYouThink.withReturn; public class WhenStepAsExpressionLambdaTest { - @Test - public void should_accept_a_function_as_a_lambda_without_ambiguity_by_using_an_intermediary_method() { - // GIVEN - GivenWhenThenDefinition givenWhenThenDefinitionMock = Mockito.mock(GivenWhenThenDefinition.class); + private GivenWhenThenDefinition givenWhenThenDefinitionMock; - // WHEN - givenSutClass(SystemUnderTest.class) - .when(asFunction(sut -> sut.nonVoidMethod())) - .then(result -> { - givenWhenThenDefinitionMock.thenTheActualResultIsInKeepingWithTheExpectedResult(); - }); + @Before + public void prepareFixtures() { + givenWhenThenDefinitionMock = Mockito.mock(GivenWhenThenDefinition.class); + } + @After + public void verifyMocks() { // THEN verify(givenWhenThenDefinitionMock).thenTheActualResultIsInKeepingWithTheExpectedResult(); verifyNoMoreInteractions(givenWhenThenDefinitionMock); } + + @Test + public void should_accept_a_function_as_a_lambda_without_ambiguity_by_using_an_intermediary_method() { + // WHEN + givenSutClass(SystemUnderTest.class) + .when(withReturn(sut -> sut.nonVoidMethod())) + .then((Consumer) result -> givenWhenThenDefinitionMock + .thenTheActualResultIsInKeepingWithTheExpectedResult()); + } + + @Test + public void should_accept_a_function_as_a_lambda_without_ambiguity_by_using_an_alternate_when_method() { + // WHEN + givenSutClass(SystemUnderTest.class) + .whenSutReturns(sut -> sut.nonVoidMethod()) + .then((Consumer) result -> givenWhenThenDefinitionMock + .thenTheActualResultIsInKeepingWithTheExpectedResult()); + } } From 3ef6d8bb41c3625b4bcd40c4b28350faaa18bbdd Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Wed, 21 Jun 2017 23:00:18 +0200 Subject: [PATCH 12/31] It should accept a consumer as a lambda without ambiguity by using an alternate when method. --- .../src/main/java/testasyouthink/GivenWhenSteps.java | 5 +++++ .../src/main/java/testasyouthink/GivenWhenThenDsl.java | 2 ++ .../testasyouthink/WhenStepAsExpressionLambdaTest.java | 8 ++++++++ 3 files changed, 15 insertions(+) diff --git a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenSteps.java b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenSteps.java index d85fc48..9bdac1d 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenSteps.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenSteps.java @@ -114,6 +114,11 @@ public class GivenWhenSteps<$SystemUnderTest> implements Given<$SystemUnderTest> return thenStepFactory.createThenStep(preparation, whenStep); } + @Override + public ThenWithoutResult<$SystemUnderTest> whenSutRuns(CheckedConsumer<$SystemUnderTest> whenStep) { + return when(whenStep); + } + @Override public ThenFailure whenSutRunsOutsideOperatingConditions(CheckedConsumer<$SystemUnderTest> whenStep) { return thenStepFactory.createThenStep(preparation, functions.toFunctionWithThrowableAsResult(whenStep)); diff --git a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java index 570ddf2..2477177 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java @@ -120,6 +120,8 @@ interface When<$SystemUnderTest> { ThenWithoutResult<$SystemUnderTest> when(CheckedConsumer<$SystemUnderTest> whenStep); + ThenWithoutResult<$SystemUnderTest> whenSutRuns(CheckedConsumer<$SystemUnderTest> whenStep); + ThenFailure whenSutRunsOutsideOperatingConditions(CheckedConsumer<$SystemUnderTest> whenStep); } diff --git a/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java b/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java index 5d73244..7910586 100644 --- a/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java +++ b/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java @@ -69,4 +69,12 @@ public void should_accept_a_function_as_a_lambda_without_ambiguity_by_using_an_a .then((Consumer) result -> givenWhenThenDefinitionMock .thenTheActualResultIsInKeepingWithTheExpectedResult()); } + + @Test + public void should_accept_a_consumer_as_a_lambda_without_ambiguity_by_using_an_alternate_when_method() { + // WHEN + givenSutClass(SystemUnderTest.class) + .whenSutRuns(sut -> sut.voidMethod()) + .then(() -> givenWhenThenDefinitionMock.thenTheActualResultIsInKeepingWithTheExpectedResult()); + } } From 4b0de42ab177c7afb08743c4bfbfff53709491c6 Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Thu, 22 Jun 2017 02:07:35 +0200 Subject: [PATCH 13/31] It should accept a bifunction as a lambda without ambiguity by using an alternate when method. --- .../java/testasyouthink/GivenArgumentWhenSteps.java | 6 ++++++ .../main/java/testasyouthink/GivenWhenThenDsl.java | 3 +++ .../WhenStepAsExpressionLambdaTest.java | 11 +++++++++++ 3 files changed, 20 insertions(+) diff --git a/test-as-you-think-core/src/main/java/testasyouthink/GivenArgumentWhenSteps.java b/test-as-you-think-core/src/main/java/testasyouthink/GivenArgumentWhenSteps.java index c651efa..c50ba1a 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/GivenArgumentWhenSteps.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/GivenArgumentWhenSteps.java @@ -75,6 +75,12 @@ public class GivenArgumentWhenSteps<$SystemUnderTest, $Argument> implements AndG functions.toFunction(whenStep, preparation.getArgumentSuppliers())); } + @Override + public <$Result> Then<$SystemUnderTest, $Result> whenSutReturns( + CheckedBiFunction<$SystemUnderTest, $Argument, $Result> whenStep) { + return when(whenStep); + } + @Override public ThenFailure whenSutRunsOutsideOperatingConditions(CheckedBiConsumer<$SystemUnderTest, $Argument> whenStep) { return thenStepFactory.createThenStep(preparation, functions.toFunctionWithThrowableAsResult( diff --git a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java index 2477177..e770624 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java @@ -132,6 +132,9 @@ interface WhenApplyingOneArgument<$SystemUnderTest, $Argument> { <$Result> Then<$SystemUnderTest, $Result> when( CheckedBiFunction<$SystemUnderTest, $Argument, $Result> whenStep); + <$Result> Then<$SystemUnderTest, $Result> whenSutReturns( + CheckedBiFunction<$SystemUnderTest, $Argument, $Result> whenStep); + ThenFailure whenSutRunsOutsideOperatingConditions(CheckedBiConsumer<$SystemUnderTest, $Argument> whenStep); } diff --git a/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java b/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java index 7910586..4f42584 100644 --- a/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java +++ b/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java @@ -70,6 +70,17 @@ public void should_accept_a_function_as_a_lambda_without_ambiguity_by_using_an_a .thenTheActualResultIsInKeepingWithTheExpectedResult()); } + @Test + public void should_accept_a_bifunction_as_a_lambda_without_ambiguity_by_using_an_alternate_when_method() { + // WHEN + givenSutClass(SystemUnderTest.class) + .givenArgument("one argument", "argument") + .whenSutReturns((sut, argument) -> sut.nonVoidMethodWithParameter(argument)) + .then(result -> { + givenWhenThenDefinitionMock.thenTheActualResultIsInKeepingWithTheExpectedResult(); + }); + } + @Test public void should_accept_a_consumer_as_a_lambda_without_ambiguity_by_using_an_alternate_when_method() { // WHEN From 5dda79d689fd58fff6e7e6d62982d5a7c147ff60 Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Thu, 22 Jun 2017 02:14:20 +0200 Subject: [PATCH 14/31] It should accept a trifunction as a lambda without ambiguity by using an alternate when method. --- .../GivenTwoArgumentsWhenSteps.java | 6 ++++++ .../java/testasyouthink/GivenWhenThenDsl.java | 3 +++ .../WhenStepAsExpressionLambdaTest.java | 16 +++++++++++++--- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/test-as-you-think-core/src/main/java/testasyouthink/GivenTwoArgumentsWhenSteps.java b/test-as-you-think-core/src/main/java/testasyouthink/GivenTwoArgumentsWhenSteps.java index da208c8..5ff28d5 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/GivenTwoArgumentsWhenSteps.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/GivenTwoArgumentsWhenSteps.java @@ -78,6 +78,12 @@ public class GivenTwoArgumentsWhenSteps<$SystemUnderTest, $Argument1, $Argument2 functions.toFunction(whenStep, preparation.getArgumentSuppliers())); } + @Override + public <$Result> Then<$SystemUnderTest, $Result> whenSutReturns( + CheckedTriFunction<$SystemUnderTest, $Argument1, $Argument2, $Result> whenStep) { + return when(whenStep); + } + @Override public ThenFailure whenSutRunsOutsideOperatingConditions( CheckedTriConsumer<$SystemUnderTest, $Argument1, $Argument2> whenStep) { diff --git a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java index e770624..91a78a3 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java @@ -146,6 +146,9 @@ interface WhenApplyingTwoArguments<$SystemUnderTest, $Argument1, $Argument2> { <$Result> Then<$SystemUnderTest, $Result> when( CheckedTriFunction<$SystemUnderTest, $Argument1, $Argument2, $Result> whenStep); + <$Result> Then<$SystemUnderTest, $Result> whenSutReturns( + CheckedTriFunction<$SystemUnderTest, $Argument1, $Argument2, $Result> whenStep); + ThenFailure whenSutRunsOutsideOperatingConditions( CheckedTriConsumer<$SystemUnderTest, $Argument1, $Argument2> whenStep); } diff --git a/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java b/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java index 4f42584..64836c2 100644 --- a/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java +++ b/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java @@ -76,9 +76,19 @@ public void should_accept_a_bifunction_as_a_lambda_without_ambiguity_by_using_an givenSutClass(SystemUnderTest.class) .givenArgument("one argument", "argument") .whenSutReturns((sut, argument) -> sut.nonVoidMethodWithParameter(argument)) - .then(result -> { - givenWhenThenDefinitionMock.thenTheActualResultIsInKeepingWithTheExpectedResult(); - }); + .then((Consumer) result -> givenWhenThenDefinitionMock + .thenTheActualResultIsInKeepingWithTheExpectedResult()); + } + + @Test + public void should_accept_a_trifunction_as_a_lambda_without_ambiguity_by_using_an_alternate_when_method() { + // WHEN + givenSutClass(SystemUnderTest.class) + .givenArgument("one argument", "argument") + .andArgument("second argument", 20170622) + .whenSutReturns((sut, argument1, argument2) -> sut.nonVoidMethodWithTwoParameters(argument1, argument2)) + .then((Consumer) result -> givenWhenThenDefinitionMock + .thenTheActualResultIsInKeepingWithTheExpectedResult()); } @Test From 58e58a76a1ea0c0e47685c59deba041a52069bea Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Thu, 22 Jun 2017 02:17:48 +0200 Subject: [PATCH 15/31] It should accept a quadrifunction as a lambda without ambiguity by using an alternate when method. --- .../GivenThreeArgumentsWhenSteps.java | 6 ++++++ .../main/java/testasyouthink/GivenWhenThenDsl.java | 3 +++ .../WhenStepAsExpressionLambdaTest.java | 14 ++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/test-as-you-think-core/src/main/java/testasyouthink/GivenThreeArgumentsWhenSteps.java b/test-as-you-think-core/src/main/java/testasyouthink/GivenThreeArgumentsWhenSteps.java index 74de492..ffcea9e 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/GivenThreeArgumentsWhenSteps.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/GivenThreeArgumentsWhenSteps.java @@ -55,6 +55,12 @@ public class GivenThreeArgumentsWhenSteps<$SystemUnderTest, $Argument1, $Argumen functions.toFunction(whenStep, preparation.getArgumentSuppliers())); } + @Override + public <$Result> Then<$SystemUnderTest, $Result> whenSutReturns( + CheckedQuadriFunction<$SystemUnderTest, $Argument1, $Argument2, $Argument3, $Result> whenStep) { + return when(whenStep); + } + @Override public ThenFailure whenSutRunsOutsideOperatingConditions( CheckedQuadriConsumer<$SystemUnderTest, $Argument1, $Argument2, $Argument3> whenStep) { diff --git a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java index 91a78a3..1bc9d99 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java @@ -161,6 +161,9 @@ interface WhenApplyingThreeArguments<$SystemUnderTest, $Argument1, $Argument2, $ <$Result> Then<$SystemUnderTest, $Result> when( CheckedQuadriFunction<$SystemUnderTest, $Argument1, $Argument2, $Argument3, $Result> whenStep); + <$Result> Then<$SystemUnderTest, $Result> whenSutReturns( + CheckedQuadriFunction<$SystemUnderTest, $Argument1, $Argument2, $Argument3, $Result> whenStep); + ThenFailure whenSutRunsOutsideOperatingConditions( CheckedQuadriConsumer<$SystemUnderTest, $Argument1, $Argument2, $Argument3> whenStep); } diff --git a/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java b/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java index 64836c2..121d3fe 100644 --- a/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java +++ b/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java @@ -91,6 +91,20 @@ public void should_accept_a_trifunction_as_a_lambda_without_ambiguity_by_using_a .thenTheActualResultIsInKeepingWithTheExpectedResult()); } + @Test + public void should_accept_a_quadrifunction_as_a_lambda_without_ambiguity_by_using_an_alternate_when_method() { + // WHEN + givenSutClass(SystemUnderTest.class) + .givenArgument("one argument", "argument") + .andArgument("second argument", 20170622) + .andArgument("third argument", true) + .whenSutReturns( + (sut, argument1, argument2, argument3) -> sut.nonVoidMethodWithThreeParameters(argument1, + argument2, argument3)) + .then((Consumer) result -> givenWhenThenDefinitionMock + .thenTheActualResultIsInKeepingWithTheExpectedResult()); + } + @Test public void should_accept_a_consumer_as_a_lambda_without_ambiguity_by_using_an_alternate_when_method() { // WHEN From e8b7ba52fd5ed912d7942a1eb506d3c82e680ab2 Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Thu, 22 Jun 2017 12:00:22 +0200 Subject: [PATCH 16/31] It should accept a biconsumer as a lambda without ambiguity by using an alternate when method. --- .../java/testasyouthink/GivenArgumentWhenSteps.java | 5 +++++ .../main/java/testasyouthink/GivenWhenThenDsl.java | 2 ++ .../WhenStepAsExpressionLambdaTest.java | 13 +++++++++++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/test-as-you-think-core/src/main/java/testasyouthink/GivenArgumentWhenSteps.java b/test-as-you-think-core/src/main/java/testasyouthink/GivenArgumentWhenSteps.java index c50ba1a..e1f7e2b 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/GivenArgumentWhenSteps.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/GivenArgumentWhenSteps.java @@ -68,6 +68,11 @@ public class GivenArgumentWhenSteps<$SystemUnderTest, $Argument> implements AndG functions.toConsumer(whenStep, preparation.getArgumentSuppliers())); } + @Override + public ThenWithoutResult<$SystemUnderTest> whenSutRuns(CheckedBiConsumer<$SystemUnderTest, $Argument> whenStep) { + return when(whenStep); + } + @Override public <$Result> Then<$SystemUnderTest, $Result> when( CheckedBiFunction<$SystemUnderTest, $Argument, $Result> whenStep) { diff --git a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java index 1bc9d99..5d303a3 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java @@ -129,6 +129,8 @@ interface WhenApplyingOneArgument<$SystemUnderTest, $Argument> { ThenWithoutResult<$SystemUnderTest> when(CheckedBiConsumer<$SystemUnderTest, $Argument> whenStep); + ThenWithoutResult<$SystemUnderTest> whenSutRuns(CheckedBiConsumer<$SystemUnderTest, $Argument> whenStep); + <$Result> Then<$SystemUnderTest, $Result> when( CheckedBiFunction<$SystemUnderTest, $Argument, $Result> whenStep); diff --git a/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java b/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java index 121d3fe..c16f4b6 100644 --- a/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java +++ b/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java @@ -84,7 +84,7 @@ public void should_accept_a_bifunction_as_a_lambda_without_ambiguity_by_using_an public void should_accept_a_trifunction_as_a_lambda_without_ambiguity_by_using_an_alternate_when_method() { // WHEN givenSutClass(SystemUnderTest.class) - .givenArgument("one argument", "argument") + .givenArgument("first argument", "argument") .andArgument("second argument", 20170622) .whenSutReturns((sut, argument1, argument2) -> sut.nonVoidMethodWithTwoParameters(argument1, argument2)) .then((Consumer) result -> givenWhenThenDefinitionMock @@ -95,7 +95,7 @@ public void should_accept_a_trifunction_as_a_lambda_without_ambiguity_by_using_a public void should_accept_a_quadrifunction_as_a_lambda_without_ambiguity_by_using_an_alternate_when_method() { // WHEN givenSutClass(SystemUnderTest.class) - .givenArgument("one argument", "argument") + .givenArgument("first argument", "argument") .andArgument("second argument", 20170622) .andArgument("third argument", true) .whenSutReturns( @@ -112,4 +112,13 @@ public void should_accept_a_consumer_as_a_lambda_without_ambiguity_by_using_an_a .whenSutRuns(sut -> sut.voidMethod()) .then(() -> givenWhenThenDefinitionMock.thenTheActualResultIsInKeepingWithTheExpectedResult()); } + + @Test + public void should_accept_a_biconsumer_as_a_lambda_without_ambiguity_by_using_an_alternate_when_method() { + // WHEN + givenSutClass(SystemUnderTest.class) + .givenArgument("first argument", "argument") + .whenSutRuns((sut, argument) -> sut.voidMethodWithParameter(argument)) + .then(() -> givenWhenThenDefinitionMock.thenTheActualResultIsInKeepingWithTheExpectedResult()); + } } From ee5e63695bf396ae14e2840d85df748064c9d76b Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Thu, 22 Jun 2017 12:10:26 +0200 Subject: [PATCH 17/31] It should accept a triconsumer as a lambda without ambiguity by using an alternate when method. --- .../testasyouthink/GivenTwoArgumentsWhenSteps.java | 6 ++++++ .../main/java/testasyouthink/GivenWhenThenDsl.java | 3 +++ .../WhenStepAsExpressionLambdaTest.java | 12 +++++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/test-as-you-think-core/src/main/java/testasyouthink/GivenTwoArgumentsWhenSteps.java b/test-as-you-think-core/src/main/java/testasyouthink/GivenTwoArgumentsWhenSteps.java index 5ff28d5..0cb6d02 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/GivenTwoArgumentsWhenSteps.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/GivenTwoArgumentsWhenSteps.java @@ -71,6 +71,12 @@ public class GivenTwoArgumentsWhenSteps<$SystemUnderTest, $Argument1, $Argument2 functions.toConsumer(whenStep, preparation.getArgumentSuppliers())); } + @Override + public ThenWithoutResult<$SystemUnderTest> whenSutRuns( + CheckedTriConsumer<$SystemUnderTest, $Argument1, $Argument2> whenStep) { + return when(whenStep); + } + @Override public <$Result> Then<$SystemUnderTest, $Result> when( CheckedTriFunction<$SystemUnderTest, $Argument1, $Argument2, $Result> whenStep) { diff --git a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java index 5d303a3..9ca146b 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java @@ -145,6 +145,9 @@ interface WhenApplyingTwoArguments<$SystemUnderTest, $Argument1, $Argument2> { ThenWithoutResult<$SystemUnderTest> when( CheckedTriConsumer<$SystemUnderTest, $Argument1, $Argument2> whenStep); + ThenWithoutResult<$SystemUnderTest> whenSutRuns( + CheckedTriConsumer<$SystemUnderTest, $Argument1, $Argument2> whenStep); + <$Result> Then<$SystemUnderTest, $Result> when( CheckedTriFunction<$SystemUnderTest, $Argument1, $Argument2, $Result> whenStep); diff --git a/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java b/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java index c16f4b6..d7af34f 100644 --- a/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java +++ b/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java @@ -117,8 +117,18 @@ public void should_accept_a_consumer_as_a_lambda_without_ambiguity_by_using_an_a public void should_accept_a_biconsumer_as_a_lambda_without_ambiguity_by_using_an_alternate_when_method() { // WHEN givenSutClass(SystemUnderTest.class) - .givenArgument("first argument", "argument") + .givenArgument("one argument", "argument") .whenSutRuns((sut, argument) -> sut.voidMethodWithParameter(argument)) .then(() -> givenWhenThenDefinitionMock.thenTheActualResultIsInKeepingWithTheExpectedResult()); } + + @Test + public void should_accept_a_triconsumer_as_a_lambda_without_ambiguity_by_using_an_alternate_when_method() { + // WHEN + givenSutClass(SystemUnderTest.class) + .givenArgument("first argument", "argument") + .andArgument("second argument", 20170622) + .whenSutRuns((sut, argument1, argument2) -> sut.voidMethodWithTwoParameters(argument1, argument2)) + .then(() -> givenWhenThenDefinitionMock.thenTheActualResultIsInKeepingWithTheExpectedResult()); + } } From 3b572a929d513ba4f9c3ca5a05c46a070281c97c Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Thu, 22 Jun 2017 12:18:50 +0200 Subject: [PATCH 18/31] It should accept a quadriconsumer as a lambda without ambiguity by using an alternate when method. --- .../testasyouthink/GivenThreeArgumentsWhenSteps.java | 6 ++++++ .../main/java/testasyouthink/GivenWhenThenDsl.java | 3 +++ .../WhenStepAsExpressionLambdaTest.java | 12 ++++++++++++ 3 files changed, 21 insertions(+) diff --git a/test-as-you-think-core/src/main/java/testasyouthink/GivenThreeArgumentsWhenSteps.java b/test-as-you-think-core/src/main/java/testasyouthink/GivenThreeArgumentsWhenSteps.java index ffcea9e..d7cadcc 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/GivenThreeArgumentsWhenSteps.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/GivenThreeArgumentsWhenSteps.java @@ -48,6 +48,12 @@ public class GivenThreeArgumentsWhenSteps<$SystemUnderTest, $Argument1, $Argumen functions.toConsumer(whenStep, preparation.getArgumentSuppliers())); } + @Override + public ThenWithoutResult<$SystemUnderTest> whenSutRuns( + CheckedQuadriConsumer<$SystemUnderTest, $Argument1, $Argument2, $Argument3> whenStep) { + return when(whenStep); + } + @Override public <$Result> Then<$SystemUnderTest, $Result> when( CheckedQuadriFunction<$SystemUnderTest, $Argument1, $Argument2, $Argument3, $Result> whenStep) { diff --git a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java index 9ca146b..7051b92 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/GivenWhenThenDsl.java @@ -163,6 +163,9 @@ interface WhenApplyingThreeArguments<$SystemUnderTest, $Argument1, $Argument2, $ ThenWithoutResult<$SystemUnderTest> when( CheckedQuadriConsumer<$SystemUnderTest, $Argument1, $Argument2, $Argument3> whenStep); + ThenWithoutResult<$SystemUnderTest> whenSutRuns( + CheckedQuadriConsumer<$SystemUnderTest, $Argument1, $Argument2, $Argument3> whenStep); + <$Result> Then<$SystemUnderTest, $Result> when( CheckedQuadriFunction<$SystemUnderTest, $Argument1, $Argument2, $Argument3, $Result> whenStep); diff --git a/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java b/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java index d7af34f..edb9f25 100644 --- a/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java +++ b/test-as-you-think-core/src/test/java/testasyouthink/WhenStepAsExpressionLambdaTest.java @@ -131,4 +131,16 @@ public void should_accept_a_triconsumer_as_a_lambda_without_ambiguity_by_using_a .whenSutRuns((sut, argument1, argument2) -> sut.voidMethodWithTwoParameters(argument1, argument2)) .then(() -> givenWhenThenDefinitionMock.thenTheActualResultIsInKeepingWithTheExpectedResult()); } + + @Test + public void should_accept_a_quadriconsumer_as_a_lambda_without_ambiguity_by_using_an_alternate_when_method() { + // WHEN + givenSutClass(SystemUnderTest.class) + .givenArgument("first argument", "argument") + .andArgument("second argument", 20170622) + .andArgument("third argument", true) + .whenSutRuns((sut, argument1, argument2, argument3) -> sut.voidMethodWithThreeParameters(argument1, + argument2, argument3)) + .then(() -> givenWhenThenDefinitionMock.thenTheActualResultIsInKeepingWithTheExpectedResult()); + } } From 82c579e4790856a4898431d80e35d5348234b6c9 Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Thu, 22 Jun 2017 16:10:09 +0200 Subject: [PATCH 19/31] It should start with when given a void method. --- .../java/testasyouthink/TestAsYouThink.java | 9 +++ .../testasyouthink/StartingWithWhenTest.java | 55 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 test-as-you-think-core/src/test/java/testasyouthink/StartingWithWhenTest.java diff --git a/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java b/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java index 37cf1b9..ee25833 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java @@ -23,6 +23,7 @@ package testasyouthink; import testasyouthink.GivenWhenThenDsl.PreparationStage.Given; +import testasyouthink.function.CheckedConsumer; import testasyouthink.function.CheckedFunction; public class TestAsYouThink { @@ -39,6 +40,14 @@ public class TestAsYouThink { } } + public static ThenWithoutResultStep when(Runnable whenStep) { + Preparation nothingToPrepare = new Preparation<>(null); + CheckedConsumer whenStepAsVoidConsumer = Void -> whenStep.run(); + Event event = new Event<>(null, whenStepAsVoidConsumer); + GivenWhenContext context = new GivenWhenContext<>(nothingToPrepare, event); + return new ThenWithoutResultStep<>(context); + } + public static <$SystemUnderTest, $Result> CheckedFunction<$SystemUnderTest, $Result> withReturn( CheckedFunction<$SystemUnderTest, $Result> whenStep) { return whenStep; diff --git a/test-as-you-think-core/src/test/java/testasyouthink/StartingWithWhenTest.java b/test-as-you-think-core/src/test/java/testasyouthink/StartingWithWhenTest.java new file mode 100644 index 0000000..cb5fd36 --- /dev/null +++ b/test-as-you-think-core/src/test/java/testasyouthink/StartingWithWhenTest.java @@ -0,0 +1,55 @@ +/*- + * #%L + * Test As You Think + * %% + * Copyright (C) 2017 Xavier Pigeon and TestAsYouThink contributors + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * . + * #L% + */ + +package testasyouthink; + +import org.junit.Test; +import org.mockito.InOrder; +import org.mockito.Mockito; +import testasyouthink.fixture.GivenWhenThenDefinition; +import testasyouthink.fixture.SystemUnderTest; + +import static org.mockito.Mockito.mock; +import static testasyouthink.TestAsYouThink.when; + +public class StartingWithWhenTest { + + @Test + public void should_start_with_when_given_a_void_method() { + // GIVEN + SystemUnderTest sut = mock(SystemUnderTest.class); + GivenWhenThenDefinition gwtMock = mock(GivenWhenThenDefinition.class); + + // WHEN + when(sut::voidMethod).then(() -> gwtMock.thenTheActualResultIsInKeepingWithTheExpectedResult()); + + // THEN + InOrder inOrder = Mockito.inOrder(sut, gwtMock); + inOrder + .verify(sut) + .voidMethod(); + inOrder + .verify(gwtMock) + .thenTheActualResultIsInKeepingWithTheExpectedResult(); + inOrder.verifyNoMoreInteractions(); + } +} From 7b72c692f9ce8c9380c2cb81b7f3c1a15303a553 Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Thu, 22 Jun 2017 16:20:58 +0200 Subject: [PATCH 20/31] It should start with when given a non-void method. --- .../java/testasyouthink/TestAsYouThink.java | 11 ++++++++ .../testasyouthink/StartingWithWhenTest.java | 26 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java b/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java index ee25833..67ed3a9 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java @@ -23,9 +23,12 @@ package testasyouthink; import testasyouthink.GivenWhenThenDsl.PreparationStage.Given; +import testasyouthink.GivenWhenThenDsl.VerificationStage.Then; import testasyouthink.function.CheckedConsumer; import testasyouthink.function.CheckedFunction; +import java.util.function.Supplier; + public class TestAsYouThink { public static <$SystemUnderTest> Given<$SystemUnderTest> givenSut($SystemUnderTest systemUnderTest) { @@ -48,6 +51,14 @@ public static ThenWithoutResultStep when(Runnable whenStep) { return new ThenWithoutResultStep<>(context); } + public static <$Result> Then when(Supplier<$Result> whenStep) { + Preparation nothingToPrepare = new Preparation<>(null); + CheckedFunction whenStepAsFunction = Void -> whenStep.get(); + Event event = new Event<>(null, whenStepAsFunction); + GivenWhenContext context = new GivenWhenContext<>(nothingToPrepare, event); + return new ThenStep<>(context); + } + public static <$SystemUnderTest, $Result> CheckedFunction<$SystemUnderTest, $Result> withReturn( CheckedFunction<$SystemUnderTest, $Result> whenStep) { return whenStep; diff --git a/test-as-you-think-core/src/test/java/testasyouthink/StartingWithWhenTest.java b/test-as-you-think-core/src/test/java/testasyouthink/StartingWithWhenTest.java index cb5fd36..4f82580 100644 --- a/test-as-you-think-core/src/test/java/testasyouthink/StartingWithWhenTest.java +++ b/test-as-you-think-core/src/test/java/testasyouthink/StartingWithWhenTest.java @@ -28,7 +28,9 @@ import testasyouthink.fixture.GivenWhenThenDefinition; import testasyouthink.fixture.SystemUnderTest; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import static testasyouthink.TestAsYouThink.when; public class StartingWithWhenTest { @@ -52,4 +54,28 @@ public void should_start_with_when_given_a_void_method() { .thenTheActualResultIsInKeepingWithTheExpectedResult(); inOrder.verifyNoMoreInteractions(); } + + @Test + public void should_start_with_when_given_a_non_void_method() { + // GIVEN + SystemUnderTest sut = mock(SystemUnderTest.class); + when(sut.nonVoidMethod()).thenReturn("expected result"); + GivenWhenThenDefinition gwtMock = mock(GivenWhenThenDefinition.class); + + // WHEN + when(sut::nonVoidMethod).then(result -> { + assertThat(result).isEqualTo("expected result"); + gwtMock.thenTheActualResultIsInKeepingWithTheExpectedResult(); + }); + + // THEN + InOrder inOrder = Mockito.inOrder(sut, gwtMock); + inOrder + .verify(sut) + .nonVoidMethod(); + inOrder + .verify(gwtMock) + .thenTheActualResultIsInKeepingWithTheExpectedResult(); + inOrder.verifyNoMoreInteractions(); + } } From 2fbf707697efb1c2b9e82ee297e35d2d759020ba Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Thu, 22 Jun 2017 16:40:06 +0200 Subject: [PATCH 21/31] Refactor to delegate the then step creation. --- .../src/main/java/testasyouthink/TestAsYouThink.java | 12 ++++-------- .../main/java/testasyouthink/ThenStepFactory.java | 10 ++++++++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java b/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java index 67ed3a9..55b0963 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java @@ -31,6 +31,8 @@ public class TestAsYouThink { + private static ThenStepFactory thenStepFactory = ThenStepFactory.INSTANCE; + public static <$SystemUnderTest> Given<$SystemUnderTest> givenSut($SystemUnderTest systemUnderTest) { return new GivenWhenSteps<>(systemUnderTest); } @@ -44,19 +46,13 @@ public class TestAsYouThink { } public static ThenWithoutResultStep when(Runnable whenStep) { - Preparation nothingToPrepare = new Preparation<>(null); CheckedConsumer whenStepAsVoidConsumer = Void -> whenStep.run(); - Event event = new Event<>(null, whenStepAsVoidConsumer); - GivenWhenContext context = new GivenWhenContext<>(nothingToPrepare, event); - return new ThenWithoutResultStep<>(context); + return thenStepFactory.createThenStep(whenStepAsVoidConsumer); } public static <$Result> Then when(Supplier<$Result> whenStep) { - Preparation nothingToPrepare = new Preparation<>(null); CheckedFunction whenStepAsFunction = Void -> whenStep.get(); - Event event = new Event<>(null, whenStepAsFunction); - GivenWhenContext context = new GivenWhenContext<>(nothingToPrepare, event); - return new ThenStep<>(context); + return thenStepFactory.createThenStep(whenStepAsFunction); } public static <$SystemUnderTest, $Result> CheckedFunction<$SystemUnderTest, $Result> withReturn( diff --git a/test-as-you-think-core/src/main/java/testasyouthink/ThenStepFactory.java b/test-as-you-think-core/src/main/java/testasyouthink/ThenStepFactory.java index 983db89..a11efd6 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/ThenStepFactory.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/ThenStepFactory.java @@ -42,4 +42,14 @@ enum ThenStepFactory { GivenWhenContext<$SystemUnderTest, Void> context = new GivenWhenContext<>(preparation, event); return new ThenWithoutResultStep<>(context); } + + <$Result> ThenStep createThenStep(CheckedFunction whenStep) { + Preparation nothingToPrepare = new Preparation<>(null); + return createThenStep(nothingToPrepare, whenStep); + } + + ThenWithoutResultStep createThenStep(CheckedConsumer whenStep) { + Preparation nothingToPrepare = new Preparation<>(null); + return createThenStep(nothingToPrepare, whenStep); + } } From 5c091163fa50b126438dd44237e4a3b9b2e95415 Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Thu, 22 Jun 2017 16:56:09 +0200 Subject: [PATCH 22/31] Refactor to delegate the lambda transformations. --- .../src/main/java/testasyouthink/TestAsYouThink.java | 9 ++++----- .../src/main/java/testasyouthink/function/Functions.java | 8 ++++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java b/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java index 55b0963..f84e5f5 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/TestAsYouThink.java @@ -24,13 +24,14 @@ import testasyouthink.GivenWhenThenDsl.PreparationStage.Given; import testasyouthink.GivenWhenThenDsl.VerificationStage.Then; -import testasyouthink.function.CheckedConsumer; import testasyouthink.function.CheckedFunction; +import testasyouthink.function.Functions; import java.util.function.Supplier; public class TestAsYouThink { + private static Functions functions = Functions.INSTANCE; private static ThenStepFactory thenStepFactory = ThenStepFactory.INSTANCE; public static <$SystemUnderTest> Given<$SystemUnderTest> givenSut($SystemUnderTest systemUnderTest) { @@ -46,13 +47,11 @@ public class TestAsYouThink { } public static ThenWithoutResultStep when(Runnable whenStep) { - CheckedConsumer whenStepAsVoidConsumer = Void -> whenStep.run(); - return thenStepFactory.createThenStep(whenStepAsVoidConsumer); + return thenStepFactory.createThenStep(functions.toCheckedConsumer(whenStep)); } public static <$Result> Then when(Supplier<$Result> whenStep) { - CheckedFunction whenStepAsFunction = Void -> whenStep.get(); - return thenStepFactory.createThenStep(whenStepAsFunction); + return thenStepFactory.createThenStep(functions.toCheckedFunction(whenStep)); } public static <$SystemUnderTest, $Result> CheckedFunction<$SystemUnderTest, $Result> withReturn( diff --git a/test-as-you-think-core/src/main/java/testasyouthink/function/Functions.java b/test-as-you-think-core/src/main/java/testasyouthink/function/Functions.java index d87441b..d0ad87a 100644 --- a/test-as-you-think-core/src/main/java/testasyouthink/function/Functions.java +++ b/test-as-you-think-core/src/main/java/testasyouthink/function/Functions.java @@ -39,6 +39,10 @@ public Consumer toConsumer(Runnable runnable) { return toBeConsumed -> runnable.run(); } + public CheckedConsumer toCheckedConsumer(Runnable runnable) { + return toBeConsumed -> runnable.run(); + } + public CheckedFunction toFunction(CheckedConsumer checkedConsumer) { return toBeConsumed -> { checkedConsumer.accept(toBeConsumed); @@ -46,6 +50,10 @@ public CheckedFunction toFunction(CheckedConsumer checkedConsume }; } + public CheckedFunction toCheckedFunction(Supplier supplier) { + return Void -> supplier.get(); + } + public CheckedFunction toFunctionWithThrowableAsResult(CheckedConsumer checkedConsumer) { return toBeConsumed -> { Throwable result = null; From 6417a13d97f60d82fd58f153e155c9b40c299e28 Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Sat, 17 Jun 2017 21:42:50 +0200 Subject: [PATCH 23/31] Update the documentation. --- README.md | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2272f70..9f5777c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -What you think is what you test... Not yet another testing api! +What you think is what you test... Not yet another testing API or framework! # Fluent testing and added value @@ -17,7 +17,7 @@ Moreover *TestAsYouThink* uses the Given-When-Then canvas as a formal guide to c ## Installation -Add it to your project with Maven, or download it from Maven Central. +Add *TestAsYouThink* as a dependency to your project with Maven, or download it from [Maven Central](https://search.maven.org/#search%7Cga%7C1%7Ctest%20as%20you%20think). ```xml com.github.xapn @@ -28,7 +28,14 @@ Add it to your project with Maven, or download it from Maven Central. ## Basics -Here is a very simple example of what you can do. +Here is the minimal syntax to implement your test methods for a `SystemUnderTest` class. +```java +givenSutClass(SystemUnderTest.class) +.when(sut -> {}) +.then(() -> {}); +``` + +Let us complete the previous scenario with a very simple example of what you can do, while testing a non-void method of your system under test. ```java import static testasyouthink.TestAsYouThink.givenSutClass; ... @@ -50,7 +57,7 @@ givenSutClass(SystemUnderTest.class) Notice that: - any Given-When-Then step can be implemented by a lambda expression or a method reference; - you manipule the same SUT type from the beginning to the end, because the `sut` type is determined during the *Given* step, until the end; -- there is no need to instantiate the `sut` object, even if it is allowed by the `givenSut(sutInstance)` alternate end point; +- there is no need to instantiate the `sut` object, even if it is allowed by the `givenSut(sutInstance)` alternate end point, as below; - the call to any `given()` method is optional; - you manipule the same `result` type until the end, because the `result` type is determined during the *When* step; - you cannot inadvertently make a fake test that would verify nothing, because any `then()` method is always a sequence termination. @@ -70,6 +77,7 @@ givenSut(systemUnderTest) // Verification of expectations }); ``` + ## Test Fixtures ### Separation of concerns with multiple Given steps @@ -178,13 +186,13 @@ givenSutClass(SystemUnderTest.class) ### Failures -If a method signature contains a `throws` clause with a checked, compile-time expectation, it is not necessary to modify the testing method signature anymore by adding the same clause to it. This clause and its spreading are considered as a technical constaint without value in a executable specification approach. As a consequence, it becomes imperceptible for the test code, and above all for the software developer who can stay focused on his tests. Tests will continue to fail if any unexpected exception is raised. +If a method signature contains a `throws` clause with a checked, compile-time exception, it is not necessary to modify the testing method signature anymore by adding the same clause to it. This clause and its spreading are considered as a technical constaint without value in a executable specification approach. As a consequence, it becomes imperceptible for the test code, and above all for the software developer who can stay focused on his tests. Tests will continue to fail if any unexpected exception is raised. #### Expected failures Because the failure testing is an important part of your use cases, you can verify the behavior of the system under test when it is used ouside operating conditions. ```java -givenSut(SystemUnderTest.class) +givenSutClass(SystemUnderTest.class) .given(() -> { ... }) .whenSutRunsOutsideOperatingConditions(sut -> { // where an event causes a failure @@ -229,4 +237,4 @@ When an unexpected failure occurs - because of a regression for example -, the t # License -*Test As You Think* is distributed under the GNU LGPLv3 license. See the LICENSE.txt file for details. +*Test As You Think* is distributed under the GNU LGPLv3 license. The LGPLv3 license is included in the LICENSE.txt file. More information about this license is available at http://www.gnu.org. From 6dafef3181fd4360753db11f340151086a49869b Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Tue, 20 Jun 2017 12:43:43 +0200 Subject: [PATCH 24/31] Update the documentation for the time limit expectation. --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index 9f5777c..d15b015 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,26 @@ givenSutClass(SystemUnderTest.class) When an unexpected failure occurs - because of a regression for example -, the test fails by raising an `AssertionError`, because the defaut behavior consists of asserting no failure should happen, unless the software developer wants. +### Time limit + +Sometimes you need to limit the allowed execution time of the tested event. +```java +givenSutClass(SystemUnderTest.class) +.when(SystemUnderTest::spendSomeTime) +.thenSutRepliesWithin(100); +``` +By default, the time limit is given in milliseconds. If you want to use another time unit: +```java +import java.time.Duration; +... + +givenSutClass(SystemUnderTest.class) +.when(SystemUnderTest::spendSomeTime) +.thenSutRepliesWithin(Duration.ofMinutes(3); +``` + +The advantage of TestAsYouThink is that the time limit is only applied to the tested event, while [JUnit](https://github.com/junit-team/junit4/wiki/timeout-for-tests) applies its `timeout` to the whole test method with its `@Test` annotation. [JUnit 5](http://junit.org/junit5/docs/snapshot/user-guide/) will propose an `assertTimeout(duration, lambda)` method that returns the lamba expression result, but such a syntax amalgamates irremediably the expectations and the event. + # Release Notes ## Version 0.3 From c6f82332eede0ec23e51542470ab6fea4632671f Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Tue, 20 Jun 2017 19:41:43 +0200 Subject: [PATCH 25/31] Add badges. - Add a bagde for Javadoc.io for an online Javadoc. - Add a badge for Maven Central. - Add a badge for the license. https://gist.github.com/lukas-h/2a5d00690736b4c3a7ba - Add a badge for counters. https://github.com/Aaronepower/tokei --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index d15b015..435938d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,17 @@ What you think is what you test... Not yet another testing API or framework! +Software: +[![Maven Central](https://img.shields.io/maven-central/v/org.apache.maven/apache-maven.svg)](https://search.maven.org/#artifactdetails%7Ccom.github.xapn%7Ctest-as-you-think-core%7C0.3%7C) +[![Javadocs](http://javadoc.io/badge/com.github.xapn/test-as-you-think-core.svg?color=orange)](http://javadoc.io/doc/com.github.xapn/test-as-you-think-core) +[![License: GNU LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](http://www.gnu.org/licenses/lgpl-3.0) + +Counters: +[![LoC](https://tokei.rs/b1/github/xapn/test-as-you-think?category=code)](https://github.com/xapn/test-as-you-think) +[![Files](https://tokei.rs/b1/github/xapn/test-as-you-think?category=files)](https://github.com/xapn/test-as-you-think) +[![Total lines](https://tokei.rs/b1/github/xapn/test-as-you-think?category=lines)](https://github.com/xapn/test-as-you-think) +[![Comments](https://tokei.rs/b1/github/xapn/test-as-you-think?category=comments)](https://github.com/xapn/test-as-you-think) +[![Blank lines](https://tokei.rs/b1/github/xapn/test-as-you-think?category=blanks)](https://github.com/xapn/test-as-you-think) + # Fluent testing and added value *TestAsYouThink* is an open source software library in Java for testing purposes. It is designed as a **fluent API** that will change the way development teams write their unit and integration tests. It aims to take control over the coding practices as **executable guidelines**, from beginners to experts, to get **high-quality tests**. Why should you adopt *TestAsYouThink*? From 4f640c960c5054b3bf028e2cbfb6787f7e4f0ae4 Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Thu, 22 Jun 2017 12:58:10 +0200 Subject: [PATCH 26/31] Update the documentation for the alternate when methods. --- README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/README.md b/README.md index 435938d..cd8cfa9 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,40 @@ givenSutClass(SystemUnderTest.class) .then(result -> { ... }); ``` +## Event + +You can use different syntaxes to pass the event to the `when()` method: +- a method reference (`SystemUnderTest::targetMethod`), +- a statement lambda (`sut -> { return sut.targetMethod(); }`), +- an expression lambda (`sut -> sut.targetMethod()`). + +All of them are useful: the more proper one depends on the use case. + +You can favor the simplest `when()` method, or choose a more explicit, alternate method: `whenSutReturns()` if a result is expected; otherwise `whenSutRuns()`. + +### Avoid ambiguous method calls + +To define the event, you may want to pass an expression lambda to the `when()` method like this. +```java +givenSutClass(SystemUnderTest.class) +.when(sut -> sut.testedMethod()) // compilation error +.then(...); +``` +In such a case, the compiler meets an error because of an ambiguous method call: it does not know which `when()` method must be called. One receives a lambda that returns a value, while another one receives a lambda that returns nothing. Instead of casting the expression lambda to a function or a consumer, you can avoid this compilation problem by using the following alternate methods. + +Without return: +```java +givenSutClass(SystemUnderTest.class) +.whenSutRuns(sut -> sut.voidMethod(...)) +.then(...); +``` +With a return: +```java +givenSutClass(SystemUnderTest.class) +.whenSutReturns(sut -> sut.nonVoidMethod(...)) +.then(...); +``` + ## Expectations ### Separation of concerns with multiple Then steps From 669b8a79fdafd035f86fdbae1852effe225ff418 Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Thu, 22 Jun 2017 18:07:10 +0200 Subject: [PATCH 27/31] Add social badges. --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index cd8cfa9..b2310ab 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,13 @@ Counters: [![Comments](https://tokei.rs/b1/github/xapn/test-as-you-think?category=comments)](https://github.com/xapn/test-as-you-think) [![Blank lines](https://tokei.rs/b1/github/xapn/test-as-you-think?category=blanks)](https://github.com/xapn/test-as-you-think) +Social: +[![Twitter URL](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/search?q=%23TestAsYouThink) +[![Twitter Follow](https://img.shields.io/twitter/follow/espadrine.svg?style=social&label=Follow)](https://twitter.com/XEngineer) +[![GitHub stars](https://img.shields.io/github/stars/badges/shields.svg?style=social&label=Star)](https://github.com/xapn/test-as-you-think/stargazers) +[![GitHub watchers](https://img.shields.io/github/watchers/badges/shields.svg?style=social&label=Watch)](https://github.com/xapn/test-as-you-think/watchers) +[![GitHub forks](https://img.shields.io/github/forks/badges/shields.svg?style=social&label=Fork)](https://github.com/xapn/test-as-you-think) + # Fluent testing and added value *TestAsYouThink* is an open source software library in Java for testing purposes. It is designed as a **fluent API** that will change the way development teams write their unit and integration tests. It aims to take control over the coding practices as **executable guidelines**, from beginners to experts, to get **high-quality tests**. Why should you adopt *TestAsYouThink*? From aa4c6cfa7a64d53af9f74a8a496769b2d4aa3f84 Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Thu, 22 Jun 2017 19:40:32 +0200 Subject: [PATCH 28/31] Update the documentation for starting with when. --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index b2310ab..e779fb3 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,17 @@ All of them are useful: the more proper one depends on the use case. You can favor the simplest `when()` method, or choose a more explicit, alternate method: `whenSutReturns()` if a result is expected; otherwise `whenSutRuns()`. +### Starting with the event + +To write very simple tests, you might want to directly attack the system under test. In such a use case, the API syntax becomes very minimalist. +```java +import static testasyouthink.TestAsYouThink.when; +... + +when(() -> sut.targetMethod(oneOrMoreArguments)).then(...); // or... +when(SystemUnderTest::targetMethod).then(...); // without arguments to be passed to the target method +``` + ### Avoid ambiguous method calls To define the event, you may want to pass an expression lambda to the `when()` method like this. From 996a9bfb2aa3591bec0e7dde881d028b97c9a0e6 Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Thu, 22 Jun 2017 19:53:52 +0200 Subject: [PATCH 29/31] Write the release note. --- README.md | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e779fb3..351ad24 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ What you think is what you test... Not yet another testing API or framework! Software: -[![Maven Central](https://img.shields.io/maven-central/v/org.apache.maven/apache-maven.svg)](https://search.maven.org/#artifactdetails%7Ccom.github.xapn%7Ctest-as-you-think-core%7C0.3%7C) +[![Maven Central](https://img.shields.io/maven-central/v/org.apache.maven/apache-maven.svg)](https://search.maven.org/#artifactdetails%7Ccom.github.xapn%7Ctest-as-you-think-core%7C0.4%7C) [![Javadocs](http://javadoc.io/badge/com.github.xapn/test-as-you-think-core.svg?color=orange)](http://javadoc.io/doc/com.github.xapn/test-as-you-think-core) [![License: GNU LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](http://www.gnu.org/licenses/lgpl-3.0) @@ -41,7 +41,7 @@ Add *TestAsYouThink* as a dependency to your project with Maven, or download it com.github.xapn test-as-you-think-core - 0.3 + 0.4 ``` @@ -289,14 +289,20 @@ The advantage of TestAsYouThink is that the time limit is only applied to the te # Release Notes -## Version 0.3 +## Version 0.4: Time limit as an expectation + +- Expect that the system under test replies within a time limit. +- Resolve ambiguous method calls in relation to using expression lambdas. +- Start to write a test with the when step. + +## Version 0.3: TestAsYouThink as a Maven distributed OSS library - Rename the API to **TestAsYouThink**. - Choose an open source license. - Publish artifacts to Maven Central. - Check version updates. -## Version 0.2 +## Version 0.2: Test fixtures as method arguments - Include a data as a method argument during the preparation phase. - Include two data as method arguments during the preparation phase. @@ -306,7 +312,7 @@ The advantage of TestAsYouThink is that the time limit is only applied to the te - Verify failures while invoking methods with arguments. - Verify the expected exception and the expected message separately. -## Version 0.1 +## Version 0.1: Given-When-Then as a canvas - Write an unit or integration test by using the Given-When-Then canvas and full sequence. - Delegate the system under test instantiation to the API. From aabecd095c71fc7bcca4be6a3e8e4ba33bc02bd5 Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Thu, 22 Jun 2017 19:59:50 +0200 Subject: [PATCH 30/31] Release the Maven artifacts. --- pom.xml | 2 +- test-as-you-think-core/pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 36451c1..09e642c 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.github.xapn test-as-you-think-project - 0.4-SNAPSHOT + 0.4 pom ${project.groupId}:${project.artifactId}:${project.version}:${project.packaging} The TestAsYouThink project aims to provide tooling to improve test code quality and to make testing diff --git a/test-as-you-think-core/pom.xml b/test-as-you-think-core/pom.xml index 3e55ca5..e605ed0 100644 --- a/test-as-you-think-core/pom.xml +++ b/test-as-you-think-core/pom.xml @@ -5,7 +5,7 @@ com.github.xapn test-as-you-think-project - 0.4-SNAPSHOT + 0.4 test-as-you-think-core From 1a2f507d9f5568d2afe0e48d4917ccdfcfcce017 Mon Sep 17 00:00:00 2001 From: Xavier Pigeon Date: Thu, 22 Jun 2017 20:08:21 +0200 Subject: [PATCH 31/31] Generate the table of content of the documentation. - How-to: with markdown-toc https://github.com/jonschlinkert/markdown-toc > sudo -E npm -g install --save markdown-toc # Add markdown to the PATH environment variable. # Add '' to point out the TOC place in the Markdown file. > markdown-toc -i README.md # for example --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index 351ad24..8559575 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,35 @@ Social: [![GitHub watchers](https://img.shields.io/github/watchers/badges/shields.svg?style=social&label=Watch)](https://github.com/xapn/test-as-you-think/watchers) [![GitHub forks](https://img.shields.io/github/forks/badges/shields.svg?style=social&label=Fork)](https://github.com/xapn/test-as-you-think) + + +- [Fluent testing and added value](#fluent-testing-and-added-value) +- [Getting Started](#getting-started) + * [Installation](#installation) + * [Basics](#basics) + * [Test Fixtures](#test-fixtures) + + [Separation of concerns with multiple Given steps](#separation-of-concerns-with-multiple-given-steps) + + [Specifying fixtures](#specifying-fixtures) + * [Event](#event) + + [Starting with the event](#starting-with-the-event) + + [Avoid ambiguous method calls](#avoid-ambiguous-method-calls) + * [Expectations](#expectations) + + [Separation of concerns with multiple Then steps](#separation-of-concerns-with-multiple-then-steps) + + [Expectations as predicates](#expectations-as-predicates) + + [Specifying expectations](#specifying-expectations) + + [Failures](#failures) + - [Expected failures](#expected-failures) + - [Unexpected failures](#unexpected-failures) + + [Time limit](#time-limit) +- [Release Notes](#release-notes) + * [Version 0.4: Time limit as an expectation](#version-04-time-limit-as-an-expectation) + * [Version 0.3: TestAsYouThink as a Maven distributed OSS library](#version-03-testasyouthink-as-a-maven-distributed-oss-library) + * [Version 0.2: Test fixtures as method arguments](#version-02-test-fixtures-as-method-arguments) + * [Version 0.1: Given-When-Then as a canvas](#version-01-given-when-then-as-a-canvas) +- [License](#license) + + + # Fluent testing and added value *TestAsYouThink* is an open source software library in Java for testing purposes. It is designed as a **fluent API** that will change the way development teams write their unit and integration tests. It aims to take control over the coding practices as **executable guidelines**, from beginners to experts, to get **high-quality tests**. Why should you adopt *TestAsYouThink*?