Code Monkey home page Code Monkey logo

javaspec's People

Contributors

kkrull avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

smiklos

javaspec's Issues

Non-public context classes raise UnsupportedConstructorException

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 initialized twice

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);
    }

Test class fails to initialize

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;

Context class lacking It field leaves spinner in IntelliJ

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 */ }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.