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

Using Java 8 lambdas as tested classes #27

Open
VsevolodGolovanov opened this issue Apr 15, 2016 · 0 comments
Open

Using Java 8 lambdas as tested classes #27

VsevolodGolovanov opened this issue Apr 15, 2016 · 0 comments

Comments

@VsevolodGolovanov
Copy link

I'm currently using parameterized tests with anonymous inner classes:

@RunWith(Parameterized.class)
public class RulesTest extends AbstractDetectorTest<MyDetector> {

    enum Expectation {
        FAIL, PASS
    };

    private Expectation expectation;
    private Object parameter;

    public RulesTest(int ln, Expectation expectation, Object parameter) {
        this.expectation = expectation;
        this.parameter = parameter;
    }

    // use source line number in test's name to quickly find the failing dataset
    @Parameters(name = "Line {0}")
    @SuppressWarnings({ "unused" })
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {
            //@formatter:off
            { ln(), PASS, new Object() { void $() { someCodeToCheck(); } } },
            { ln(), PASS, new Object() { void $() { moreCodeToCheck(); } } },
            // ... more parameter sets ...
            //@formatter:on
        });
    }

    @Test
    public void test() throws Exception {
        switch (expectation) {
        case FAIL:
            DetectorAssert.assertBugReported(parameter.getClass(), detector, bugReporter);
            break;
        case PASS:
            DetectorAssert.assertNoBugsReported(parameter.getClass(), detector, bugReporter);
            break;
        default:
            checkState(false);
        }
    }

}

I want to get rid on the verbose new Object() { void $() { } stuff. Let's try using lambdas for this:

interface $ {
    public void method();
}

...
    { ln(), PASS, ($)() -> someCodeToCheck() },

This fails when running the test:

edu.umd.cs.findbugs.classfile.MissingClassException: Resource not found: ru/argustelecom/designtimeutils/findbugs/detectors/RulesTest$$Lambda$1/42121758.class
    at edu.umd.cs.findbugs.classfile.engine.ClassDataAnalysisEngine.analyze(ClassDataAnalysisEngine.java:60)
    at edu.umd.cs.findbugs.classfile.engine.ClassDataAnalysisEngine.analyze(ClassDataAnalysisEngine.java:42)
    at edu.umd.cs.findbugs.classfile.impl.AnalysisCache.getClassAnalysis(AnalysisCache.java:262)
    at edu.umd.cs.findbugs.classfile.engine.bcel.JavaClassAnalysisEngine.analyze(JavaClassAnalysisEngine.java:57)
    at edu.umd.cs.findbugs.classfile.engine.bcel.JavaClassAnalysisEngine.analyze(JavaClassAnalysisEngine.java:42)
    at edu.umd.cs.findbugs.classfile.impl.AnalysisCache.getClassAnalysis(AnalysisCache.java:262)
    at edu.umd.cs.findbugs.classfile.engine.bcel.ClassContextClassAnalysisEngine.analyze(ClassContextClassAnalysisEngine.java:49)
    at edu.umd.cs.findbugs.classfile.engine.bcel.ClassContextClassAnalysisEngine.analyze(ClassContextClassAnalysisEngine.java:37)
    at edu.umd.cs.findbugs.classfile.impl.AnalysisCache.getClassAnalysis(AnalysisCache.java:262)
    at edu.umd.cs.findbugs.DetectorToDetector2Adapter.visitClass(DetectorToDetector2Adapter.java:72)
    at com.youdevise.fbplugins.tdd4fb.DetectorRunner$Singleton.doRunDetectorOnClass(DetectorRunner.java:247)
    at com.youdevise.fbplugins.tdd4fb.DetectorRunner$Singleton.access$000(DetectorRunner.java:70)
    at com.youdevise.fbplugins.tdd4fb.DetectorRunner.runDetectorOnClass(DetectorRunner.java:286)
    at com.youdevise.fbplugins.tdd4fb.DetectorAssert.assertNoBugsReported(DetectorAssert.java:107)
    at com.youdevise.fbplugins.tdd4fb.DetectorAssert.assertNoBugsReported(DetectorAssert.java:98)
    at ru.argustelecom.designtimeutils.findbugs.detectors.RulesTest.test(RulesTest.java:166)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    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.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.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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    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: edu.umd.cs.findbugs.classfile.ResourceNotFoundException: Resource not found: ru/argustelecom/designtimeutils/findbugs/detectors/RulesTest$$Lambda$1/42121758.class
    at edu.umd.cs.findbugs.classfile.impl.ClassPathImpl.lookupResource(ClassPathImpl.java:164)
    at edu.umd.cs.findbugs.classfile.engine.ClassDataAnalysisEngine.analyze(ClassDataAnalysisEngine.java:53)
    ... 55 more

Is it possible to support lambdas for such usage?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant