Skip to content

Recipe to discover test methods with a non-void return type #790

@timtebeek

Description

@timtebeek

What problem are you trying to solve?

Some tests were not discovered or ran, leading to broken test coverage and warnings on 5.13+

So when we detect a method declaration annotated with any of the test annotations (@Test, @ParameterizedTest, @RepeatedTest, ...), which does not have void return type:

  • Change the method return type to void
  • Remove any return statements from the body, which are not inside a nested lambda or class

Describe the situation before applying the recipe

import org.junit.jupiter.api.Test;
class A {
    @Test
    int foo() {
        int i = 42;
        return i;
    }
}

Describe the situation after applying the recipe

import org.junit.jupiter.api.Test;
class A {
    @Test
    void foo() {
        int i = 42;
    }
}

Have you considered any alternatives or workarounds?

  • Add a warning comment about test discovery: Already done at test execution and in IDE.
  • Remove the test annotation; perhaps safer in case the test method is called anywhere, but perhaps more unexpected.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions