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

NPE when using nested JUnit5 classes #4393

Closed
acoburn opened this issue Oct 5, 2019 · 10 comments
Closed

NPE when using nested JUnit5 classes #4393

acoburn opened this issue Oct 5, 2019 · 10 comments
Assignees
Labels
area/testing kind/bug Something isn't working
Milestone

Comments

@acoburn
Copy link
Contributor

acoburn commented Oct 5, 2019

When using the quarkus-junit5 module (Quarkus version 0.23.2) with the @QuarkusTest annotation, any @Nested JUnit5 classes consistently lead to null pointer exceptions.

The relevant portion of the stack trace is:

org.junit.jupiter.api.extension.TestInstantiationException: TestInstanceFactory [io.quarkus.test.junit.QuarkusTestExtension] failed to instantiate test class [org.acme.quickstart.GreetingResourceTest$NestedClass]: Bean is null
	at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeTestInstanceFactory(ClassBasedTestDescriptor.java:300)
	at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.instantiateTestClass(ClassBasedTestDescriptor.java:275)
	[ ... ]
Caused by: java.lang.NullPointerException: Bean is null
	at java.base/java.util.Objects.requireNonNull(Objects.java:246)
	at io.quarkus.arc.BeanManagerImpl.getReference(BeanManagerImpl.java:54)
	at io.quarkus.test.common.TestInstantiator.instantiateTest(TestInstantiator.java:25)
	at io.quarkus.test.junit.QuarkusTestExtension.createTestInstance(QuarkusTestExtension.java:368)
	at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeTestInstanceFactory(ClassBasedTestDescriptor.java:285)
	... 82 more

I can reproduce this consistently in JDK8 and JDK11, with both Gradle and Maven build environments.

A minimal example of this error can be found at https://github.com/acoburn/quarkus-junit5-test, which is built from the getting-started archetype. The only change to the generated code is that the JUnit @Test is wrapped in a @Nested class.

@acoburn acoburn added the kind/bug Something isn't working label Oct 5, 2019
@geoand
Copy link
Contributor

geoand commented Oct 7, 2019

So after debugging a bit I saw that Arc doesn't support non-static inner classes (see this).
So I'm not sure how @Nested can be supported. @manovotn @mkouba WDYT?

@manovotn
Copy link
Contributor

manovotn commented Oct 7, 2019

@geoand In Junit testing with Quarkus, the test class itself is a bean (see this code).
And CDI spec doesn't like inner classes (non static), see https://docs.jboss.org/cdi/spec/2.0/cdi-spec.html#what_classes_are_beans

@manovotn
Copy link
Contributor

manovotn commented Oct 7, 2019

Although I agree that we should remedy the error and make it more readable.
We could probably detect this in QuarkusTestExtension#createTestInstance() and throw and exception?

@geoand
Copy link
Contributor

geoand commented Oct 7, 2019

@geoand In Junit testing with Quarkus, the test class itself is a bean (see this code).

Yup, that's why I looked at Arc in the first place :)

We could probably detect this in QuarkusTestExtension#createTestInstance() and throw and exception?

Sounds reasonable to me

@manovotn
Copy link
Contributor

manovotn commented Oct 7, 2019

Ok, in that case, I'll send a PR for that later today.

@manovotn manovotn self-assigned this Oct 7, 2019
@geoand
Copy link
Contributor

geoand commented Oct 7, 2019

Cool, thanks!

@manovotn
Copy link
Contributor

manovotn commented Oct 7, 2019

@acoburn BTW if you really need it to be @Nested for some reason, then you can make it static and that will work too.

@geoand
Copy link
Contributor

geoand commented Oct 7, 2019

I think that @Nested in JUnit 5 is not supposed to be static.

But of course the question of the usefulness of @Nested is still valid. I personally haven't encountered it.

@manovotn
Copy link
Contributor

manovotn commented Oct 7, 2019

Ah, you're right, just spotted that in their docs - https://junit.org/junit5/docs/current/user-guide/#writing-tests-nested

But of course the question of the usefulness of @nested is still valid. I personally haven't encountered it.

Yeah, I've got similar experience.

@acoburn
Copy link
Contributor Author

acoburn commented Oct 12, 2019

@manovotn thanks for fixing this so quickly. While it would be nice to use @Nested classes at some future point, I can work around that. The NPE issue described here has been fixed, so I am going to close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/testing kind/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants