From 08c85459a72bd709b83587520c33d62bdc4fb4a8 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Wed, 20 Sep 2023 22:23:18 +0300 Subject: [PATCH] Add the TestCommandFactory with a getTestMethods. --- .../tests/factories/TestCommandFactory.java | 40 +++++++++++++++++++ .../factories/TestCommandFactorySpec.groovy | 22 ++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/main/java/space/kklochko/simple_jbdd/tests/factories/TestCommandFactory.java create mode 100644 src/test/groovy/space/kklochko/simple_jbdd/tests/factories/TestCommandFactorySpec.groovy diff --git a/src/main/java/space/kklochko/simple_jbdd/tests/factories/TestCommandFactory.java b/src/main/java/space/kklochko/simple_jbdd/tests/factories/TestCommandFactory.java new file mode 100644 index 0000000..d4d3274 --- /dev/null +++ b/src/main/java/space/kklochko/simple_jbdd/tests/factories/TestCommandFactory.java @@ -0,0 +1,40 @@ +package space.kklochko.simple_jbdd.tests.factories; + +import space.kklochko.simple_jbdd.annotations.Given; +import space.kklochko.simple_jbdd.annotations.Then; +import space.kklochko.simple_jbdd.annotations.When; +import space.kklochko.simple_jbdd.tests.Test; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; +import java.util.Map; +import java.util.TreeMap; + +public class TestCommandFactory { + public void create(T input) { + Class aClass = input.getClass(); + Map test_methods = this.getTestMethods(aClass); + } + + protected Map getTestMethods(Class aClass) { + Map test_methods = new TreeMap(); + + for(Method method : aClass.getDeclaredMethods()) { + Annotation[] annotations = method.getAnnotations(); + + if (annotations.length == 0) + continue; + + if(method.isAnnotationPresent(Given.class)) + test_methods.put("Given", method); + + if(method.isAnnotationPresent(When.class)) + test_methods.put("When", method); + + if(method.isAnnotationPresent(Then.class)) + test_methods.put("Then", method); + } + + return test_methods; + } +} diff --git a/src/test/groovy/space/kklochko/simple_jbdd/tests/factories/TestCommandFactorySpec.groovy b/src/test/groovy/space/kklochko/simple_jbdd/tests/factories/TestCommandFactorySpec.groovy new file mode 100644 index 0000000..baba5db --- /dev/null +++ b/src/test/groovy/space/kklochko/simple_jbdd/tests/factories/TestCommandFactorySpec.groovy @@ -0,0 +1,22 @@ +package space.kklochko.simple_jbdd.tests.factories + +import space.kklochko.simple_jbdd.test_examples.tests.SimpleThenTest +import space.kklochko.simple_jbdd.tests.factories.TestCommandFactory; +import spock.lang.Specification + +class TestCommandFactorySpec extends Specification { + def "If the map of methods have one test block."() { + given: "I have a TestCommandFactory object" + def factory = new TestCommandFactory(); + + and: "I also have a SimpleThenTest object" + def simpleThenTest = new SimpleThenTest() + + when: "If a test have only one block" + def map = factory.getTestMethods(simpleThenTest.getClass()) + + then: "the map size is 1 method" + 1 == map.size() + } +} +