How to use ConstructorBlock class of com.greghaskins.spectrum.internal.blocks package

Best Spectrum code snippet using com.greghaskins.spectrum.internal.blocks.ConstructorBlock

Source:Spectrum.java Github

copy

Full Screen

1package com.greghaskins.spectrum;2import com.greghaskins.spectrum.dsl.specification.Specification;3import com.greghaskins.spectrum.internal.DeclarationState;4import com.greghaskins.spectrum.internal.Suite;5import com.greghaskins.spectrum.internal.blocks.ConstructorBlock;6import com.greghaskins.spectrum.internal.junit.Rules;7import com.greghaskins.spectrum.internal.junit.RunNotifierReporting;8import org.junit.runner.Description;9import org.junit.runner.Runner;10import org.junit.runner.notification.RunNotifier;11import java.util.function.Supplier;12/**13 * Implements a BDD-style test runner, similar to RSpec and Jasmine. It uses JUnit's standard14 * reporting mechanisms ({@link org.junit.runner.Description}), but provides a completely different15 * way of writing tests. Annotate your class with {@code @RunWith(Spectrum.class)}, and use the16 * static methods to declare your specs.17 *18 * @see Specification#describe19 * @see Specification#it20 * @see Specification#beforeEach21 * @see Specification#afterEach22 * @see Specification#let23 */24public final class Spectrum extends Runner {25 /**26 * A generic code block with a {@link #run()} method to perform any action. Usually defined by a27 * lambda function.28 *29 * @deprecated since 1.1.0 - use {@link com.greghaskins.spectrum.Block} instead30 */31 @Deprecated32 @FunctionalInterface33 public interface Block extends com.greghaskins.spectrum.Block {34 /**35 * Execute the code block, raising any {@code Throwable} that may occur.36 *37 * @throws Throwable any uncaught Error or Exception38 */39 @Override40 void run() throws Throwable;41 }42 /**43 * Supplier that is allowed to throw.44 *45 * @param <T> type of object to supply46 * @deprecated since 1.1.0 - use {@link com.greghaskins.spectrum.ThrowingSupplier} instead47 */48 @Deprecated49 @FunctionalInterface50 public interface ThrowingSupplier<T> extends com.greghaskins.spectrum.ThrowingSupplier<T> {51 }52 /**53 * Declare a test suite that describes the expected behavior of the system in a given context.54 *55 * @param context Description of the context for this suite56 * @param block {@link com.greghaskins.spectrum.Block} with one or more calls to {@link57 * #it(String, com.greghaskins.spectrum.Block) it} that define each expected58 * behavior59 * @see Specification#describe60 */61 public static void describe(final String context, final com.greghaskins.spectrum.Block block) {62 Specification.describe(context, block);63 }64 /**65 * Focus on this specific suite, while ignoring others.66 *67 * @param context Description of the context for this suite68 * @param block {@link com.greghaskins.spectrum.Block} with one or more calls to {@link69 * #it(String, com.greghaskins.spectrum.Block) it} that define each expected70 * behavior71 * @see #describe(String, com.greghaskins.spectrum.Block)72 * @see Specification#fdescribe73 */74 public static void fdescribe(final String context, final com.greghaskins.spectrum.Block block) {75 Specification.fdescribe(context, block);76 }77 /**78 * Ignore the specific suite.79 *80 * @param context Description of the context for this suite81 * @param block {@link com.greghaskins.spectrum.Block} with one or more calls to {@link82 * #it(String, com.greghaskins.spectrum.Block) it} that define each expected83 * behavior84 * @see #describe(String, com.greghaskins.spectrum.Block)85 * @see Specification#xdescribe86 */87 public static void xdescribe(final String context, final com.greghaskins.spectrum.Block block) {88 Specification.xdescribe(context, block);89 }90 /**91 * Declare a spec, or test, for an expected behavior of the system in this suite context.92 *93 * @param behavior Description of the expected behavior94 * @param block {@link com.greghaskins.spectrum.Block} that verifies the system behaves as95 * expected and throws a {@link java.lang.Throwable Throwable} if that expectation96 * is not met.97 * @see Specification#it98 */99 public static void it(final String behavior, final com.greghaskins.spectrum.Block block) {100 Specification.it(behavior, block);101 }102 /**103 * Declare a pending spec (without a block) that will be ignored.104 *105 * @param behavior Description of the expected behavior106 * @see #xit(String, com.greghaskins.spectrum.Block)107 * @see Specification#it(String)108 */109 public static void it(final String behavior) {110 Specification.it(behavior);111 }112 /**113 * Focus on this specific spec, while ignoring others.114 *115 * @param behavior Description of the expected behavior116 * @param block {@link com.greghaskins.spectrum.Block} that verifies the system behaves as117 * expected and throws a {@link java.lang.Throwable Throwable} if that expectation118 * is not met.119 * @see #it(String, com.greghaskins.spectrum.Block)120 * @see Specification#fit121 */122 public static void fit(final String behavior, final com.greghaskins.spectrum.Block block) {123 Specification.fit(behavior, block);124 }125 /**126 * Mark a spec as ignored so that it will be skipped.127 *128 * @param behavior Description of the expected behavior129 * @param block {@link com.greghaskins.spectrum.Block} that will not run, since this spec is130 * ignored.131 * @see #it(String, com.greghaskins.spectrum.Block)132 * @see Specification#xit133 */134 public static void xit(final String behavior, final com.greghaskins.spectrum.Block block) {135 Specification.xit(behavior, block);136 }137 /**138 * Declare a {@link com.greghaskins.spectrum.Block} to be run before each spec in the suite.139 *140 * <p>141 * Use this to perform setup actions that are common across tests in the context. If multiple142 * {@code beforeEach} blocks are declared, they will run in declaration order.143 * </p>144 *145 * @param block {@link com.greghaskins.spectrum.Block} to run once before each spec146 * @see Specification#beforeEach147 */148 public static void beforeEach(final com.greghaskins.spectrum.Block block) {149 Specification.beforeEach(block);150 }151 /**152 * Declare a {@link com.greghaskins.spectrum.Block Block} to be run after each spec in the current153 * suite.154 *155 * <p>156 * Use this to perform teardown or cleanup actions that are common across specs in this suite. If157 * multiple {@code afterEach} blocks are declared, they will run in declaration order.158 * </p>159 *160 * @param block {@link com.greghaskins.spectrum.Block Block} to run once after each spec161 * @see Specification#afterEach162 */163 public static void afterEach(final com.greghaskins.spectrum.Block block) {164 Specification.afterEach(block);165 }166 /**167 * Declare a {@link com.greghaskins.spectrum.Block Block} to be run once before all the specs in168 * the current suite begin.169 *170 * <p>171 * Use {@code beforeAll} and {@link #afterAll(com.greghaskins.spectrum.Block) afterAll} blocks172 * with caution: since they only run once, shared state <strong>will</strong> leak across specs.173 * </p>174 *175 * @param block {@link com.greghaskins.spectrum.Block} to run once before all specs in this suite176 * @see Specification#beforeAll177 */178 public static void beforeAll(final com.greghaskins.spectrum.Block block) {179 Specification.beforeAll(block);180 }181 /**182 * Declare a {@link com.greghaskins.spectrum.Block} to be run once after all the specs in the183 * current suite have run.184 *185 * <p>186 * Use {@link #beforeAll(com.greghaskins.spectrum.Block) beforeAll} and {@code afterAll} blocks187 * with caution: since they only run once, shared state <strong>will</strong> leak across tests.188 * </p>189 *190 * @param block {@link com.greghaskins.spectrum.Block} to run once after all specs in this suite191 * @see Specification#afterAll192 */193 public static void afterAll(final com.greghaskins.spectrum.Block block) {194 Specification.afterAll(block);195 }196 /**197 * A value that will be fresh within each spec and cannot bleed across specs.198 *199 * <p>200 * Note that {@code let} is lazy-evaluated: the {@code supplier} is not called until the first201 * time it is used.202 * </p>203 *204 * @param <T> The type of value205 * @param supplier {@link com.greghaskins.spectrum.ThrowingSupplier} function that either206 * generates the value, or throws a {@link Throwable}207 * @return supplier which is refreshed for each spec's context208 * @see Specification#let209 */210 public static <T> Supplier<T> let(final com.greghaskins.spectrum.ThrowingSupplier<T> supplier) {211 return Specification.let(supplier);212 }213 private final Suite rootSuite;214 /**215 * Main constructor called via reflection by the JUnit runtime.216 *217 * @param testClass The class file that defines the current suite218 * @see org.junit.runner.Runner219 */220 public Spectrum(final Class<?> testClass) {221 this(Description.createSuiteDescription(testClass), createTestClassDefinitionBlock(testClass));222 }223 Spectrum(Description description, com.greghaskins.spectrum.Block definitionBlock) {224 this.rootSuite = Suite.rootSuite(description);225 DeclarationState.instance().beginDeclaration(this.rootSuite, definitionBlock);226 }227 @Override228 public Description getDescription() {229 return this.rootSuite.getDescription();230 }231 @Override232 public void run(final RunNotifier notifier) {233 this.rootSuite.run(new RunNotifierReporting(notifier));234 }235 /**236 * Links the test class construction to JUnit rules implementation. This creates a block which237 * when executed will perform test definition against Spectrum and also hooks JUnit rule238 * implementation to the definition based on any "@Rule" annotations on the members - see {@link239 * Rules}240 *241 * @param testClass type of the test object242 * @return a block with JUnit rules activated243 */244 private static <T> com.greghaskins.spectrum.Block createTestClassDefinitionBlock(245 final Class<T> testClass) {246 ConstructorBlock<T> constructTestClass = new ConstructorBlock<>(testClass);247 return () -> {248 constructTestClass.run();249 Rules.applyRules(constructTestClass.get(), DeclarationState.instance()::addHook);250 };251 }252}...

Full Screen

Full Screen

Source:RuleContext.java Github

copy

Full Screen

1package com.greghaskins.spectrum.internal.junit;2import static com.greghaskins.spectrum.internal.junit.StubJUnitFrameworkMethod.stubFrameworkMethod;3import com.greghaskins.spectrum.Block;4import com.greghaskins.spectrum.internal.blocks.ConstructorBlock;5import com.greghaskins.spectrum.internal.hooks.Hook;6import org.junit.AfterClass;7import org.junit.BeforeClass;8import org.junit.ClassRule;9import org.junit.Rule;10import org.junit.internal.runners.statements.RunAfters;11import org.junit.internal.runners.statements.RunBefores;12import org.junit.rules.MethodRule;13import org.junit.rules.RunRules;14import org.junit.rules.TestRule;15import org.junit.runner.Description;16import org.junit.runners.model.FrameworkMethod;17import org.junit.runners.model.Statement;18import org.junit.runners.model.TestClass;19import java.util.List;20import java.util.function.Supplier;21import java.util.stream.Collectors;22import java.util.stream.Stream;23/**24 * Tracks a junit.rule that must be applied to all descendants of a suite.25 */26public class RuleContext<T> implements Supplier<T> {27 private final Class<T> ruleClass;28 private final TestClass testClass;29 private T currentTestObject;30 private final boolean constructEveryTime;31 RuleContext(final Class<T> ruleClass) {32 this.ruleClass = ruleClass;33 this.testClass = new TestClass(ruleClass);34 this.constructEveryTime = true;35 }36 @SuppressWarnings("unchecked")37 RuleContext(final T object) {38 this.ruleClass = (Class<T>) object.getClass();39 this.testClass = new TestClass(this.ruleClass);40 this.currentTestObject = object;41 this.constructEveryTime = false;42 }43 @Override44 public T get() {45 return currentTestObject;46 }47 /**48 * Construct the class level hook.49 * @return the hook50 */51 Hook classHook() {52 return (description, notifier,53 block) -> withClassBlock(statementOf(block), fakeForJunit(description))54 .evaluate();55 }56 /**57 * Construct a hook for around methods.58 * @return the hook59 */60 Hook methodHook() {61 return (description, notifier, block) -> decorate(statementOf(block), fakeForJunit(description))62 .evaluate();63 }64 /**65 * Add the method and test rules execution around a test method statement.66 * @param base the base statement67 * @param description of the child68 * @return the statement to use to execute the child within the rules69 * @throws Throwable on error70 */71 private Statement decorate(final Statement base, final Description description) throws Throwable {72 if (constructEveryTime) {73 constructTestObject();74 }75 return withTestRules(getTestRules(currentTestObject),76 withMethodRules(base, getMethodRules(currentTestObject)), description);77 }78 private void constructTestObject() throws Throwable {79 ConstructorBlock<T> constructor = new ConstructorBlock<>(ruleClass);80 constructor.run();81 currentTestObject = constructor.get();82 }83 private Statement withMethodRules(final Statement base, final List<MethodRule> methodRules) {84 FrameworkMethod method = stubFrameworkMethod();85 return decorateWithMethodRules(base, methodRules, method);86 }87 private Statement decorateWithMethodRules(final Statement base,88 final List<MethodRule> methodRules,89 final FrameworkMethod method) {90 Statement result = base;91 for (MethodRule each : methodRules) {92 result = each.apply(result, method, currentTestObject);93 }...

Full Screen

Full Screen

Source:ConstructorBlock.java Github

copy

Full Screen

2import com.greghaskins.spectrum.Block;3import java.lang.reflect.Constructor;4import java.lang.reflect.InvocationTargetException;5import java.util.function.Supplier;6public final class ConstructorBlock<T> implements Block, Supplier<Object> {7 private final Class<T> klass;8 private T testObject;9 public ConstructorBlock(final Class<T> klass) {10 this.klass = klass;11 }12 @Override13 public void run() throws Throwable {14 try {15 final Constructor<T> constructor = this.klass.getDeclaredConstructor();16 constructor.setAccessible(true);17 testObject = constructor.newInstance();18 } catch (final InvocationTargetException invocationTargetException) {19 throw invocationTargetException.getTargetException();20 } catch (final Exception error) {21 throw new UnableToConstructSpecException(this.klass, error);22 }23 }...

Full Screen

Full Screen

ConstructorBlock

Using AI Code Generation

copy

Full Screen

1public class ConstructorBlockTest {2 public void test() {3 ConstructorBlock cb = new ConstructorBlock();4 cb.beforeAll(() -> System.out.println("beforeAll"));5 cb.beforeEach(() -> System.out.println("beforeEach"));6 cb.it("test1", () -> System.out.println("test1"));7 cb.it("test2", () -> System.out.println("test2"));8 cb.afterEach(() -> System.out.println("afterEach"));9 cb.afterAll(() -> System.out.println("afterAll"));10 cb.run();11 }12}13public class ConstructorBlockTest {14 public void test() {15 ConstructorBlock cb = new ConstructorBlock();16 cb.beforeAll(() -> System.out.println("beforeAll"));17 cb.beforeEach(() -> System.out.println("beforeEach"));18 cb.it("test1", () -> System.out.println("test1"));19 cb.it("test2", () -> System.out.println("test2"));20 cb.afterEach(() -> System.out.println("afterEach"));21 cb.afterAll(() -> System.out.println("afterAll"));22 cb.run();23 }24}25public class ConstructorBlockTest {26 public void test() {27 ConstructorBlock cb = new ConstructorBlock();28 cb.beforeAll(() -> System.out.println("beforeAll"));29 cb.beforeEach(() -> System.out.println("beforeEach"));30 cb.it("test1", () -> System.out.println("test1"));31 cb.it("test2", () -> System.out.println("test2"));32 cb.afterEach(() -> System.out.println("afterEach"));33 cb.afterAll(() -> System.out.println("afterAll"));34 cb.run();35 }36}37public class ConstructorBlockTest {38 public void test() {39 ConstructorBlock cb = new ConstructorBlock();40 cb.beforeAll(() -> System.out.println("beforeAll"));41 cb.beforeEach(() -> System.out.println("beforeEach"));42 cb.it("test1", () -> System.out.println("test1"));

Full Screen

Full Screen

ConstructorBlock

Using AI Code Generation

copy

Full Screen

1import com.greghaskins.spectrum.internal.blocks.ConstructorBlock;2public class 1 {3 public static void main(String[] args) {4 ConstructorBlock cb=new ConstructorBlock("1.java");5 cb.run();6 }7}8package com.greghaskins.spectrum.internal.blocks;9import com.greghaskins.spectrum.internal.Block;10public class ConstructorBlock extends Block {11 private String sourceFile;12 public ConstructorBlock(String sourceFile) {13 this.sourceFile = sourceFile;14 }15 public void run() {16 System.out.println(sourceFile);17 }18}

Full Screen

Full Screen

ConstructorBlock

Using AI Code Generation

copy

Full Screen

1ConstructorBlock cb = new ConstructorBlock("com.greghaskins.spectrum.internal.blocks.ConstructorBlock");2cb.setConstructor("com.greghaskins.spectrum.internal.blocks.ConstructorBlock");3cb.setConstructor("com.greghaskins.spectrum.internal.blocks.ConstructorBlock", "java.lang.String");4cb.setConstructor("com.greghaskins.spectrum.internal.blocks.ConstructorBlock", "java.lang.String", "java.lang.String");5cb.setConstructor("com.greghaskins.spectrum.internal.blocks.ConstructorBlock", "java.lang.String", "java.lang.String", "java.lang.String");6cb.setConstructor("com.greghaskins.spectrum.internal.blocks.ConstructorBlock", "java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String");7cb.setConstructor("com.greghaskins.spectrum.internal.blocks.ConstructorBlock", "java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String");8cb.setConstructor("com.greghaskins.spectrum.internal.blocks.ConstructorBlock", "java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String");9cb.setConstructor("com.greghaskins.spec

Full Screen

Full Screen

ConstructorBlock

Using AI Code Generation

copy

Full Screen

1import static com.greghaskins.spectrum.Spectrum.beforeEach;2import com.greghaskins.spectrum.Spectrum;3import com.greghaskins.spectrum.internal.blocks.ConstructorBlock;4public class 1 {5 public static void main(String[] args) {6 Spectrum.describe("A class", () -> {7 beforeEach(new ConstructorBlock<>(A.class));8 });9 }10}11import static com.greghaskins.spectrum.Spectrum.beforeEach;12import com.greghaskins.spectrum.Spectrum;13import com.greghaskins.spectrum.internal.blocks.ConstructorBlock;14public class 2 {15 public static void main(String[] args) {16 Spectrum.describe("A class", () -> {17 beforeEach(new ConstructorBlock<>(A.class, "arg1", "arg2"));18 });19 }20}21import static com.greghaskins.spectrum.Spectrum.beforeEach;22import com.greghaskins.spectrum.Spectrum;23import com.greghaskins.spectrum.internal.blocks.ConstructorBlock;24public class 3 {25 public static void main(String[] args) {26 Spectrum.describe("A class", () -> {27 beforeEach(new ConstructorBlock<>(A.class, new Object[] { "arg1", "arg2" }));28 });29 }30}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Spectrum automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in ConstructorBlock

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful