Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demonstrated and referred to the new inclusion and DI ideas. #70

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/greghaskins/spectrum/Spectrum.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ public interface Block extends com.greghaskins.spectrum.Block {
public interface ThrowingSupplier<T> extends com.greghaskins.spectrum.ThrowingSupplier<T> {
}

/**
* 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.
Expand Down
67 changes: 67 additions & 0 deletions src/test/java/specs/IncludingSpecs.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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> string = let(() -> "Hello world!");

functionToInclude(string);
new ClassToIncludeWithDependencyInjection(string);
});

describe("Including modules example", () -> {
include(TestModule.class);
});
}
}