这里以 AlphaService、BetaService 为例,将 BetaService 作为单元测试的目标类。
public class EatInfo {
private String food;
private String address;
// setter/getter ...
}
@Service
public class AlphaService {
public void init() {
System.out.println("init...");
}
public void say(String content) {
System.out.println(content);
}
public void run() {
System.out.println("run...");
}
public void eat(EatInfo eatInfo) {
System.out.println("go to" + eatInfo.getAddress() + " to eat " + eatInfo.getFood());
}
}
@Service
public class BetaService {
@Autowired
private AlphaService alphaService;
public void sayHello() {
alphaService.init();
alphaService.say("hello");
EatInfo eatInfo = new EatInfo();
eatInfo.setFood("饺子");
eatInfo.setAddress("你家");
alphaService.eat(eatInfo);
}
}
@RunWith(MockitoJUnitRunner.class)
public class BetaServiceTest {
@InjectMocks
private BetaService betaService;
@Mock
private AlphaService alphaService;
@Test
public void sayHello1() {
...
betaService.sayHello();
...
}
}
各场景下 verify 的使用:
-
校验 BetaService.sayHello 中是否调用了 AlphaService.init
@RunWith(MockitoJUnitRunner.class) public class BetaServiceTest { @InjectMocks private BetaService betaService; @Mock private AlphaService alphaService; @Test public void sayHello1() { ... betaService.sayHello(); ... // 校验 betaService.sayHello() 是否调用了 AlphaService.init verify(alphaService).init(); } }
-
校验 BetaService.sayHello 中调用了 AlphaService.init 的次数
@RunWith(MockitoJUnitRunner.class) public class BetaServiceTest { @InjectMocks private BetaService betaService; @Mock private AlphaService alphaService; @Test public void sayHello1() { ... 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) public class BetaServiceTest { @InjectMocks private BetaService betaService; @Mock private AlphaService alphaService; @Test public void sayHello1() { ... betaService.sayHello(); ... // 校验 betaService.sayHello() 没有调用过 AlphaService.run verify(alphaService, times(0)).run(); } }
或
@RunWith(MockitoJUnitRunner.class) public class BetaServiceTest { @InjectMocks private BetaService betaService; @Mock private AlphaService alphaService; @Test public void sayHello1() { ... betaService.sayHello(); ... // 校验 betaService.sayHello() 没有调用过 AlphaService.run verify(alphaService, never()).run(); } }
-
校验 BetaService.sayHello 中的 AlphaService.run 方法 最少 or 最多 调用了多少次
@RunWith(MockitoJUnitRunner.class) public class BetaServiceTest { @InjectMocks private BetaService betaService; @Mock private AlphaService alphaService; @Test public void sayHello1() { ... 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) public class BetaServiceTest { @InjectMocks private BetaService betaService; @Mock private AlphaService alphaService; @Test public void sayHello1() { ... betaService.sayHello(); ... // 校验 betaService.sayHello() 调用 AlphaService.say 的时候,参数值是 "hello" verify(alphaService).say("hello"); } }
任何参数值:
@RunWith(MockitoJUnitRunner.class) public class BetaServiceTest { @InjectMocks private BetaService betaService; @Mock private AlphaService alphaService; @Test public void sayHello1() { ... betaService.sayHello(); ... // 校验 betaService.sayHello() 调用 AlphaService.say 的时候,参数值是任何字符串 verify(alphaService).say(anyString()); } }
-
校验 BetaService.sayHello 中的 AlphaService.eat 方法被调用的时候,作为参数的 EatInfo 对象中属性 food 值为 “饺子”, 属性 address 值为 “你家”
@RunWith(MockitoJUnitRunner.class) public class BetaServiceTest { @InjectMocks private BetaService betaService; @Mock private AlphaService alphaService; @Test public void sayHello1() { ... 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 **文章来源:https://www.toymoban.com/news/detail-433778.html
-
校验在 BetaService.sayHello 中方法的调用顺序是 alphaService.init、alphaService.say、alphaService.eat文章来源地址https://www.toymoban.com/news/detail-433778.html
@RunWith(MockitoJUnitRunner.class) public class BetaServiceTest { @InjectMocks private BetaService betaService; @Mock private AlphaService alphaService; @Test public void sayHello1() { ... betaService.sayHello(); ... // 校验 betaService.sayHello() 中方法的调用顺序是 alphaService.init、alphaService.say、alphaService.eat InOrder alphaServiceIO = inOrder(alphaService); alphaServiceIO.verify(alphaService).init(); alphaServiceIO.verify(alphaService).say(anyString()); alphaServiceIO.verify(alphaService).eat(any(EatInfo.class)); } } ```
到了这里,关于Mockito 之 verify 使用方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!