Skip to content

JUnit view shows wrong results for repeated test case #2692

@trancexpress

Description

@trancexpress

To reproduce, create a Java project and add JUnit 6 to the projects libraries.

Then add test.TestCase and test.RepeatedClass with the sources below and run test.TestCase.

package test;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import org.junit.jupiter.api.ClassTemplate;
import org.junit.jupiter.api.extension.ClassTemplateInvocationContext;
import org.junit.jupiter.api.extension.ClassTemplateInvocationContextProvider;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.platform.commons.support.AnnotationSupport;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ClassTemplate
@ExtendWith(RepeatedClass.Extension.class)
public @interface RepeatedClass {

	int value();

	class Extension implements ClassTemplateInvocationContextProvider {

		@Override
		public boolean supportsClassTemplate(ExtensionContext context) {
			return AnnotationSupport.isAnnotated(context.getElement(), RepeatedClass.class);
		}

		@Override
		public Stream<? extends ClassTemplateInvocationContext> provideClassTemplateInvocationContexts(
				ExtensionContext context) {
			int totalRepetitions = AnnotationSupport.findAnnotation(context.getElement(), RepeatedClass.class)
					.orElseThrow()
					.value();
			return IntStream.rangeClosed(1, totalRepetitions)
					.mapToObj(repetition -> new ClassTemplateInvocationContext() {
						@Override
						public String getDisplayName(int invocationIndex) {
							return "repetition %d of %d".formatted(repetition, totalRepetitions);
						}
					});
		}
	}
}

(see: junit-team/junit-framework#4608)

package test;

import org.junit.jupiter.api.Test;

@RepeatedClass(2)
public class TestCase {

	@Test
	public void test1() {
		System.out.println("TestCase.test1()");
	}
}

Result is:

Image

Expected is:

Image

Reverting the commit from #1217 fixes the problem:

diff --git a/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/model/TestSuiteElement.java b/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/model/TestSuiteElement.java
index bcf24b2771..ec8e7907a5 100644
--- a/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/model/TestSuiteElement.java
+++ b/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/model/TestSuiteElement.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2024 IBM Corporation and others.
+ * Copyright (c) 2000, 2017 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -33,10 +33,6 @@ public class TestSuiteElement extends TestElement implements ITestSuiteElement {
 
        @Override
        public Result getTestResult(boolean includeChildren) {
-               TestCaseElement child= getSingleDynamicChild();
-               if (child != null) {
-                       return child.getStatus().convertToResult();
-               }
                if (includeChildren) {
                        return getStatus().convertToResult();
                } else {
@@ -51,12 +47,7 @@ public class TestSuiteElement extends TestElement implements ITestSuiteElement {
 
        @Override
        public ITestElement[] getChildren() {
-               TestElement[] elements= fChildren.toArray(new TestElement[fChildren.size()]);
-               if (elements.length != 1 || !isSingleDynamicTest(elements[0])) {
-                       return elements;
-               }
-               // Filter out if this is a single dynamic test inside a testsuite
-               return new ITestElement[0];
+               return fChildren.toArray(new ITestElement[fChildren.size()]);
        }
 
        public void addChild(TestElement child) {
@@ -163,84 +154,4 @@ public class TestSuiteElement extends TestElement implements ITestSuiteElement {
                return "TestSuite: " + getTestName() + " : " + super.toString() + " (" + fChildren.size() + ")";   //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
        }
 
-       private boolean isSingleDynamicTest(TestElement element) {
-               if (element instanceof TestCaseElement testCase) {
-                       if (testCase.isDynamicTest() && fChildren.size() == 1) {
-                               return true;
-                       }
-               }
-               return false;
-       }
-
-       /**
-        * If this test suite is a {@code @TestTemplate} test case with a single child, return that child.
-        * @return The single dynamic test case child or {@code null} if the suite has no children or multiple or non-dynamid children.
-        */
-       public TestCaseElement getSingleDynamicChild() {
-               try {
-                       if (fChildren.size() == 1) {
-                               TestElement child= fChildren.get(0);
-                               if (isSingleDynamicTest(child)) {
-                                       return (TestCaseElement) child;
-                               }
-                       }
-               } catch (IndexOutOfBoundsException e) {
-                       // don't care, children changed concurrently
-               }
-               return null;
-       }
-
-       @Override
-       public boolean isComparisonFailure() {
-               TestCaseElement child= getSingleDynamicChild();
-               if (child == null) {
-                       return super.isComparisonFailure();
-               }
-               return child.isComparisonFailure();
-       }
-
-       @Override
-       public boolean isAssumptionFailure() {
-               TestCaseElement child= getSingleDynamicChild();
-               if (child == null) {
-                       return super.isAssumptionFailure();
-               }
-               return child.isAssumptionFailure();
-       }
-
-       @Override
-       public FailureTrace getFailureTrace() {
-               TestCaseElement child= getSingleDynamicChild();
-               if (child == null) {
-                       return super.getFailureTrace();
-               }
-               return child.getFailureTrace();
-       }
-
-       @Override
-       public String getTrace() {
-               TestCaseElement child= getSingleDynamicChild();
-               if (child != null) {
-                       return child.getTrace();
-               }
-               return super.getTrace();
-       }
-
-       @Override
-       public String getExpected() {
-               TestCaseElement child= getSingleDynamicChild();
-               if (child == null) {
-                       return super.getExpected();
-               }
-               return child.getExpected();
-       }
-
-       @Override
-       public String getActual() {
-               TestCaseElement child= getSingleDynamicChild();
-               if (child == null) {
-                       return super.getActual();
-               }
-               return child.getActual();
-       }
 }
diff --git a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestViewer.java b/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestViewer.java
index 4c4c49debe..2305e99fd4 100644
--- a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestViewer.java
+++ b/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/TestViewer.java
@@ -452,14 +452,6 @@ public class TestViewer {
                        // a group of parameterized tests
                        return new OpenTestAction(fTestRunnerPart, tce, tce.getParameterTypes());
                }
-               if (children.length == 0) {
-                       // check if we have applied the workaround for: https://github.com/eclipse-jdt/eclipse.jdt.ui/issues/945
-                       TestCaseElement child= testSuite.getSingleDynamicChild();
-                       if (child != null) {
-                               // a parameterized test that ran only one test
-                               return new OpenTestAction(fTestRunnerPart, child, child.getParameterTypes());
-                       }
-               }
 
                int index= testName.indexOf('(');
                // test factory method

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingregressionRegression defect

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions