From 9a14ea648633aa223c51be4192e959f1991a5a87 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sun, 24 Sep 2023 21:48:49 +0300 Subject: [PATCH] Add SimpleTestRunner as a test runner. --- .../tests/runners/AbstractTestRunner.java | 6 ++ .../tests/runners/SimpleTestRunner.java | 29 +++++++ .../tests/runners/SimpleTestRunnerSpec.groovy | 87 +++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 src/main/java/space/kklochko/simple_jbdd/tests/runners/AbstractTestRunner.java create mode 100644 src/main/java/space/kklochko/simple_jbdd/tests/runners/SimpleTestRunner.java create mode 100644 src/test/groovy/space/kklochko/simple_jbdd/tests/runners/SimpleTestRunnerSpec.groovy diff --git a/src/main/java/space/kklochko/simple_jbdd/tests/runners/AbstractTestRunner.java b/src/main/java/space/kklochko/simple_jbdd/tests/runners/AbstractTestRunner.java new file mode 100644 index 0000000..cfbc38b --- /dev/null +++ b/src/main/java/space/kklochko/simple_jbdd/tests/runners/AbstractTestRunner.java @@ -0,0 +1,6 @@ +package space.kklochko.simple_jbdd.tests.runners; + +public abstract class AbstractTestRunner { + public abstract boolean run(); +} + diff --git a/src/main/java/space/kklochko/simple_jbdd/tests/runners/SimpleTestRunner.java b/src/main/java/space/kklochko/simple_jbdd/tests/runners/SimpleTestRunner.java new file mode 100644 index 0000000..69d62b4 --- /dev/null +++ b/src/main/java/space/kklochko/simple_jbdd/tests/runners/SimpleTestRunner.java @@ -0,0 +1,29 @@ +package space.kklochko.simple_jbdd.tests.runners; + +import space.kklochko.simple_jbdd.tests.Test; +import space.kklochko.simple_jbdd.tests.commands.AbstractTestCommand; + +public class SimpleTestRunner extends AbstractTestRunner { + AbstractTestCommand command; + + public SimpleTestRunner(AbstractTestCommand command) { + this.command = command; + } + + public boolean run() { + try { + return getCommand().test(); + }catch (AssertionError e) { + return false; + } + } + + public AbstractTestCommand getCommand() { + return command; + } + + public void setCommand(AbstractTestCommand command) { + this.command = command; + } +} + diff --git a/src/test/groovy/space/kklochko/simple_jbdd/tests/runners/SimpleTestRunnerSpec.groovy b/src/test/groovy/space/kklochko/simple_jbdd/tests/runners/SimpleTestRunnerSpec.groovy new file mode 100644 index 0000000..3edcee2 --- /dev/null +++ b/src/test/groovy/space/kklochko/simple_jbdd/tests/runners/SimpleTestRunnerSpec.groovy @@ -0,0 +1,87 @@ +package space.kklochko.simple_jbdd.tests.runners + +import space.kklochko.simple_jbdd.test_examples.tests.SimpleFailedThenTest +import space.kklochko.simple_jbdd.test_examples.tests.SimpleThenTest +import space.kklochko.simple_jbdd.tests.commands.SimpleTestCommand +import space.kklochko.simple_jbdd.tests.factories.TestCommandFactory +import spock.lang.Narrative +import spock.lang.Specification +import spock.lang.Subject +import spock.lang.Title + +@Narrative("""The runner must return a result of test, so +those tests check if passed and failed test generate the right result. +""") +@Title("Unit tests for SimpleTestRunner") +class SimpleTestRunnerSpec extends Specification { + def "The test with then has been passed."() { + given: "I have a testCommand" + def test = Stub(SimpleTestCommand) + test.test() >>> [true] + + and: "I have a test runner" + @Subject + def runner = new SimpleTestRunner(test) + + when: "The testCommand has been run" + def result = runner.run() + + then: "The result is true" + result + } + + def "The real test with then has been passed."() { + given: "I have a test and a factory" + def aTest = new SimpleThenTest() + def factory = new TestCommandFactory(); + + and: "I have a testCommand" + def test = factory.create(aTest) + + and: "I have a test runner" + @Subject + def runner = new SimpleTestRunner(test) + + when: "The testCommand has been run" + def result = runner.run() + + then: "The result is true" + result + } + + def "The test with then that has not been passed."() { + given: "I have a failed testCommand" + def test = Stub(SimpleTestCommand) + test.test() >> { assert 1 == 0 } + + and: "I have a test runner" + @Subject + def runner = new SimpleTestRunner(test) + + when: "The testCommand has been run" + def result = runner.run() + + then: "The result is true" + !result + } + + def "The real test with then has not been passed."() { + given: "I have a test and a factory" + def aTest = new SimpleFailedThenTest() + def factory = new TestCommandFactory(); + + and: "I have a testCommand" + def test = factory.create(aTest) + + and: "I have a test runner" + @Subject + def runner = new SimpleTestRunner(test) + + when: "The testCommand has been run" + def result = runner.run() + + then: "The result is true" + !result + } +} +