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

Tests within inner classes that are children of an abstract class are not executed despite @Nested #4125

Closed
SIMULATAN opened this issue Nov 13, 2024 · 3 comments

Comments

@SIMULATAN
Copy link

Expected

Tests declared within a @Nested inner class inheriting from their parent abstract class should run out of the box.

Actual

No tests are picked up.

Rationale

I want to test multiple strategies (MapperEngine). In order to verify feature parity, the tests are abstract functions.
To avoid boilerplate and group the tests, the strategy implementation verifications are inner classes of the abstract class.

Steps to reproduce

abstract class MapperEngineTest {
  abstract fun map()

  @Nested
  inner class Liquid : MapperEngineTest() {
    @Test
    // this test should run, but it doesn't
    override fun map() { ... }
  }

  @Nested
  inner class Replace {
    @Test
    // doesn't run either (this time without inheriting the abstract class)
    fun map() { ... }
  }
}

Context

  • Used versions: JUnit 5.10.2
  • Build Tool/IDE: Gradle 8.10.2, IntelliJ 2024.3 RC
@marcphilipp
Copy link
Member

For @Nested on inner classes to work, JUnit needs to be able to create an instance of the enclosing class. Since it's abstract that doesn't work here. Instead of using @Nested (which requires inner classes) you should use regular "static" classes:

abstract class MapperEngineTest {
    abstract fun map()

    class Liquid : MapperEngineTest() {
        @Test
        override fun map() { fail("boom") }
    }

    class Replace {
        @Test
        fun map() { fail("boom") }
    }
}

@marcphilipp
Copy link
Member

I've added this issue to #242 which will introduce an error reporting mechanism that should be triggered if such a @Nested annotation is found on a inner class of an abstract class so you'd notice what's wrong sooner.

@SIMULATAN
Copy link
Author

@marcphilipp thank you for the quick help, that works as expected indeed!
The error report mechanism would be awesome to avoid such issues in the future.

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

No branches or pull requests

2 participants