Skip to content

InjectSpy disables all observers in JUnit5 tests too #49872

@wglanzer

Description

@wglanzer

Describe the bug

When using @InjectSpy in a @QuarkusTest, all observer methods declared on the spied bean are not invoked anymore.
This seems identical to the intended behavior of @InjectMock (disabling observers), but it is unexpected for spies, because a spy is supposed to wrap the real bean.

The original PR, that lead to this behavior, should be: #11122

Expected behavior

Observer methods of a bean annotated with @InjectSpy should still be registered and invoked when firing corresponding CDI events.

Actual behavior

Observer methods are not called at all when the bean is spied.

How to Reproduce?

import io.quarkus.test.InjectSpy;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Event;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;

@QuarkusTest
public class MinimalReproducer
{
  @InjectSpy // works with regular @Inject
  DemoObserver observer;

  @Inject
  Event<ObserverEvent> event;

  @Test
  void shouldFireEvent()
  {
    event.fire(new ObserverEvent());
  }

  public record ObserverEvent() {}

  @ApplicationScoped
  public static class DemoObserver
  {
    void onAudit(@Observes ObserverEvent evt)
    {
      System.out.println(evt); // never gets called
    }
  }
}

Output of uname -a or ver

Darwin wglanzer-mb.home.lan 24.6.0 Darwin Kernel Version 24.6.0: Mon Jul 14 11:28:17 PDT 2025; root:xnu-11417.140.69~1/RELEASE_X86_64 x86_64

Output of java -version

java 21.0.5 2024-10-15 LTS Java(TM) SE Runtime Environment Oracle GraalVM 21.0.5+9.1 (build 21.0.5+9-LTS-jvmci-23.1-b48) Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 21.0.5+9.1 (build 21.0.5+9-LTS-jvmci-23.1-b48, mixed mode, sharing)

Quarkus version or git rev

3.25.0

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.9.9 (8e8579a9e76f7d015ee5ec7bfcdc97d260186937)

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions