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

Improve error message if multiple "test" annotations are present on a method #876

Closed
ctapobep opened this issue Jun 9, 2017 · 14 comments
Closed

Comments

@ctapobep
Copy link

ctapobep commented Jun 9, 2017

For the following test two descriptors will be created -- one for @ParameterizedTest and another for @Test:

@ParameterizedTest
@ValueSource(strings = "blah")
@Test
void blah(String blah) {}

The parameterized test passes, while the other one fails because JUnit Juiter cannot resolve the parameter:

org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [java.lang.String arg0] in executable [void io.qala.datagen.junit5.Junit5ParameterizedTest.blah(java.lang.String)].

I don't know if it's a bug or a feature, but because I didn't know about this I spent an hour debugging JUnit. I assume many more people could step on this, so would be great to make the logic more obvious. Couple of options:

  • Ignore @Test if some other test descriptor is created
  • Make error message clearer: ... This can happen e.g. if 2 different Test annotations are put on one method.
  • Use mechanisms for creating Test Descriptors similar to how ParameterResolver works: boolean supportsTest(...)

Related Issues

@marcphilipp
Copy link
Member

Agreed, there should definitely be a better error message.

@marcphilipp marcphilipp added this to the 5.0 M6 milestone Jun 10, 2017
@sbrannen sbrannen changed the title 2 different test descriptors are created if we put both @Test & @ParameterizedTest Improve error message if both @Test and @ParameterizedTest are present on a method Jul 11, 2017
@sbrannen sbrannen self-assigned this Jul 11, 2017
@sbrannen sbrannen changed the title Improve error message if both @Test and @ParameterizedTest are present on a method Improve error message if multiple "test" annotations are present on a method Jul 11, 2017
@sbrannen
Copy link
Member

sbrannen commented Jul 11, 2017

Note that this is likely a more general topic involving all types of "test" annotations such as @Test, @TestTemplate, and @TestFactory.

Keep in mind that @RepeatedTest and @ParameterizedTest are specializations of @TestTemplate.

I have therefore changed this issue's title accordingly.

@akozlova
Copy link

BTW I've wrote inspections for IntelliJ to highlight such dbl tests.

@sbrannen
Copy link
Member

BTW I've wrote inspections for IntelliJ to highlight such dbl tests.

Nice, @akozlova! 👍

@sbrannen
Copy link
Member

@akozlova, in IntelliJ do you simply inspect for multiple annotations meta-annotated with @Testable?

Or do you check for individual "test" annotations?

@akozlova
Copy link

Now it's only check for predefined annotations, mainly due to the fact that IDEA knows nothing about custom engines and thus can't check them: who knows, probably some engine would require both parameterized and some testable annotation due to the fact that jupiter won't be available/filtered.

@sbrannen
Copy link
Member

Now it's only check for predefined annotations, mainly due to the fact that IDEA knows nothing about custom engines and thus can't check them:

Makes sense!

who knows, probably some engine would require both parameterized and some testable annotation due to the fact that jupiter won't be available/filtered.

Yeah, I was pondering the exact same thing. 😉

@smoyer64
Copy link
Contributor

smoyer64 commented Jul 11, 2017

I accidentally ended up with @Test and @TestTemplate on the same test method (converting from a non-parameterized test to a parameterized test) and it actually ran both. When complete, there were five tests provided by the parameterized annotation and one test by the normal test annotation. Note that the non-parameterized test failed due to lack of parameter resolution.

@sbrannen
Copy link
Member

in progress

@sbrannen
Copy link
Member

sbrannen commented Jul 16, 2017

Update:

In my local branch, JUnit Jupiter now logs the following warning in such cases.

18:50:43.774 [main] WARN  org.junit.jupiter.engine.discovery.JavaElementsResolver - Possible configuration error: method [void org.junit.jupiter.engine.MultipleTestableAnnotationsTests.testAndRepeatedTest(org.junit.jupiter.api.RepetitionInfo)] resulted in multiple TestDescriptors [org.junit.jupiter.engine.descriptor.TestTemplateTestDescriptor, org.junit.jupiter.engine.descriptor.MethodTestDescriptor]. This is typically the result of annotating a method with multiple competing annotations such as @Test, @RepeatedTest, @ParameterizedTest, @TestFactory, etc.

How does that sound?

@sbrannen
Copy link
Member

Note that, with the aforementioned log statement, the test would still fail in the same manner as described in this issue's description, but the warning would help to determine the cause.

@marcphilipp
Copy link
Member

How does that sound?

SGTM! 👍

sbrannen added a commit that referenced this issue Jul 16, 2017
Prior to this commit, the execution of a test method could fail with a
misleading exception message if the method was annotated with more than
one "test" annotation -- for example, with @test and @ParameterizedTest.

This commit addresses this issue by logging a warning if more than one
TestDescriptor is resolved for a single method. This helps to debug
errors resulting from a method being simultaneously annotated with
multiple competing annotations such as @test, @RepeatedTest,
@ParameterizedTest, @testfactory, etc.

Issue: #876
@sbrannen
Copy link
Member

Resolved in master in commit 1ffeb82.

@yangyechi
Copy link

Good!

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

6 participants