Skip to content
This repository has been archived by the owner on Dec 5, 2021. It is now read-only.

Introduce BehaviorAssertion to implement BDD-style assertions #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 <S> The system under test
* @param <R> The result of the performed operation
*
* @author Romain Rochegude
*/
public class BehaviorAssertion<S, R> implements Assertion {

private final Step.Given<S> given;
private final Step.When<S, R> when;
private final Step.Then<R> then;

/**
* Ctor.
*
* @param given The Given step
* @param when The When step
* @param then The Then step
*/
public BehaviorAssertion(
final Step.Given<S> given,
final Step.When<S, R> when,
final Step.Then<R> then
) {
this.given = given;
this.when = when;
this.then = then;
}

@Override
public final void check() throws Exception {
then.check(
when.when(
given.given()
)
);
}
}
Original file line number Diff line number Diff line change
@@ -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 <S> The system under test
*/
interface Given<S> {
/**
* Build the system under test instance.
*
* @return An instance of the system under test
*/
S given();
}

/**
* Interface to describe the When step.
*
* @param <S> The system under test
* @param <R> The result of the performed operation
*/
interface When<S, R> {
/**
* 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 <R> The result of the performed operation
*/
interface Then<R> {
/**
* Check the result of the performed operation.
*
* @param result The result of the performed operation
*/
void check(final R result);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<Void> {
@Override
public final Void given() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -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<Void> {
@Override
public final void check(final Void result) {
fail("FAIL, just as planned");
}
}
Original file line number Diff line number Diff line change
@@ -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<Void> {
@Override
public final void check(final Void result) {
}
}
Original file line number Diff line number Diff line change
@@ -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<Void, Void> {
@Override
public final Void when(final Void sut) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -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;