Solved - JUnit Mockito: 比较 @Inject @InjectMocks @MockBean @Mock

ref: https://howtodoinjava.com/mockito/mockito-mock-injectmocks/

1. initMocks vs. @SpringBootTest ( 不想看这些背景介绍的可以跳过直接看2.)

1.1 在@Before中用initMocks

源码注释:Initializes object annotated with Mockito annotations for given testClass

1.1.1 先看看init mocks的多种方法:

https://examples.javacodegeeks.com/core-java/mockito/mockito-initmocks-example/

1.1.1.1 用Mocikito.mock():
  • 传入的参数可以是interface或者class
LinkedList mocklinkedList = Mockito.mock(LinkedList.class);
  • 当某个action被触发,返回需要的东西:
Mockito.when(mocklinkedList.get(0)).thenReturn("First Value");
  • 验证
Assert.assertEquals("First Value", mocklinkedList.get(0));
Mockito.verify(mocklinkedList).get(0); // verify the invocation
1.1.1.2 用initMocks()
MockitoAnnotations.initMocks(this);

“This method is useful when you have a lot of mocks to inject. It minimizes repetitive mock creation code, makes the test class more readable and makes the verification error easier to read because the field name is used to identify the mock.”
发现:不initmocks,mock(RequestNorPermitted.class)并没有mock,而是返回了真实的exception....不对,到现在都不知道改了啥,突然mock的就不行了

1.1.1.2.1 Inject Mocks

主要参考了https://site.mockito.org/javadoc/current/org/mockito/InjectMocks.html

@InjectMocks
Mockito will try to inject mocks only either by constructor injection, setter injection, or property injection in order and as described below. If any of the following strategy fail, then Mockito won’t report failure; i.e. you will have to provide dependencies yourself.

下面的可结合1.1.1.2.2的例子理解:

  1. Constructor Injection
    需要被Inject的Object,其构造函数所需要的参数需要在test中被声明。如果没有被声明,则要注入的Object为null。

原文:the biggest constructor is chosen, then arguments are resolved with mocks declared in the test only. If the object is successfully created with the constructor, then Mockito won’t try the other strategies.
Note: If arguments can not be found, then null is passed. If non-mockable types are wanted, then constructor injection won’t happen. In these cases, you will have to satisfy dependencies yourself.

  1. Property setter Injection
    优先级:type > name
    如果同一type的properties有很多,那最好给它们都起个名字

原文:mocks will first be resolved by type (if a single type match injection will happen regardless of the name), then, if there is several property of the same type, by the match of the property name and the mock name.
Note 1: If you have properties with the same type (or same erasure), it’s better to name all @Mock annotated fields with the matching properties, otherwise Mockito might get confused and injection won’t happen.
Note 2: If @InjectMocks instance wasn’t initialized before and have a no-arg constructor, then it will be initialized with this constructor.

  1. Field Injection
    优先级:type > name
    如果同一type的fields有很多,那最好给它们都起个名字

原文:Field injection; mocks will first be resolved by type (if a single type match injection will happen regardless of the name), then, if there is several property of the same type, by the match of the field name and the mock name.
Note 1: If you have fields with the same type (or same erasure), it’s better to name all @Mock annotated fields with th

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值