0

springboot version:2.4.13 spring-boot-starter-test version: 2.4.13 question when use @SpyBean ,it not support circular bean eg:

public class A {
    @Autowired
    private B b;

    public String getName() {
        return A.class.getName();
    }

    public int getAge() {
        return 18;
    }
}
public class B {
    @Autowired
    private A a;

    public String getName() {
        return B.class.getName();
    }
}
public class TestCycle {
    @SpyBean
    private A a;

    @Test
    public void test() {
        Mockito.doReturn("name").when(a).getName();
        Assertions.assertEquals("name", a.getName());
        Assertions.assertEquals(18, a.getAge());
    }
}

exception:

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'a': Bean with name 'a' has been injected into other beans [b] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.

i want support circular bean when use @SpyBean

samabcde
  • 6,988
  • 2
  • 25
  • 41

0 Answers0