Skip to content

Commit 7542d46

Browse files
committed
Allow @ResourceLock on @ClassTemplate classes
Fixes #5155 Signed-off-by: raccoonback <[email protected]>
1 parent 048c482 commit 7542d46

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/ClassTemplateInvocationTestDescriptor.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package org.junit.jupiter.engine.descriptor;
1212

13+
import static java.util.Collections.emptySet;
1314
import static java.util.Objects.requireNonNull;
1415
import static org.apiguardian.api.API.Status.INTERNAL;
1516
import static org.junit.jupiter.engine.descriptor.CallbackSupport.invokeAfterCallbacks;
@@ -36,6 +37,7 @@
3637
import org.junit.jupiter.engine.extension.MutableExtensionRegistry;
3738
import org.junit.platform.engine.TestSource;
3839
import org.junit.platform.engine.UniqueId;
40+
import org.junit.platform.engine.support.hierarchical.ExclusiveResource;
3941
import org.junit.platform.engine.support.hierarchical.ThrowableCollector;
4042

4143
/**
@@ -112,6 +114,12 @@ public Function<ResourceLocksProvider, Set<ResourceLocksProvider.Lock>> getResou
112114

113115
// --- Node ----------------------------------------------------------------
114116

117+
@Override
118+
public Set<ExclusiveResource> getExclusiveResources() {
119+
// Resources are already collected and returned by the enclosing ClassTemplateTestDescriptor
120+
return emptySet();
121+
}
122+
115123
@Override
116124
public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext context) {
117125
MutableExtensionRegistry registry = context.getExtensionRegistry();

jupiter-tests/src/test/java/org/junit/jupiter/engine/ClassTemplateInvocationTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
import org.junit.jupiter.api.extension.ParameterResolutionException;
8080
import org.junit.jupiter.api.extension.ParameterResolver;
8181
import org.junit.jupiter.api.extension.RegisterExtension;
82+
import org.junit.jupiter.api.parallel.ResourceLock;
8283
import org.junit.jupiter.engine.descriptor.ClassTemplateInvocationTestDescriptor;
8384
import org.junit.jupiter.engine.descriptor.ClassTemplateTestDescriptor;
8485
import org.junit.jupiter.engine.descriptor.ClassTestDescriptor;
@@ -1004,6 +1005,14 @@ void ignoresComposedAnnotations() {
10041005
assertThat(engineDescriptor.getDescendants()).isEmpty();
10051006
}
10061007

1008+
@Test
1009+
void classTemplateWithResourceLockExecutesSuccessfully() {
1010+
var results = executeTestsForClass(ClassTemplateWithResourceLockTestCase.class);
1011+
1012+
results.testEvents().assertStatistics(stats -> stats.started(2).succeeded(2));
1013+
results.containerEvents().assertStatistics(stats -> stats.started(4).succeeded(4));
1014+
}
1015+
10071016
// -------------------------------------------------------------------
10081017

10091018
private static Stream<String> allReportEntryValues(EngineExecutionResults results) {
@@ -1567,4 +1576,15 @@ void test() {
15671576
}
15681577
}
15691578

1579+
@SuppressWarnings("JUnitMalformedDeclaration")
1580+
@ClassTemplate
1581+
@ExtendWith(TwoInvocationsClassTemplateInvocationContextProvider.class)
1582+
@ResourceLock("test-resource")
1583+
static class ClassTemplateWithResourceLockTestCase {
1584+
@Test
1585+
void test() {
1586+
// This test verifies that @ResourceLock works with @ClassTemplate (issue #5155)
1587+
}
1588+
}
1589+
15701590
}

0 commit comments

Comments
 (0)