Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a3d7f4f42 | ||
|
|
6feb7f87dc | ||
|
|
47af8b4522 | ||
|
|
db8fe1fc47 | ||
|
|
e803452389 | ||
|
|
08c85459a7 | ||
|
|
9edbca09cf | ||
|
|
b3a010f6e9 | ||
|
|
b1332c838b |
@@ -0,0 +1,13 @@
|
|||||||
|
package space.kklochko.simple_jbdd.annotations;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.TYPE)
|
||||||
|
public @interface Title {
|
||||||
|
String value() default "Title";
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
package space.kklochko.simple_jbdd.test_examples;
|
|
||||||
|
|
||||||
public class Sum {
|
|
||||||
static int sum(int a, int b) {
|
|
||||||
return a + b;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package space.kklochko.simple_jbdd.test_examples.samples;
|
||||||
|
|
||||||
|
public class Sum {
|
||||||
|
public static int sum(int a, int b) {
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+15
@@ -0,0 +1,15 @@
|
|||||||
|
package space.kklochko.simple_jbdd.test_examples.tests;
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.annotations.Then;
|
||||||
|
import space.kklochko.simple_jbdd.annotations.Title;
|
||||||
|
import space.kklochko.simple_jbdd.tests.Test;
|
||||||
|
|
||||||
|
|
||||||
|
@Title("A Simple Failed GivenWhenTest for Sum")
|
||||||
|
public class SimpleFailedThenTest extends Test {
|
||||||
|
@Then
|
||||||
|
public void check() {
|
||||||
|
assert 30 == 10 + 20 + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+32
@@ -0,0 +1,32 @@
|
|||||||
|
package space.kklochko.simple_jbdd.test_examples.tests;
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.annotations.Given;
|
||||||
|
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.test_examples.samples.Sum;
|
||||||
|
import space.kklochko.simple_jbdd.tests.Test;
|
||||||
|
|
||||||
|
@Title("A Simple GivenWhenTest for Sum")
|
||||||
|
public class SimpleGivenWhenThenTest extends Test {
|
||||||
|
private Sum sum;
|
||||||
|
private int a;
|
||||||
|
private int b;
|
||||||
|
|
||||||
|
@Given
|
||||||
|
void setup() {
|
||||||
|
sum = new Sum();
|
||||||
|
}
|
||||||
|
|
||||||
|
@When
|
||||||
|
void trigger() {
|
||||||
|
a = 10;
|
||||||
|
b = 20;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Then
|
||||||
|
void check() {
|
||||||
|
assert 30 == sum.sum(a, b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package space.kklochko.simple_jbdd.test_examples.tests;
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.annotations.Then;
|
||||||
|
import space.kklochko.simple_jbdd.annotations.Title;
|
||||||
|
import space.kklochko.simple_jbdd.tests.Test;
|
||||||
|
|
||||||
|
@Title("Simple test")
|
||||||
|
public class SimpleThenTest extends Test {
|
||||||
|
@Then("The sum of 10 and 20 is 30.")
|
||||||
|
public void check() {
|
||||||
|
assert 30 == 10 + 20;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package space.kklochko.simple_jbdd.test_examples.tests;
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.annotations.Then;
|
||||||
|
import space.kklochko.simple_jbdd.annotations.Title;
|
||||||
|
import space.kklochko.simple_jbdd.tests.Test;
|
||||||
|
|
||||||
|
public class SimpleThenTestWithoutTitle extends Test {
|
||||||
|
@Then("The sum of 10 and 20 is 30.")
|
||||||
|
public void check() {
|
||||||
|
assert 30 == 10 + 20;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package space.kklochko.simple_jbdd.tests;
|
||||||
|
|
||||||
|
public abstract class Test {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package space.kklochko.simple_jbdd.tests.commands;
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.tests.Test;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
public abstract class AbstractTestCommand<T extends Test> {
|
||||||
|
protected String type;
|
||||||
|
protected String label;
|
||||||
|
|
||||||
|
public AbstractTestCommand(String label) {
|
||||||
|
setLabel(label);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract boolean test();
|
||||||
|
|
||||||
|
public Map<String, String> getLabels() {
|
||||||
|
Map<String, String> labels = new TreeMap<String, String>();
|
||||||
|
labels.put(getType(), this.getLabel());
|
||||||
|
return labels;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLabel(String label) {
|
||||||
|
this.label = label;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLabel() {
|
||||||
|
return label;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void setObject(T object);
|
||||||
|
|
||||||
|
public abstract T getObject();
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package space.kklochko.simple_jbdd.tests.commands;
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.tests.Test;
|
||||||
|
|
||||||
|
public class SimpleTestCommand<T extends Test> extends AbstractTestCommand<T> {
|
||||||
|
protected T object;
|
||||||
|
|
||||||
|
public SimpleTestCommand(String label, T object) {
|
||||||
|
super(label);
|
||||||
|
setObject(object);
|
||||||
|
setType("Title");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean test() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setObject(T object) {
|
||||||
|
this.object = object;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T getObject() {
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+55
@@ -0,0 +1,55 @@
|
|||||||
|
package space.kklochko.simple_jbdd.tests.commands.decorators;
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.tests.Test;
|
||||||
|
import space.kklochko.simple_jbdd.tests.commands.AbstractTestCommand;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public abstract class AbstractDecorator<T extends Test> extends AbstractTestCommand<T> {
|
||||||
|
protected AbstractTestCommand<T> command;
|
||||||
|
|
||||||
|
protected Method method;
|
||||||
|
|
||||||
|
public AbstractDecorator(String label, AbstractTestCommand<T> command) {
|
||||||
|
super(label);
|
||||||
|
setCommand(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract boolean test();
|
||||||
|
|
||||||
|
public Map<String, String> getLabels() {
|
||||||
|
Map<String, String> labels = getCommand().getLabels();
|
||||||
|
labels.put(getType(), this.getLabel());
|
||||||
|
return labels;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract boolean runMethod();
|
||||||
|
|
||||||
|
public void setMethod(Method method) {
|
||||||
|
this.method = method;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Method getMethod() {
|
||||||
|
return method;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setObject(T object) {
|
||||||
|
command.setObject(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T getObject() {
|
||||||
|
return command.getObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
public AbstractTestCommand<T> getCommand() {
|
||||||
|
return command;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCommand(AbstractTestCommand<T> command) {
|
||||||
|
this.command = command;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+39
@@ -0,0 +1,39 @@
|
|||||||
|
package space.kklochko.simple_jbdd.tests.commands.decorators;
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.tests.Test;
|
||||||
|
import space.kklochko.simple_jbdd.tests.commands.AbstractTestCommand;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
public class BlockDecorator<T extends Test> extends AbstractDecorator<T> {
|
||||||
|
public BlockDecorator(String label, AbstractTestCommand<T> command, Method method, String type) {
|
||||||
|
super(label, command);
|
||||||
|
setType(type);
|
||||||
|
setMethod(method);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean test() {
|
||||||
|
boolean result = runMethod();
|
||||||
|
|
||||||
|
if(!result)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return getCommand().test();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean runMethod() {
|
||||||
|
try {
|
||||||
|
method.setAccessible(true);
|
||||||
|
method.invoke(getCommand().getObject());
|
||||||
|
return true;
|
||||||
|
} catch (InvocationTargetException e) {
|
||||||
|
System.err.println(e.getMessage());
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
System.err.println("Check access modifiers.");
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
@@ -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 <T extends Test> void create(T input) {
|
||||||
|
Class<?> aClass = input.getClass();
|
||||||
|
Map<String, Method> test_methods = this.getTestMethods(aClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected <T extends Test> Map<String, Method> getTestMethods(Class<?> aClass) {
|
||||||
|
Map<String, Method> test_methods = new TreeMap<String, Method>();
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-1
@@ -1,5 +1,6 @@
|
|||||||
package space.kklochko.simple_jbdd.test_examples
|
package space.kklochko.simple_jbdd.test_examples.samples
|
||||||
|
|
||||||
|
import space.kklochko.simple_jbdd.test_examples.samples.Sum
|
||||||
import spock.lang.Specification
|
import spock.lang.Specification
|
||||||
|
|
||||||
class SumSpec extends Specification {
|
class SumSpec extends Specification {
|
||||||
+22
@@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user