Class MicronautTestResourceLocksProvider

java.lang.Object
io.micronaut.test.extensions.junit5.MicronautTestResourceLocksProvider
All Implemented Interfaces:
org.junit.jupiter.api.parallel.ResourceLocksProvider

@Internal public final class MicronautTestResourceLocksProvider extends Object implements org.junit.jupiter.api.parallel.ResourceLocksProvider
Serialises everything that shares a single Micronaut test ApplicationContext.

A @MicronautTest class owns one extension instance, one application context and one set of singleton beans (including @MockBeans) for the whole class, and @Nested classes reuse the extension instance of their outermost enclosing class. Running the methods of such a group concurrently therefore races on shared mutable state. This provider hands every method - and every nested container - a lock keyed on the outermost test class, so JUnit still runs different @MicronautTest classes in parallel while keeping one context single-threaded.

Two escape hatches exist for tests that are known to be thread-safe:

  • declare @Execution(CONCURRENT) directly on the test class, which opts that class out;
  • set -Dmicronaut.test.parallel.methods=true, which opts the whole JVM out.

In both cases the test class, its beans and any TestExecutionListener on its classpath become the author's responsibility.

Since:
5.2.0
Author:
Denis Stepanov
  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.junit.jupiter.api.parallel.ResourceLocksProvider

    org.junit.jupiter.api.parallel.ResourceLocksProvider.Lock
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    System property that disables the per-context lock for the whole JVM.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Set<org.junit.jupiter.api.parallel.ResourceLocksProvider.Lock>
    provideForMethod(List<Class<?>> enclosingInstanceTypes, Class<?> testClass, Method testMethod)
     
    Set<org.junit.jupiter.api.parallel.ResourceLocksProvider.Lock>
    provideForNestedClass(List<Class<?>> enclosingInstanceTypes, Class<?> testClass)
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.junit.jupiter.api.parallel.ResourceLocksProvider

    provideForClass
  • Field Details

    • PARALLEL_METHODS_PROPERTY

      public static final String PARALLEL_METHODS_PROPERTY
      System property that disables the per-context lock for the whole JVM.
      See Also:
  • Constructor Details

    • MicronautTestResourceLocksProvider

      public MicronautTestResourceLocksProvider()
  • Method Details

    • provideForNestedClass

      public Set<org.junit.jupiter.api.parallel.ResourceLocksProvider.Lock> provideForNestedClass(List<Class<?>> enclosingInstanceTypes, Class<?> testClass)
      Specified by:
      provideForNestedClass in interface org.junit.jupiter.api.parallel.ResourceLocksProvider
    • provideForMethod

      public Set<org.junit.jupiter.api.parallel.ResourceLocksProvider.Lock> provideForMethod(List<Class<?>> enclosingInstanceTypes, Class<?> testClass, Method testMethod)
      Specified by:
      provideForMethod in interface org.junit.jupiter.api.parallel.ResourceLocksProvider