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

JUnit: Wrong test method passed in TestInfo parameter of BeforeEach/AfterEach methods #19747

Closed
jonathan-meier opened this issue Aug 28, 2021 · 6 comments · Fixed by #19751
Closed
Assignees
Labels
area/testing kind/bug Something isn't working
Milestone

Comments

@jonathan-meier
Copy link
Contributor

jonathan-meier commented Aug 28, 2021

Describe the bug

Consider the following test class:

package com.esri.karl.api;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
public final class TestInfoTest {

	@BeforeEach
	void before(TestInfo testInfo) {
		testInfo.getTestMethod().ifPresent(m -> System.out.println(m.getName())); // prints "before", should print "testMethod"
	}

	@AfterEach
	void after(TestInfo testInfo) {
		testInfo.getTestMethod().ifPresent(m -> System.out.println(m.getName())); // prints "after", should print "testMethod"
	}

	@Test
	void testMethod() {}

}

Expected behavior

The currently running test method annotated with @Test (testMethod in the test class above) should be passed via testInfo in before and after.

Actual behavior

The methods before and after are passed in respectively, which is wrong.

How to Reproduce?

Repro: Just run the test class provided above in Quarkus 2.2.0.Final.

Additional information

Quarkus version 2.1.4.Final works as expected. This is likely a regression introduced with PR #19518.

Likely also related: the class of the passed test method has a different class loader than the class of the currently executing before/after method.

package com.esri.karl.api;

import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.platform.commons.util.AnnotationUtils;

import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
public final class TestInfoTest {

	@Target(ElementType.METHOD)
	@Retention(RetentionPolicy.RUNTIME)
	public @interface TestAnnotation { }

	@BeforeEach
	void before(TestInfo testInfo) throws ClassNotFoundException {
		System.out.println(AnnotationUtils.isAnnotated(testInfo.getTestMethod(), TestAnnotation.class)); // prints "false", should print "true"
		System.out.println(AnnotationUtils.isAnnotated(testInfo.getTestMethod(),
				(Class<? extends Annotation>) testInfo.getTestMethod().get().getDeclaringClass().getClassLoader().loadClass(TestAnnotation.class.getName()))); // prints "true"
	}

	@AfterEach
	void after(TestInfo testInfo) throws ClassNotFoundException {
		System.out.println(AnnotationUtils.isAnnotated(testInfo.getTestMethod(), TestAnnotation.class)); // prints "false", should print "true"
		System.out.println(AnnotationUtils.isAnnotated(testInfo.getTestMethod(),
				(Class<? extends Annotation>) testInfo.getTestMethod().get().getDeclaringClass().getClassLoader().loadClass(TestAnnotation.class.getName()))); // prints "true"
	}

	@Test
	@TestAnnotation
	void testAnnotation() {}

}
@jonathan-meier jonathan-meier added the kind/bug Something isn't working label Aug 28, 2021
@jonathan-meier
Copy link
Contributor Author

/cc @stuartwdouglas

@quarkus-bot
Copy link

quarkus-bot bot commented Aug 28, 2021

/cc @geoand

@famod
Copy link
Member

famod commented Aug 28, 2021

I have a fix for this, will send a PR soon.

@famod
Copy link
Member

famod commented Aug 28, 2021

#19751 should take care of it. Thanks for reporting, btw!

@quarkus-bot quarkus-bot bot added this to the 2.3 - main milestone Aug 30, 2021
@gsmet gsmet modified the milestones: 2.3 - main, 2.2.1.Final Aug 30, 2021
gsmet pushed a commit to gsmet/quarkus that referenced this issue Aug 30, 2021
@jonathan-meier
Copy link
Contributor Author

@famod Thanks a lot for providing a fix so quickly. Our JUnit tests are all green again with version 2.2.1.Final 👍

@famod
Copy link
Member

famod commented Sep 1, 2021

@jonathan-meier great to hear! Just beware that there is a regression w.r.t. @Before/AfterAll: #19800
That will be fixed in 2.2.2.Final.

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

Successfully merging a pull request may close this issue.

3 participants