sbt-jupiter-interface icon indicating copy to clipboard operation
sbt-jupiter-interface copied to clipboard

Test class is not enclosed

Open csekol opened this issue 4 years ago • 3 comments

Hi,

I tried to execute the test interfaces from https://junit.org/junit5/docs/current/user-guide/#writing-tests-test-interfaces-and-default-methods, but I got an exception:

[info] Test com.example.StringTests#returnsNegativeNumberWhenComparedToLargerValue() started
[error] Test com.example.StringTests failed: java.lang.RuntimeException: Test class com.example.ComparableContract is not enclosed by com.example.StringTests, took 0.036s
[error]     at net.aichler.jupiter.internal.event.TaskName.nestedSuiteId(TaskName.java:135)
[error]     at net.aichler.jupiter.internal.event.TaskName.of(TaskName.java:116)
[error]     at net.aichler.jupiter.internal.event.Dispatcher.lambda$executionFinished$2(Dispatcher.java:99)
[error]     at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
[error]     at net.aichler.jupiter.internal.event.Dispatcher.executionFinished(Dispatcher.java:95)
[error]     ...
[info] Test run finished: 1 failed, 0 ignored, 6 total, 0.115s

Is this an issue?

Thanks, csekol

csekol avatar May 27 '20 06:05 csekol

I'm having this exact issue stemming from running suites, is there a way to mitigate this issue?

D3Castro avatar Sep 06 '22 16:09 D3Castro

I have the same stack trace with a test class that inherits @Nested inner classes

class BaseTests {
  @Nested class Foo {
    @Test void bar() {
    }
  }
}

class DerivedTests extends BaseTests {
}

I'm currently avoiding the exception by modifying

        if (!className.startsWith(testSuite)) {
            throw new RuntimeException(
                    "Test class " + className + " is not enclosed by " + testSuite);
        }

with

        if (!className.startsWith(testSuite)) {
            // ad-hoc fix
            var dollar = className.indexOf('$');
            if (dollar >= 0)
                return className.substring(dollar);

            throw new RuntimeException(
                    "Test class " + className + " is not enclosed by " + testSuite);
        }

(this seems to be working with more than one level of @Nested)

https://github.com/sbt/sbt-jupiter-interface/blob/0.11.1/src/library/src/main/java/net/aichler/jupiter/internal/event/TaskName.java#L134-L137

ken1ma avatar Nov 13 '22 06:11 ken1ma

Hi and thank you for providing another sample.

When I run both samples as a unit test in the library project, the exception isn't thrown. Only when I add them to the scripted tests. Not sure what's going on yet, might be something in how the tests are collected by the TestRunner in the library compared to SBT for scripted tests?

Have to investigate further.

maichler avatar Nov 13 '22 17:11 maichler