【问题标题】:Is a mocked bean (@MockBean) mocked before the Spring Context loads the actual Spring Bean?在 Spring Context 加载实际的 Spring Bean 之前是否模拟了一个模拟 bean (@MockBean)?
【发布时间】:2018-05-28 18:08:57
【问题描述】:

我们以下面的例子为例。

@Autowired
@MockBean
private Foo foobar;

Spring Context 是否先加载类Foo,然后再应用mock?或者@Mockbean 是否以某种方式被检测到,Spring 创建并应用模拟而不是将Foo 类加载到 Spring 上下文中。我怀疑是后者,但我想确认一下。

【问题讨论】:

    标签: spring spring-boot spring-test


    【解决方案1】:

    Spring 会抛出异常。

    让我们定义类 Foo。

    @Component
    public class Foo {
        public Foo() {
            System.out.println("I am not a mock");
        }
    }
    

    每当使用@Autowired 进行测试时,spring 都会注入一个 Foo 实例,并且构造函数将打印"I am not a mock",如下面的代码所示。

    @SpringBootTest(classes = Main.class)
    @RunWith(SpringRunner.class)
    public class FooTest {
    
        @Autowired
        Foo foo;
    
        @Test
        public void test() {
            System.out.println(foo);
        }
    }
    

    另一方面,使用@MockBean,spring不会创建真正的bean,构造函数中的消息也不会被打印出来。该场景由以下代码表示。

    @SpringBootTest(classes = Main.class)
    @RunWith(SpringRunner.class)
    public class FooTest {
    
        @MockBean
        Foo foo;
        @Test
        public void test() {
            System.out.println(foo);
        }
    }
    

    然而,当你尝试同时使用这两个注解时,spring 会抛出一个由IllegalStateException 引起的BeanCreationException。这意味着字段 foo 不能具有现有值。在执行下面的代码时会出现这种情况:

    @SpringBootTest(classes = Main.class)
    @RunWith(SpringRunner.class)
    public class FooTest {
       // this will not work
        @Autowired
        @MockBean
        Foo foo;
    
        @Test
        public void test() {
            System.out.println(foo);
        }
    }
    

    堆栈跟踪将类似于:

    org.springframework.beans.factory.BeanCreationException: Could not inject field: com.tbp.Foo com.FooTest.foo; nested exception is java.lang.IllegalStateException: The field com.tbp.Foo com.FooTest.foo cannot have an existing value
        at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.inject(MockitoPostProcessor.java:413) ~[spring-boot-test-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    

    【讨论】:

      猜你喜欢
      • 2019-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多