Skip to content

@InjectTest does not rewire static fields or instance fields for each Test Class resp. Test Method #858

Open
@spfeiffer-iem

Description

@spfeiffer-iem

Minimal reproducer (tested using avaje-inject 11.5 and 11.6.-RC5):

package main;

import io.avaje.inject.BeanScope;
import jakarta.inject.Singleton;

public class Main {

  @Singleton
  public static class SingletonGreeter {

    public String greet() {
      return "Hello from SingletonGreeter";
    }
  }

  public static void main(String[] args) {
    try(BeanScope beanScope = BeanScope.builder().build()) {
      SingletonGreeter singletonGreeter = beanScope.get(SingletonGreeter.class);
      System.out.println(singletonGreeter.greet());
    }
  }
}

Test using @InjectTest:

package main;

import main.Main.SingletonGreeter;
import io.avaje.inject.test.InjectTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;

@InjectTest
class MainTest {
  @Inject SingletonGreeter sg;

  @Test
  void one() {
    System.out.println(sg.toString());
  }

  @Test
  void two() {
    System.out.println(sg.toString());
  }
}
Jul 14, 2025 1:38:13 PM io.avaje.inject.spi.DBeanScope start
INFO: Wired beans in 11ms
main.Main$SingletonGreeter@3541cb24
main.Main$SingletonGreeter@3541cb24

Both test methods return the same instance of SingletonGreeter. I expect the SingletonGreeter to be rewired between the two test methods calls, returning different instances in each test method.

Programmatic Approach works:

package main;

import main.Main.SingletonGreeter;
import io.avaje.inject.BeanScope;
import io.avaje.inject.test.TestBeanScope;
import jakarta.inject.Inject;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class MainProgrammaticalTest {
  @Inject SingletonGreeter sg;
  BeanScope instanceScope;

  @BeforeEach
  void beforeEach() {
    instanceScope = TestBeanScope.builder()
        .forTesting()
        .build();
    sg = instanceScope.get(SingletonGreeter.class);
  }

  @AfterEach
  void afterEach() {
    instanceScope.close();
  }


  @Test
  void one() {
    System.out.println(sg.toString());
  }

  @Test
  void two() {
    System.out.println(sg.toString());
  }
}
Jul 14, 2025 1:53:40 PM io.avaje.inject.spi.DBeanScope start
INFO: Wired beans in 13ms
Jul 14, 2025 1:53:40 PM io.avaje.inject.spi.DBeanScope start
INFO: Wired beans in 2ms
main.Main$SingletonGreeter@672872e1
Jul 14, 2025 1:53:40 PM io.avaje.inject.spi.DBeanScope start
INFO: Wired beans in 2ms
main.Main$SingletonGreeter@f381794

Both test methods return different instances of SingletonGreeter, as expected.

The same is true for static variables annotated with @Inject: They do not get rewired between the test classes, all test classes get the same global instance.

I expected the @InjectTest method to work the same way, as described in https://avaje.io/inject/#test-static .
Am i doing something wrong?
Thank you very much.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions