这里以 AlphaService、BetaService 为例,将 BetaService 作为单元测试的目标类。
publicclassEatInfo{privateString food;privateString address;// setter/getter ...}@ServicepublicclassAlphaService{publicvoidinit(){System.out.println("init...");}publicvoidsay(String content){System.out.println(content);}publicvoidrun(){System.out.println("run...");}publicvoideat(EatInfo eatInfo){System.out.println("go to"+ eatInfo.getAddress()+" to eat "+ eatInfo.getFood());}}@ServicepublicclassBetaService{@AutowiredprivateAlphaService alphaService;publicvoidsayHello(){
alphaService.init();
alphaService.say("hello");EatInfo eatInfo =newEatInfo();
eatInfo.setFood("饺子");
eatInfo.setAddress("你家");
alphaService.eat(eatInfo);}}@RunWith(MockitoJUnitRunner.class)publicclassBetaServiceTest{@InjectMocksprivateBetaService betaService;@MockprivateAlphaService alphaService;@TestpublicvoidsayHello1(){...
betaService.sayHello();...}}
各场景下 verify 的使用:
- 校验 BetaService.sayHello 中是否调用了 AlphaService.init
@RunWith(MockitoJUnitRunner.class)publicclassBetaServiceTest{@InjectMocksprivateBetaService betaService;@MockprivateAlphaService alphaService;@TestpublicvoidsayHello1(){... betaService.sayHello();...// 校验 betaService.sayHello() 是否调用了 AlphaService.initverify(alphaService).init();}}
- 校验 BetaService.sayHello 中调用了 AlphaService.init 的次数
@RunWith(MockitoJUnitRunner.class)publicclassBetaServiceTest{@InjectMocksprivateBetaService betaService;@MockprivateAlphaService alphaService;@TestpublicvoidsayHello1(){... betaService.sayHello();...// 校验 betaService.sayHello() 是否调用了 AlphaService.init 1 次verify(alphaService,times(1)).init();}}
其实,这里 verify(alphaService, times(1)).init() 等同于 **verify(alphaService).init()**。让我们来看下 verify(…) 的源代码实现就知道了,如下图: - 校验 BetaService.sayHello 中没有调用过 AlphaService.run
@RunWith(MockitoJUnitRunner.class)publicclassBetaServiceTest{@InjectMocksprivateBetaService betaService;@MockprivateAlphaService alphaService;@TestpublicvoidsayHello1(){... betaService.sayHello();...// 校验 betaService.sayHello() 没有调用过 AlphaService.run verify(alphaService,times(0)).run();}}
或@RunWith(MockitoJUnitRunner.class)publicclassBetaServiceTest{@InjectMocksprivateBetaService betaService;@MockprivateAlphaService alphaService;@TestpublicvoidsayHello1(){... betaService.sayHello();...// 校验 betaService.sayHello() 没有调用过 AlphaService.run verify(alphaService,never()).run();}}
- 校验 BetaService.sayHello 中的 AlphaService.run 方法 最少 or 最多 调用了多少次
@RunWith(MockitoJUnitRunner.class)publicclassBetaServiceTest{@InjectMocksprivateBetaService betaService;@MockprivateAlphaService alphaService;@TestpublicvoidsayHello1(){... betaService.sayHello();...// 校验 betaService.sayHello() 中 AlphaService.run 方法最少被调用过 1 次verify(alphaService,atLeast(1)).run();// 校验 betaService.sayHello() 中 AlphaService.run 方法最多被调用过 5 次verify(alphaService,atMost(5)).run();}}
- 校验 BetaService.sayHello 中的 AlphaService.say 方法被调用的时候,是否使用了特定的参数固定参数值:
@RunWith(MockitoJUnitRunner.class)publicclassBetaServiceTest{@InjectMocksprivateBetaService betaService;@MockprivateAlphaService alphaService;@TestpublicvoidsayHello1(){... betaService.sayHello();...// 校验 betaService.sayHello() 调用 AlphaService.say 的时候,参数值是 "hello" verify(alphaService).say("hello");}}
任何参数值:@RunWith(MockitoJUnitRunner.class)publicclassBetaServiceTest{@InjectMocksprivateBetaService betaService;@MockprivateAlphaService alphaService;@TestpublicvoidsayHello1(){... betaService.sayHello();...// 校验 betaService.sayHello() 调用 AlphaService.say 的时候,参数值是任何字符串verify(alphaService).say(anyString());}}
- 校验 BetaService.sayHello 中的 AlphaService.eat 方法被调用的时候,作为参数的 EatInfo 对象中属性 food 值为 “饺子”, 属性 address 值为 “你家”
@RunWith(MockitoJUnitRunner.class)publicclassBetaServiceTest{@InjectMocksprivateBetaService betaService;@MockprivateAlphaService alphaService;@TestpublicvoidsayHello1(){... betaService.sayHello();...// 校验 betaService.sayHello() 调用 AlphaService.eat 的时候,作为参数的 EatInfo 对象中属性 food 值为 "饺子", 属性 address 值为 "你家"ArgumentCaptor<AlphaService> alphaServiceAC =ArgumentCaptor.forClass(AlphaService.class);verify(alphaService).eat(alphaServiceAC.capture());assertThat(alphaServiceAC.getValue().getFood(),allOf(notNullValue(),is("饺子")));assertThat(alphaServiceAC.getValue().getAddress(),allOf(notNullValue(),is("你家")));}}
**这里用到了「参数捕获器」 ArgumentCaptor ** - 校验在 BetaService.sayHello 中方法的调用顺序是 alphaService.init、alphaService.say、alphaService.eat```@RunWith(MockitoJUnitRunner.class)publicclassBetaServiceTest{@InjectMocksprivateBetaService betaService;@MockprivateAlphaService alphaService;@TestpublicvoidsayHello1(){... betaService.sayHello();...// 校验 betaService.sayHello() 中方法的调用顺序是 alphaService.init、alphaService.say、alphaService.eatInOrder alphaServiceIO =inOrder(alphaService); alphaServiceIO.verify(alphaService).init(); alphaServiceIO.verify(alphaService).say(anyString()); alphaServiceIO.verify(alphaService).eat(any(EatInfo.class));}} ``````
本文转载自: https://blog.csdn.net/yangchao1125/article/details/126381423
版权归原作者 cab5 所有, 如有侵权,请联系我们删除。
版权归原作者 cab5 所有, 如有侵权,请联系我们删除。