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..9bd6984 --- /dev/null +++ b/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertion.java @@ -0,0 +1,44 @@ +package com.pragmaticobjects.oo.tests.bdd; + +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, + final Step.Then then + ) { + this.given = given; + this.when = when; + this.then = then; + } + + @Override + public final 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..d5862af --- /dev/null +++ b/tests-core/src/main/java/com/pragmaticobjects/oo/tests/bdd/Step.java @@ -0,0 +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 new file mode 100644 index 0000000..f63dd33 --- /dev/null +++ b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/BehaviorAssertionTest.java @@ -0,0 +1,37 @@ +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; + +/** + * Test case to validate the {@link BehaviorAssertion} + * + * @author Romain Rochegude + */ +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..3f8f30f --- /dev/null +++ b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/GivenNop.java @@ -0,0 +1,13 @@ +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() { + 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..4a4b419 --- /dev/null +++ b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenFails.java @@ -0,0 +1,15 @@ +package com.pragmaticobjects.oo.tests.bdd; + +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) { + 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..38ea1a2 --- /dev/null +++ b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/ThenSucceeds.java @@ -0,0 +1,12 @@ +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 new file mode 100644 index 0000000..fe4fc71 --- /dev/null +++ b/tests-core/src/test/java/com/pragmaticobjects/oo/tests/bdd/WhenNop.java @@ -0,0 +1,13 @@ +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) { + return null; + } +} 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;