简体   繁体   English

@MockBeans示例使用

[英]@MockBeans example use

I have a controller class that makes use of several services. 我有一个使用多个服务的控制器类。 I write a test for that controller like: 我为该控制器编写了一个测试,例如:

@RunWith(SpringRunner.class)
@WebMvcTest(value = PurchaseController.class, secure = false)
public class PurchaseControllerTest {

    @MockBean
    private ShoppingService shoppingService;

    @MockBean
    private ShopRepository shopRepository;

    @MockBean
    private SomeOtherRepository someOtherRepository;

    @Autowired
    private MockMvc mockMvc;

// ... some tests goes here

Thing is, that there tend to be many of those mocks, thus many lines of code. 问题是,往往会有许多这样的模拟,因此会有很多代码行。 I know this may be a sign of a code smell, but that's not my point now. 我知道这可能是代码异味的迹象,但这不是我现在的意思。

I noticed that there is also a @MockBeans annotation that has @Target(ElementType.TYPE) . 我注意到,还有一个@MockBeans批注具有@Target(ElementType.TYPE) So I thought I could try: 所以我想我可以尝试:

@RunWith(SpringRunner.class)
@WebMvcTest(value = PurchaseController.class, secure = false)
@MockBeans(value = {ShoppingService.class, ShopRepository.class})
public class PurchaseControllerTest {

but it doesn't event compile. 但是它不会进行事件编译。

My question is: how we could use @MockBeans annotation? 我的问题是:我们如何使用@MockBeans注释? Is it applicable to my case? 它适用于我的情况吗?

@MockBeans it's just a repeatable annotation for multiply of @MockBean s. @MockBeans只是@MockBean乘以的可重复注释。 If you need to reuse this mocked bean you can put in on a class / config class. 如果您需要重用这个模拟的bean,则可以放入一个class / config类。 But you need to use @Autowired for services that you what to mock . 但是您需要使用@Autowired提供模拟服务。 So in your case it should be : 因此,在您的情况下应该是:

.....
@MockBeans({@MockBean(ShoppingService.class), MockBean(ShopRepository.class)})
public class PurchaseControllerTest {
  @Autowired
  ShoppingService shoppingService;
  @Autowired
  ShopRepository shopRepository;
.....
}

Main idea of @MockBeans it's just repeating @MockBean in one place. @MockBeans的主要思想是在一个地方重复@MockBean For me it might be useful only for some configuration / common class that you can reuse. 对我而言,它仅对某些可重用的配置/通用类有用。

@MockBean - create a mock , @Autowired - is autowired bean from context , in your case ,it mark/create bean as mocked then mocked bean will be injected in your autowired field. @MockBean创建一个模拟,@ @Autowired是从上下文自动装配的bean,在您的情况下,将标记/创建bean标记为模拟,然后@Autowired模拟的bean注入到您的autowired字段中。

So if you have a lot of autowired fields with @MockBeans (or multiply @MockBean ) you can configure is it a mock or not in one place (in @MockBeans for class level) and you don't need change @Autowired to @Mock in you test class (like in you case if you remove @MockBeans all autowired beans that are not mocked will be autowired as beans from context , and if you undo removing you will work in mocked beans (that you configured inside this annotation) ) . 所以,如果你有很多领域自动装配与@MockBeans (或乘@MockBean )可以配置它是在一个地方或模拟不(在@MockBeans类级别),你不需要改变@Autowired@Mock在您的测试类中(例如,如果您删除@MockBeans所有未@MockBeans自动装配的bean都将自动关联为上下文中的bean,如果您撤消删除,则将在模拟的bean中工作(在此注释中配置))。

If you want to avoid a lot of dependency inside one class you can extract all dependency into some parent class , but as java doesn't support multiply inheritance for class it's not always might help. 如果要避免在一个类中出现很多依赖关系,可以将所有依赖关系提取到某个父类中,但是由于Java不支持该类的多重继承,因此不一定总是有帮助。

Javadoc says it's used as Javadoc说它被用作

Container annotation that aggregates several {@link MockBean} annotations. 容器注释聚合了几个{@link MockBean}注释。

So you can write 所以你可以写

@MockBeans({@MockBean(ShoppingService.class), @MockBean(ShopRepository.class)})
public class PurchaseControllerTest {

@Autowire ShoppingService works here @Autowire ShoppingService在这里工作

Or 要么

Can also be used in conjunction with Java 8's support for repeatable annotations 也可以与Java 8对可重复注释的支持结合使用

@MockBean(ShoppingService.class)
@MockBean(ShopRepository.class)
public class PurchaseControllerTest {

Java 8 enables repeatable annotations , and for compatibility reasons, repeating annotations are stored in a container annotation @MockBeans that is automatically generated by the Java compiler. Java 8启用了可重复注释 ,并且出于兼容性原因,重复注释存储在容器注释@MockBeans ,该容器注释由Java编译器自动生成。 In order for the compiler to do this, you need 2 things: 为了使编译器执行此操作,您需要做两件事:

  • Repeatable @Repeatable(MockBeans.class) Annotation Type @MockBean 可重复@Repeatable(MockBeans.class)注释类型@MockBean
  • Containing Annotation Type @MockBeans 包含注释类型@MockBeans

您所用的最短变体是@MockBean ,它支持所需的@MockBean类的多个值

@MockBean({ShoppingService.class, ShopRepository.class})

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

相关问题 Spring Boot @MockBeans - 如何在多个测试类中使用相同的 bean - Spring Boot @MockBeans - How to use the same beans in multiple test classes Spring-Boot-Test @MockBeans是否应该符合@ConditionalOnBean条件? - Are Spring-Boot-Test @MockBeans supposed to qualify for @ConditionalOnBean conditions? 如何在没有继承的情况下在我的测试中共享@MockBeans 和模拟方法? - How to share @MockBeans and mock methods among my tests without inheritance? 为什么我的 MockBeans 和 MockRestServiceServer 在 Spring Boot 中测试 JMS 侦听器时没有返回正确的响应 - Why are my MockBeans and MockRestServiceServer not returning proper responses when testing JMS Listener in Spring Boot 当使用自动接线时,将是示例的受益者 - When autowiring use would be beneficiary with example 使用JSF和Spring架构的好例子 - Good example of use of JSF and Spring Architecture 如何在Spring-Jquery应用程序中使用ColorBox示例? - How to Use ColorBox Example in Spring-Jquery Application? 有人会提供一个如何使用和编程Spring PathMatchConfigurer的示例吗? - Would somebody have an example of how-to use and program a Spring PathMatchConfigurer? 如何设置属性排序,当我使用Jpa Example.of - How to set property sort ,When i use Jpa Example.of Spring Data REST示例,它不使用root进行映射 - Spring Data REST example that doesn't use root for mapping
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM