Skip to content

AddMissingNested adds @Nested to abstract classes, doesn't add @Nested to subclasses without their own test methods #759

@estekhin

Description

@estekhin

org.openrewrite.java.testing.junit5.AddMissingNested recipe adds @Nested to abstract classes and ignores effectively-@Nested subclasses that don't have their own @Test methods but inherit some from their superclasses.

@Nested on abstract class causes java.lang.InstantiationException at runtime.
See junit-team/junit-framework#2717, or junit-team/junit-framework#2717 (comment) more specifically for explanation.

Missing @Nested on a class which doesn't have its own @Test methods but inherits @Test methods from its possible abstract superclasses leads to missed tests - this non-annotated class will not be detected by JUnit 5.

What version of OpenRewrite are you using?

  • rewrite-maven-plugin 6.11.0
  • rewrite-testing-frameworks 3.10.0

What is the smallest, simplest way to reproduce the problem?

import org.junit.jupiter.api.Test;

public class AddMissingNestedTest {

    abstract class CommonNestedBase {
        @Test
        void commonNestedTest() {
        }
    }

    class NestedTest extends CommonNestedBase {
    }

}

What did you expect to see?

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

public class AddMissingNestedTest {

    abstract class CommonNestedBase {
        @Test
        void commonNestedTest() {
        }
    }

    @Nested
    class NestedTest extends CommonNestedBase {
    }

}

What did you see instead?

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

public class AddMissingNestedTest {

    @Nested
    abstract class CommonNestedBase {
        @Test
        void commonNestedTest() {
        }
    }

    class NestedTest extends CommonNestedBase {
    }

}

Are you interested in contributing a fix to OpenRewrite?

Seems like the case with adding @Nested to abstract class can be solved by adding the corresponding check near https://github.com/openrewrite/rewrite-testing-frameworks/blob/main/src/main/java/org/openrewrite/java/testing/junit5/AddMissingNested.java#L90.

Right now I am not sure how to approach the case with nested test class without test methods that inherits test methods for some of its superclasses.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type
No fields configured for issues without a type.

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions