kkrull / javaspec Goto Github PK
View Code? Open in Web Editor NEWSpec-style testing for Java
Home Page: https://javaspec.info
License: MIT License
Spec-style testing for Java
Home Page: https://javaspec.info
License: MIT License
Running JavaSpecRunner
on this class
package info.javaspecproto;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import info.javaspec.dsl.It;
import info.javaspec.runner.JavaSpecRunner;
import org.junit.runner.RunWith;
@RunWith(JavaSpecRunner.class)
public class HiddenInnerContext {
class hidden {
It runs = () -> assertThat(1, equalTo(1));
}
}
causes this exception
info.javaspec.runner.FieldExample$UnsupportedConstructorException: Unable to find a no-argument constructor for class info.javaspecproto.HiddenInnerContext$hidden
at info.javaspec.runner.FieldExample$ContextFactory.makeEnclosedInstance(FieldExample.java:123)
at info.javaspec.runner.FieldExample$ContextFactory.makeInstance(FieldExample.java:95)
at info.javaspec.runner.FieldExample$ContextFactory.initNewContext(FieldExample.java:72)
at info.javaspec.runner.FieldExample.readTestFunction(FieldExample.java:57)
at info.javaspec.runner.FieldExample.isSkipped(FieldExample.java:40)
at info.javaspec.runner.JavaSpecRunner.runChild(JavaSpecRunner.java:103)
at info.javaspec.runner.JavaSpecRunner.runChild(JavaSpecRunner.java:1)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.NoSuchMethodException: info.javaspecproto.HiddenInnerContext$hidden.<init>(info.javaspecproto.HiddenInnerContext)
at java.lang.Class.getConstructor0(Class.java:2971)
at java.lang.Class.getConstructor(Class.java:1812)
at info.javaspec.runner.FieldExample$ContextFactory.makeEnclosedInstance(FieldExample.java:121)
... 18 more
Typically nobody else imports or refers to a context class, so there's no real need for them to be public.
Fields in a spec class are initialized twice. If that initialization has side effects, then the side effects happen twice.
Take the following example, which creates 2 temporary files but only deletes 1 of them.
@RunWith(JavaSpecRunner.class)
public class TestWithFile {
private Closeable subject;
class after_opening {
private File path = File.createTempFile("DeletableOnlyWhenClosed", ".txt"); //RUNS TWICE
Establish that = () -> subject = ...
Because of = () -> subject.close();
It closes_the_underlying_stream = () -> FileUtils.forceDelete(path);
}
info.javaspec.spec.ClassFactory$UnsupportedConstructor: Unable to find a no-argument constructor for class com.wikitab.difftab.tablature.score.UploadTablatureHttpCommandTest
at info.javaspec.spec.ClassFactory$UnsupportedConstructor.forClass(ClassFactory.java:40)
at info.javaspec.spec.ClassFactory.makeInstance(ClassFactory.java:15)
at info.javaspec.spec.SpecExecutionContext.makeInstance(SpecExecutionContext.java:44)
at info.javaspec.spec.SpecExecutionContext.makeAndRememberInstance(SpecExecutionContext.java:36)
at info.javaspec.spec.SpecExecutionContext.makeInstance(SpecExecutionContext.java:46)
at info.javaspec.spec.SpecExecutionContext.makeAndRememberInstance(SpecExecutionContext.java:36)
at info.javaspec.spec.SpecExecutionContext.makeInstance(SpecExecutionContext.java:46)
at info.javaspec.spec.SpecExecutionContext.makeAndRememberInstance(SpecExecutionContext.java:36)
at info.javaspec.spec.SpecExecutionContext.forDeclaringClass(SpecExecutionContext.java:18)
at info.javaspec.spec.FieldSpec$DeclaredState.instantiate(FieldSpec.java:58)
at info.javaspec.spec.FieldSpec.run(FieldSpec.java:36)
at info.javaspec.context.ClassContext.lambda$run$4(ClassContext.java:57)
at java.util.LinkedList$LLSpliterator.forEachRemaining(LinkedList.java:1235)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
at info.javaspec.context.ClassContext.run(ClassContext.java:57)
at info.javaspec.context.ClassContext.lambda$run$5(ClassContext.java:58)
at java.util.LinkedList$LLSpliterator.forEachRemaining(LinkedList.java:1235)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
at info.javaspec.context.ClassContext.run(ClassContext.java:58)
at info.javaspec.context.ClassContext.lambda$run$5(ClassContext.java:58)
at java.util.LinkedList$LLSpliterator.forEachRemaining(LinkedList.java:1235)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
at info.javaspec.context.ClassContext.run(ClassContext.java:58)
at info.javaspec.runner.JavaSpecRunner.run(JavaSpecRunner.java:60)
at org.junit.runners.Suite.runChild(Suite.java:127)
at org.junit.runners.Suite.runChild(Suite.java:26)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at info.javaspec.spec.OuterClassFactory.makeInstance(OuterClassFactory.java:13)
at info.javaspec.spec.ClassFactory.makeInstance(ClassFactory.java:11)
... 39 more
Caused by: java.lang.NullPointerException
at <test class>.<init>
... 45 more
The test class in question passes when it has this:
private TempDirFixture tempDir;
Establish that = () -> tempDir = TempDirFixture.forClass(getClass());
Cleanup delete_temp_dir = () -> tempDir.delete();
and it fails like this:
private TempDirFixture tempDir;
Establish that = () -> tempDir = TempDirFixture.forClass(getClass());
Cleanup delete_temp_dir = tempDir::delete;
If you have a context class like this:
public class given_some_context_i_will_fill_in_later {
Establish that = () -> subject = new Widget();
Because of = () -> subject.foo();
// It is_waiting_for_me_to_figure_out_what_i_want_it_to_do;
}
...and run the test in IntelliJ, it runs any other tests and leaves behind a yellow status (which usually indicates a test that didn't report back to JUnit) for a 'given some context i will fill in later', which is empty.
Is JavaSpec trying to run an empty context, when the context should really just be ignored?
I've also seen the same behavior with static helper classes, such as:
public class MyTest {
private Consumer<Widget> = Mockito.mock(WidgetConsumer.class);
It does_something_cool -> ...
private static abstract class WidgetConsumer implements Consumer<Widget> { /* Make it easier to declare generic mock */ }
}
Grade needs to detect if a test is finished and the current version (1.0) fails to signal that if a test fails.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.