From 9939195984f3cdc8d5652f567d2e50f5c06c4f6d Mon Sep 17 00:00:00 2001 From: Ashley Frieze Date: Thu, 1 Dec 2016 15:41:45 +0000 Subject: [PATCH] Demonstrated and referred to the new inclusion and DI ideas. --- README.md | 1 + .../com/greghaskins/spectrum/Spectrum.java | 9 +++ src/test/java/specs/IncludingSpecs.java | 67 +++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 src/test/java/specs/IncludingSpecs.java diff --git a/README.md b/README.md index 52a32d8..97f291d 100644 --- a/README.md +++ b/README.md @@ -449,6 +449,7 @@ Spectrum also supports: - Compatibility with existing JUnit tools; no configuration required - Mixing Spectrum tests and normal JUnit tests in the same project suite - RSpec-style `aroundEach` and `aroundAll` hooks for advanced users and plugin authors +- Bringing together modularised tests with dependency injection - see [IncludingSpecs.java](src/test/java/specs/IncludingSpecs.java) ### Non-Features diff --git a/src/main/java/com/greghaskins/spectrum/Spectrum.java b/src/main/java/com/greghaskins/spectrum/Spectrum.java index 0ccf492..6b1cc2e 100644 --- a/src/main/java/com/greghaskins/spectrum/Spectrum.java +++ b/src/main/java/com/greghaskins/spectrum/Spectrum.java @@ -64,6 +64,15 @@ public interface Block extends com.greghaskins.spectrum.Block { public interface ThrowingSupplier extends com.greghaskins.spectrum.ThrowingSupplier { } + /** + * Include a test suite into the current one. + * @param testClass the class containing the Spectrum definitions + * @throws Throwable on error + */ + public static void include(final Class testClass) throws Throwable { + new ConstructorBlock(testClass).run(); + } + /** * Declare a test suite that is made of interdependent children. The whole suite should pass * atomically and if it fails, any remaining children can stop running. diff --git a/src/test/java/specs/IncludingSpecs.java b/src/test/java/specs/IncludingSpecs.java new file mode 100644 index 0000000..239094a --- /dev/null +++ b/src/test/java/specs/IncludingSpecs.java @@ -0,0 +1,67 @@ +package specs; + +import static com.greghaskins.spectrum.Spectrum.describe; +import static com.greghaskins.spectrum.Spectrum.include; +import static com.greghaskins.spectrum.Spectrum.it; +import static com.greghaskins.spectrum.Spectrum.let; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +import com.greghaskins.spectrum.Spectrum; + +import org.junit.runner.RunWith; + +import java.util.function.Supplier; + +/** + * Examples of how to include specs into Spectrum with or without dependency injection. + */ +@RunWith(Spectrum.class) +public class IncludingSpecs { + static void functionToInclude(Supplier toTest) { + describe("modularised or reusable by injected function", () -> { + it("can access the dependency passed in", () -> { + assertThat(toTest.get(), is("Hello world!")); + }); + }); + } + + static class ClassToIncludeWithDependencyInjection { + ClassToIncludeWithDependencyInjection(Supplier toTest) { + describe("modularised or reusable by injected object", () -> { + it("can access the dependency passed in", () -> { + assertThat(toTest.get(), is("Hello world!")); + }); + }); + } + } + + static class TestModule { + { + describe("test module", () -> { + it("can be included and executed without having to be annotated for JUnit running", () -> { + + }); + + it("allows for tests to be broken into separate classes, but run within the " + + "inclusion rules of a parent suite", () -> { + + }); + }); + } + } + + // weave the above together + { + describe("Dependency injection example", () -> { + Supplier string = let(() -> "Hello world!"); + + functionToInclude(string); + new ClassToIncludeWithDependencyInjection(string); + }); + + describe("Including modules example", () -> { + include(TestModule.class); + }); + } +}