Skip to content
This repository was 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 1 commit
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,29 @@
package com.pragmaticobjects.oo.tests.bdd;

import com.pragmaticobjects.oo.tests.Assertion;

public final class BehaviorAssertion<S, R> implements Assertion {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oo-atom prohibits final classes for certain reasons. Let the class being open and finalize its methods instead.


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

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 void check() throws Exception {
then.check(
when.when(
given.given()
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.pragmaticobjects.oo.tests.bdd;

public interface Step {
interface Given<S> {
S given();
}

interface When<S, R> {
R when(final S sut);
}

interface Then<R> {
void check(final R result);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.pragmaticobjects.oo.tests.bdd;

public final class GivenNop implements Step.Given<Void> {
@Override
public Void given() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -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<Void> {
@Override
public void check(final Void result) {
fail("FAIL, just as planned");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.pragmaticobjects.oo.tests.bdd;

public final class ThenSucceeds implements Step.Then<Void> {
@Override
public void check(final Void result) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.pragmaticobjects.oo.tests.bdd;

public final class WhenNop implements Step.When<Void, Void> {
@Override
public Void when(final Void sut) {
return null;
}
}