junit-team/junit5 · error · JUnitException

'ResourceLockTarget.CHILDREN' is not supported for methods…

Error message

'ResourceLockTarget.CHILDREN' is not supported for methods. Invalid method: %s

What it means

Thrown as JUnitException when a method-level @ResourceLock annotation uses ResourceLockTarget.CHILDREN. CHILDREN only makes sense on a class (it applies to nested/contained tests), so applying it to a @Test/@TestFactory/@RepeatedTest/etc. method is a misuse that the MethodBasedTestDescriptor rejects at exclusive-resource collection time.

Solutions

  1. Remove target=ResourceLockTarget.CHILDREN from the method annotation - omit it (defaults to CURRENT).
  2. If you really need to lock child resources, move the @ResourceLock to the enclosing @Test class instead.
  3. Switch to a custom @ResourceLocksProvider via provideForMethod rather than annotation-based locks if child scoping is genuinely required.

Example fix

// before
@Test
@ResourceLock(value="db", target=ResourceLockTarget.CHILDREN)
void usesDb() { }
// after
@Test
@ResourceLock(value="db") // target defaults to CURRENT
void usesDb() { }
Defensive patterns

Strategy: validation

Validate before calling

for (Method m : MyTest.class.getDeclaredMethods()) {
    ResourceLock rl = m.getAnnotation(ResourceLock.class);
    if (rl != null && rl.target() == ResourceLockTarget.CHILDREN) {
        throw new IllegalStateException("Method " + m + " uses CHILDREN target illegally");
    }
}

Prevention

When it happens

Trigger: Annotating a method with @ResourceLock(value="...", target=ResourceLockTarget.CHILDREN); ExclusiveResourceCollector.from(getTestMethod()) then finds static resources scoped to CHILDREN and the descriptor throws in getExclusiveResourceCollector().

Common situations: Copy-paste from a class-level @ResourceLock that used CHILDREN, misunderstanding the CHILDREN semantics, or a custom composed annotation that hardcodes CHILDREN.

Related errors


AI-assisted analysis of junit-team/junit5@f070c699a0 (2026-08-11). Data as JSON: /api/errors/e0fd10e071a5cd1c. Report an issue: GitHub.

Appendix: source

Thrown at junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/MethodBasedTestDescriptor.java:133

		Validatable.reportAndClear(this.methodInfo.discoveryIssues, reporter);
		DisplayNameUtils.validateAnnotation(getTestMethod(), //
			() -> "method '%s'".formatted(getTestMethod().toGenericString()), //
			// Use _declaring_ class here because that's where the `@DisplayName` annotation is declared
			() -> MethodSource.from(getTestMethod()), //
			reporter);
	}

	// --- Node ----------------------------------------------------------------

	@Override
	public ExclusiveResourceCollector getExclusiveResourceCollector() {
		// There's no need to cache this as this method should only be called once
		ExclusiveResourceCollector collector = ExclusiveResourceCollector.from(getTestMethod());

		if (collector.getStaticResourcesFor(CHILDREN).findAny().isPresent()) {
			String message = "'ResourceLockTarget.CHILDREN' is not supported for methods." + //
					" Invalid method: " + getTestMethod();
			throw new JUnitException(message);
		}

		return collector;
	}

	@Override
	public Function<ResourceLocksProvider, Set<ResourceLocksProvider.Lock>> getResourceLocksProviderEvaluator() {
		return enclosingInstanceTypesDependentResourceLocksProviderEvaluator(this::getEnclosingTestClasses,
			(provider, enclosingInstanceTypes) -> provider.provideForMethod(enclosingInstanceTypes, getTestClass(),
				getTestMethod()));
	}

	@Override
	protected Optional<ExecutionMode> getExplicitExecutionMode() {
		return getExecutionModeFromAnnotation(getTestMethod());
	}

	/**

View on GitHub (pinned to f070c699a0)