Add the create method for the TestCommandFactory.
This commit is contained in:
@@ -2,8 +2,12 @@ package space.kklochko.simple_jbdd.tests.factories;
|
|||||||
|
|
||||||
import space.kklochko.simple_jbdd.annotations.Given;
|
import space.kklochko.simple_jbdd.annotations.Given;
|
||||||
import space.kklochko.simple_jbdd.annotations.Then;
|
import space.kklochko.simple_jbdd.annotations.Then;
|
||||||
|
import space.kklochko.simple_jbdd.annotations.Title;
|
||||||
import space.kklochko.simple_jbdd.annotations.When;
|
import space.kklochko.simple_jbdd.annotations.When;
|
||||||
import space.kklochko.simple_jbdd.tests.Test;
|
import space.kklochko.simple_jbdd.tests.Test;
|
||||||
|
import space.kklochko.simple_jbdd.tests.commands.AbstractTestCommand;
|
||||||
|
import space.kklochko.simple_jbdd.tests.commands.SimpleTestCommand;
|
||||||
|
import space.kklochko.simple_jbdd.tests.commands.decorators.BlockDecorator;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
@@ -11,9 +15,41 @@ import java.util.Map;
|
|||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
|
||||||
public class TestCommandFactory {
|
public class TestCommandFactory {
|
||||||
public <T extends Test> void create(T input) {
|
public AbstractTestCommand create(Test input) {
|
||||||
Class<?> aClass = input.getClass();
|
Class<?> aClass = input.getClass();
|
||||||
Map<String, Method> test_methods = this.getTestMethods(aClass);
|
Map<String, Method> test_methods = this.getTestMethods(aClass);
|
||||||
|
|
||||||
|
AbstractTestCommand<Test> aTestCommand = createTest(aClass, input);
|
||||||
|
|
||||||
|
if(test_methods.containsKey("Then")) {
|
||||||
|
Method then = test_methods.get("Then");
|
||||||
|
String label = then.getAnnotation(Then.class).value();
|
||||||
|
aTestCommand = new BlockDecorator<Test>(label, aTestCommand, then, "Then");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(test_methods.containsKey("When")) {
|
||||||
|
Method when = test_methods.get("When");
|
||||||
|
String label = when.getAnnotation(When.class).value();
|
||||||
|
aTestCommand = new BlockDecorator<Test>(label, aTestCommand, when, "When");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(test_methods.containsKey("Given")) {
|
||||||
|
Method when = test_methods.get("Given");
|
||||||
|
String label = when.getAnnotation(Given.class).value();
|
||||||
|
aTestCommand = new BlockDecorator<Test>(label, aTestCommand, when, "Given");
|
||||||
|
}
|
||||||
|
|
||||||
|
return aTestCommand;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected AbstractTestCommand<Test> createTest(Class<?> aClass, Test input) {
|
||||||
|
Title aTitle = aClass.getAnnotation(Title.class);
|
||||||
|
String title = aClass.getName();
|
||||||
|
|
||||||
|
if(aTitle != null)
|
||||||
|
title = aTitle.value();
|
||||||
|
|
||||||
|
return new SimpleTestCommand<Test>(title, input);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected <T extends Test> Map<String, Method> getTestMethods(Class<?> aClass) {
|
protected <T extends Test> Map<String, Method> getTestMethods(Class<?> aClass) {
|
||||||
|
|||||||
+75
-1
@@ -1,12 +1,22 @@
|
|||||||
package space.kklochko.simple_jbdd.tests.factories
|
package space.kklochko.simple_jbdd.tests.factories
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.test_examples.tests.SimpleGivenWhenThenTest
|
||||||
import space.kklochko.simple_jbdd.test_examples.tests.SimpleThenTest
|
import space.kklochko.simple_jbdd.test_examples.tests.SimpleThenTest
|
||||||
import space.kklochko.simple_jbdd.tests.factories.TestCommandFactory;
|
import space.kklochko.simple_jbdd.test_examples.tests.SimpleThenTestWithoutTitle
|
||||||
|
import space.kklochko.simple_jbdd.tests.factories.TestCommandFactory
|
||||||
|
import spock.lang.Narrative;
|
||||||
import spock.lang.Specification
|
import spock.lang.Specification
|
||||||
|
import spock.lang.Subject
|
||||||
|
import spock.lang.Title
|
||||||
|
|
||||||
|
@Narrative("""The factory must generate a test command, so
|
||||||
|
those tests check if factory generate them right.
|
||||||
|
""")
|
||||||
|
@Title("Unit test for TestCommandFactory")
|
||||||
class TestCommandFactorySpec extends Specification {
|
class TestCommandFactorySpec extends Specification {
|
||||||
def "If the map of methods have one test block."() {
|
def "If the map of methods have one test block."() {
|
||||||
given: "I have a TestCommandFactory object"
|
given: "I have a TestCommandFactory object"
|
||||||
|
@Subject
|
||||||
def factory = new TestCommandFactory();
|
def factory = new TestCommandFactory();
|
||||||
|
|
||||||
and: "I also have a SimpleThenTest object"
|
and: "I also have a SimpleThenTest object"
|
||||||
@@ -18,5 +28,69 @@ class TestCommandFactorySpec extends Specification {
|
|||||||
then: "the map size is 1 method"
|
then: "the map size is 1 method"
|
||||||
1 == map.size()
|
1 == map.size()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def "createTest() sets the title to a class name if no Title annotation."() {
|
||||||
|
given: "I have a test factory"
|
||||||
|
@Subject
|
||||||
|
def factory = new TestCommandFactory()
|
||||||
|
|
||||||
|
and: "I have a test class without Title"
|
||||||
|
def testClass = new SimpleThenTestWithoutTitle()
|
||||||
|
|
||||||
|
when: "I build a test"
|
||||||
|
def test = factory.create(testClass)
|
||||||
|
|
||||||
|
then: "The test have title as the test class"
|
||||||
|
testClass.class.getName() == test.getLabels()['Title']
|
||||||
|
}
|
||||||
|
|
||||||
|
def "createTest() sets the title to a value of the Title annotation."() {
|
||||||
|
given: "I have a test factory"
|
||||||
|
@Subject
|
||||||
|
def factory = new TestCommandFactory()
|
||||||
|
|
||||||
|
and: "I have a test class with Title"
|
||||||
|
def testClass = new SimpleThenTest()
|
||||||
|
|
||||||
|
and: "and the title of the annotation"
|
||||||
|
def titleClass = space.kklochko.simple_jbdd.annotations.Title.class
|
||||||
|
def title = testClass.getClass().getAnnotation(titleClass).value()
|
||||||
|
|
||||||
|
when: "I build a test"
|
||||||
|
def test = factory.create(testClass)
|
||||||
|
|
||||||
|
then: "The test have title as the test class"
|
||||||
|
title == test.getLabels()['Title']
|
||||||
|
}
|
||||||
|
|
||||||
|
def "createTest() build a Test with only Then."() {
|
||||||
|
given: "I have a test factory"
|
||||||
|
@Subject
|
||||||
|
def factory = new TestCommandFactory()
|
||||||
|
|
||||||
|
and: "I have a test class with Title"
|
||||||
|
def testClass = new SimpleThenTest()
|
||||||
|
|
||||||
|
when: "I build a test"
|
||||||
|
def test = factory.create(testClass)
|
||||||
|
|
||||||
|
then: "The test have title and then method"
|
||||||
|
['Title', 'Then'].toSet() == test.getLabels().keySet()
|
||||||
|
}
|
||||||
|
|
||||||
|
def "createTest() build a Test with Given-When-Then."() {
|
||||||
|
given: "I have a test factory"
|
||||||
|
@Subject
|
||||||
|
def factory = new TestCommandFactory()
|
||||||
|
|
||||||
|
and: "I have a test class with Title and Given-When-Then"
|
||||||
|
def testClass = new SimpleGivenWhenThenTest()
|
||||||
|
|
||||||
|
when: "I build a test"
|
||||||
|
def test = factory.create(testClass)
|
||||||
|
|
||||||
|
then: "The test have title and then method"
|
||||||
|
['Title', 'Given', 'When', 'Then'].toSet() == test.getLabels().keySet()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user