简体   繁体   English

如何模拟Spring bean及其自动装配的ctor参数?

[英]How to mock a spring bean and also its autowired ctor arguments?

I want to mock an object and one of its autowired constructor dependencies. 我想模拟一个对象及其自动装配的构造函数依赖项之一。

Eg: 例如:

class MyTest {
  @MockBean A a;
  @Test myTest() {
    assertTrue(when(a.some()).thenCallRealMethod());
  }
}
@Component
class A {
  private B b;
  A(B dependency) {
    this.b = dependency;
  }
  boolean some() {
    return b.value();
  }
}
@Configuration
class B {
  boolean value() { return true; }
}

This will throw a NPE when a::some is called in MyTest::myTest . 当在MyTest::myTest调用a::some时,这将引发NPE。 How can I mock the mocks dependencies? 如何模拟模拟依赖项?

First, you have to choose the runner, 首先,您必须选择跑步者

SpringRunner or Mockito Runner SpringRunner或Mockito Runner

In this case I chose SpringRunner: Docs : https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/context/junit4/SpringRunner.html 在这种情况下,我选择了SpringRunner:Docs: https ://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/context/junit4/SpringRunner.html

Then you created a MockBean of A and you have to define the mock behaviour 然后创建一个A的MockBean,并且必须定义模拟行为

when(a.some()).thenReturn(true);

@RunWith(SpringRunner.class)
    public class MyTest {

         @MockBean A a;

         @Test
         public void  myTest() {
            when(a.some()).thenReturn(true);
            assertTrue(a.some());

        }
        @Component
        class A {
          private B b;
          A(B dependency) {
            this.b = dependency;
          }
          boolean some() {
            return b.value();
          }
        }
        @Configuration
        class B {
          boolean value() { return true; }
        }

    }

Testing the Real method using @SpyBean: 使用@SpyBean测试Real方法:

@RunWith(SpringRunner.class)
@SpringBootTest(classes={MyTest.class,MyTest.B.class,MyTest.A.class})
public class MyTest {

     @SpyBean A a;

     @Test
     public  void  myTest() {

       assertTrue(a.some());

    }
    @Component
    class A {
      private B b;
      A(B dependency) {
        this.b = dependency;
      }
      boolean some() {
        return b.value();
      }
    }
    @Configuration
    class B {


      boolean value() { return true; }
    }

}

Spying on A and mocking B would be like below : 监视A并嘲笑B如下所示:

@RunWith(SpringRunner.class)
@SpringBootTest(classes={MyTest.class,MyTest.B.class,MyTest.A.class})
public class MyTest {

     @SpyBean A a;

     @MockBean B b;

     @Test
     public  void  myTest() {

         when(b.value()).thenReturn(false);
       assertTrue(a.some());

    }
    @Component
    class A {
      private B b;
      A(B dependency) {
        this.b = dependency;
      }
      boolean some() {
        return b.value();
      }
    }
    @Configuration
    class B {


      boolean value() { return true; }
    }

}

Result : Assertion failure as mocked behaviour of B is false. 结果 :断言失败,因为B的模拟行为为假。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM