From 26fbecba670afbd54aac8a36755ddb3f72e3c6f2 Mon Sep 17 00:00:00 2001 From: Romain Rochegude Date: Thu, 7 Feb 2019 18:26:10 +0100 Subject: [PATCH 1/3] introduce BDD steps using an interface, define Assertion that aggregates Steps, cover by unit tests --- .../oo/tests/bdd/BehaviorAssertion.java | 29 +++++++++++++++++ .../pragmaticobjects/oo/tests/bdd/Step.java | 15 +++++++++ .../oo/tests/bdd/BehaviorAssertionTest.java | 32 +++++++++++++++++++ .../oo/tests/bdd/GivenNop.java | 8 +++++ .../oo/tests/bdd/ThenFails.java | 10 ++++++ .../oo/tests/bdd/ThenSucceeds.java | 7 ++++ .../oo/tests/bdd/WhenNop.java | 8 +++++ 7 files changed, 109 insertions(+) create mode 100644 tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertion.java create mode 100644 tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/Step.java create mode 100644 tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertionTest.java create mode 100644 tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/GivenNop.java create mode 100644 tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenFails.java create mode 100644 tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenSucceeds.java create mode 100644 tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/WhenNop.java diff --git a/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertion.java b/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertion.java new file mode 100644 index 0000000..e0bc107 --- /dev/null +++ b/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertion.java @@ -0,0 +1,29 @@ +package com.pragmaticobjects.oo.tests.bdd; + +import com.pragmaticobjects.oo.tests.Assertion; + +public final class BehaviorAssertion implements Assertion { + + private final Step.Given given; + private final Step.When when; + private final Step.Then then; + + public BehaviorAssertion( + final Step.Given given, + final Step.When when, + final Step.Then then + ) { + this.given = given; + this.when = when; + this.then = then; + } + + @Override + public void check() throws Exception { + then.check( + when.when( + given.given() + ) + ); + } +} diff --git a/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/Step.java b/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/Step.java new file mode 100644 index 0000000..eb1602d --- /dev/null +++ b/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/Step.java @@ -0,0 +1,15 @@ +package com.pragmaticobjects.oo.tests.bdd; + +public interface Step { + interface Given { + S given(); + } + + interface When { + R when(final S sut); + } + + interface Then { + void check(final R result); + } +} diff --git a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertionTest.java b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertionTest.java new file mode 100644 index 0000000..d0e5fd9 --- /dev/null +++ b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertionTest.java @@ -0,0 +1,32 @@ +package com.pragmaticobjects.oo.tests.bdd; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class BehaviorAssertionTest { + @Test + public void assertAssertionFailsTest() { + assertThatThrownBy( + () -> + new BehaviorAssertion<>( + new GivenNop(), + new WhenNop(), + new ThenFails() + ).check() + ).isInstanceOf(AssertionError.class); + } + + @Test + public void assertAssertionPassesTest() { + assertThatCode( + () -> + new BehaviorAssertion<>( + new GivenNop(), + new WhenNop(), + new ThenSucceeds() + ).check() + ).doesNotThrowAnyException(); + } +} \ No newline at end of file diff --git a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/GivenNop.java b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/GivenNop.java new file mode 100644 index 0000000..e64fb46 --- /dev/null +++ b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/GivenNop.java @@ -0,0 +1,8 @@ +package com.pragmaticobjects.oo.tests.bdd; + +public final class GivenNop implements Step.Given { + @Override + public Void given() { + return null; + } +} diff --git a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenFails.java b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenFails.java new file mode 100644 index 0000000..39bf8b5 --- /dev/null +++ b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenFails.java @@ -0,0 +1,10 @@ +package com.pragmaticobjects.oo.tests.bdd; + +import static org.assertj.core.api.Assertions.fail; + +public final class ThenFails implements Step.Then { + @Override + public void check(final Void result) { + fail("FAIL, just as planned"); + } +} diff --git a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenSucceeds.java b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenSucceeds.java new file mode 100644 index 0000000..d0544b8 --- /dev/null +++ b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenSucceeds.java @@ -0,0 +1,7 @@ +package com.pragmaticobjects.oo.tests.bdd; + +public final class ThenSucceeds implements Step.Then { + @Override + public void check(final Void result) { + } +} diff --git a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/WhenNop.java b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/WhenNop.java new file mode 100644 index 0000000..8863ffd --- /dev/null +++ b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/WhenNop.java @@ -0,0 +1,8 @@ +package com.pragmaticobjects.oo.tests.bdd; + +public final class WhenNop implements Step.When { + @Override + public Void when(final Void sut) { + return null; + } +} From 671d751c66a8777988c2f5dd59a6a61d5e4233b3 Mon Sep 17 00:00:00 2001 From: Romain Rochegude Date: Fri, 8 Feb 2019 17:41:52 +0100 Subject: [PATCH 2/3] remove final keyword for classes and set it to methods --- .../com/pragmaticobjects/oo/tests/bdd/BehaviorAssertion.java | 4 ++-- .../test/java/com/pragmaticobjects/oo/tests/bdd/GivenNop.java | 4 ++-- .../java/com/pragmaticobjects/oo/tests/bdd/ThenFails.java | 4 ++-- .../java/com/pragmaticobjects/oo/tests/bdd/ThenSucceeds.java | 4 ++-- .../test/java/com/pragmaticobjects/oo/tests/bdd/WhenNop.java | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertion.java b/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertion.java index e0bc107..5d6c068 100644 --- a/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertion.java +++ b/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertion.java @@ -2,7 +2,7 @@ import com.pragmaticobjects.oo.tests.Assertion; -public final class BehaviorAssertion implements Assertion { +public class BehaviorAssertion implements Assertion { private final Step.Given given; private final Step.When when; @@ -19,7 +19,7 @@ public BehaviorAssertion( } @Override - public void check() throws Exception { + public final void check() throws Exception { then.check( when.when( given.given() diff --git a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/GivenNop.java b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/GivenNop.java index e64fb46..5a865c4 100644 --- a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/GivenNop.java +++ b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/GivenNop.java @@ -1,8 +1,8 @@ package com.pragmaticobjects.oo.tests.bdd; -public final class GivenNop implements Step.Given { +public class GivenNop implements Step.Given { @Override - public Void given() { + public final Void given() { return null; } } diff --git a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenFails.java b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenFails.java index 39bf8b5..cc91a37 100644 --- a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenFails.java +++ b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenFails.java @@ -2,9 +2,9 @@ import static org.assertj.core.api.Assertions.fail; -public final class ThenFails implements Step.Then { +public class ThenFails implements Step.Then { @Override - public void check(final Void result) { + public final void check(final Void result) { fail("FAIL, just as planned"); } } diff --git a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenSucceeds.java b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenSucceeds.java index d0544b8..1fbce9d 100644 --- a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenSucceeds.java +++ b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenSucceeds.java @@ -1,7 +1,7 @@ package com.pragmaticobjects.oo.tests.bdd; -public final class ThenSucceeds implements Step.Then { +public class ThenSucceeds implements Step.Then { @Override - public void check(final Void result) { + public final void check(final Void result) { } } diff --git a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/WhenNop.java b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/WhenNop.java index 8863ffd..4120240 100644 --- a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/WhenNop.java +++ b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/WhenNop.java @@ -1,8 +1,8 @@ package com.pragmaticobjects.oo.tests.bdd; -public final class WhenNop implements Step.When { +public class WhenNop implements Step.When { @Override - public Void when(final Void sut) { + public final Void when(final Void sut) { return null; } } From 4c77801b8741899574bc89e17a1cfc3da1eccdb1 Mon Sep 17 00:00:00 2001 From: Romain Rochegude Date: Fri, 8 Feb 2019 17:59:42 +0100 Subject: [PATCH 3/3] add Javadoc --- .../oo/tests/bdd/BehaviorAssertion.java | 15 ++++++++ .../pragmaticobjects/oo/tests/bdd/Step.java | 37 +++++++++++++++++++ .../oo/tests/bdd/package-info.java | 24 ++++++++++++ .../oo/tests/bdd/BehaviorAssertionTest.java | 5 +++ .../oo/tests/bdd/GivenNop.java | 5 +++ .../oo/tests/bdd/ThenFails.java | 5 +++ .../oo/tests/bdd/ThenSucceeds.java | 5 +++ .../oo/tests/bdd/WhenNop.java | 5 +++ .../oo/tests/bdd/package-info.java | 24 ++++++++++++ 9 files changed, 125 insertions(+) create mode 100644 tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/package-info.java create mode 100644 tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/package-info.java diff --git a/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertion.java b/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertion.java index 5d6c068..9bd6984 100644 --- a/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertion.java +++ b/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertion.java @@ -2,12 +2,27 @@ import com.pragmaticobjects.oo.tests.Assertion; +/** + * Assertion that covers the 3 steps of BDD approach. + * + * @param The system under test + * @param The result of the performed operation + * + * @author Romain Rochegude + */ public class BehaviorAssertion implements Assertion { private final Step.Given given; private final Step.When when; private final Step.Then then; + /** + * Ctor. + * + * @param given The Given step + * @param when The When step + * @param then The Then step + */ public BehaviorAssertion( final Step.Given given, final Step.When when, diff --git a/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/Step.java b/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/Step.java index eb1602d..d5862af 100644 --- a/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/Step.java +++ b/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/Step.java @@ -1,15 +1,52 @@ package com.pragmaticobjects.oo.tests.bdd; +/** + * Interface to describe behavior-driven-development steps + * + * @author Romain Rochegude + */ public interface Step { + /** + * Interface to describe the Given step. + * + * @param The system under test + */ interface Given { + /** + * Build the system under test instance. + * + * @return An instance of the system under test + */ S given(); } + /** + * Interface to describe the When step. + * + * @param The system under test + * @param The result of the performed operation + */ interface When { + /** + * Perform the operation to test. + * + * @param sut The system under test + * @return The result of the performed operation + */ R when(final S sut); } + /** + * Interface to describe the Then step. + * + * @param The result of the performed operation + */ interface Then { + /** + * Check the result of the performed operation. + * + * @param result The result of the performed operation + */ void check(final R result); } } diff --git a/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/package-info.java b/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/package-info.java new file mode 100644 index 0000000..6023bf1 --- /dev/null +++ b/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/package-info.java @@ -0,0 +1,24 @@ +/* + * The MIT License + * + * Copyright 2017 skapral. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.pragmaticobjects.oo.tests.bdd; diff --git a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertionTest.java b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertionTest.java index d0e5fd9..f63dd33 100644 --- a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertionTest.java +++ b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertionTest.java @@ -5,6 +5,11 @@ import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; +/** + * Test case to validate the {@link BehaviorAssertion} + * + * @author Romain Rochegude + */ public class BehaviorAssertionTest { @Test public void assertAssertionFailsTest() { diff --git a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/GivenNop.java b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/GivenNop.java index 5a865c4..3f8f30f 100644 --- a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/GivenNop.java +++ b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/GivenNop.java @@ -1,5 +1,10 @@ package com.pragmaticobjects.oo.tests.bdd; +/** + * A Given step that does nothing + * + * @author Romain Rochegude + */ public class GivenNop implements Step.Given { @Override public final Void given() { diff --git a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenFails.java b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenFails.java index cc91a37..4a4b419 100644 --- a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenFails.java +++ b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenFails.java @@ -2,6 +2,11 @@ import static org.assertj.core.api.Assertions.fail; +/** + * A Then step that fails validation + * + * @author Romain Rochegude + */ public class ThenFails implements Step.Then { @Override public final void check(final Void result) { diff --git a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenSucceeds.java b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenSucceeds.java index 1fbce9d..38ea1a2 100644 --- a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenSucceeds.java +++ b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenSucceeds.java @@ -1,5 +1,10 @@ package com.pragmaticobjects.oo.tests.bdd; +/** + * A Then step that succeeds validation. + * + * @author Romain Rochegude + */ public class ThenSucceeds implements Step.Then { @Override public final void check(final Void result) { diff --git a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/WhenNop.java b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/WhenNop.java index 4120240..fe4fc71 100644 --- a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/WhenNop.java +++ b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/WhenNop.java @@ -1,5 +1,10 @@ package com.pragmaticobjects.oo.tests.bdd; +/** + * A When step that does nothing + * + * @author Romain Rochegude + */ public class WhenNop implements Step.When { @Override public final Void when(final Void sut) { diff --git a/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/package-info.java b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/package-info.java new file mode 100644 index 0000000..dd29c97 --- /dev/null +++ b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/package-info.java @@ -0,0 +1,24 @@ +/* + * The MIT License + * + * Copyright 2017 Kapralov Sergey. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.pragmaticobjects.oo.tests.bdd;