mockito快速入门

2022-10-06 14:09:58

在这里插入图片描述

自动化测试是一个安全网。 他们运行并通知系统是否已损坏,以便可以非常快速地修复有问题的代码。 如果测试套件运行一个小时,则快速反馈的目的会受到损害。

  • 以下是测试不友好行为的示例:
    • 获取数据库连接并获取/更新数据
    • 连接到互联网并下载文件
    • 与SMTP服务器进行交互以发送电子邮件
    • 查找JNDI对象
    • 调用网络服务
    • 执行I / O操作,例如打印报告

Mockito在模拟外部依赖项中扮演关键角色。

快速入门案列

在pom.xml中添加maven依赖

<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.mockito</groupId><artifactId>mockito-all</artifactId><version>1.10.19</version><scope>test</scope></dependency>

样例代码:

一、

@RunWith(SpringRunner.class)publicclassMockBeanAnnotationIntegrationTest{//mock对象@MockBean
    UserRepository mockRepository;@Autowired
    ApplicationContext context;@TestpublicvoidgivenCountMethodMocked_WhenCountInvoked_ThenMockValueReturned(){//指定mock对象的行为
        Mockito.when(mockRepository.count()).thenReturn(123L);//通过应用程序上下文获取Mock的对象//也可以直接调用mockRepository.count()方法
        UserRepository userRepoFromContext= context.getBean(UserRepository.class);long userCount= userRepoFromContext.count();
 
        Assert.assertEquals(123L, userCount);//验证被调用的就是Mock的对象
        Mockito.verify(mockRepository).count();}}

二、

@RunWith(MockitoJUnitRunner.class)publicclassAccountLoginControllerTest{//@Mockprivate AccountDao accountDao;private HttpServletRequest request;private AccountLoginController accountLoginController;@BeforepublicvoidsetUp(){this.accountDao= Mockito.mock(AccountDao.class);this.request= Mockito.mock(HttpServletRequest.class);this.accountLoginController=newAccountLoginController(accountDao);}@TestpublicvoidtestLoginSuccess(){
        Account account=newAccount();when(request.getParameter("username")).thenReturn("alex");when(request.getParameter("password")).thenReturn("123456");when(accountDao.findAccount(anyString(),anyString())).thenReturn(account);assertThat(accountLoginController.login(request),equalTo("/index"));}@TestpublicvoidtestLogin505(){when(request.getParameter("username")).thenReturn("alex");when(request.getParameter("password")).thenReturn("1234561");when(accountDao.findAccount(anyString(),anyString())).thenThrow(UnsupportedOperationException.class);assertThat(accountLoginController.login(request),equalTo("/505"));}}

注意:

  1. 每次调用返回值不一样
  Person person=mock(Person.class);// 第一次调用返回"xiaoming",第二次调用返回"xiaohong"when(person.getName()).thenReturn("xiaoming").thenReturn("xiaohong");when(person.getName()).thenReturn("xiaoming","xiaohong");
  1. mockito模拟测试无返回值的方法
// mockito模拟测试无返回值的方法
Person person=mock(Person.class);doNothing().when(person).remove();
  1. mockito还能对被测试的方法强行抛出异常
// mockito还能对被测试的方法强行抛出异常
Person person=mock(Person.class);doThrow(newRuntimeException()).when(person).remove();when(person.next()).thenThrow(newRuntimeException());

注解解释

Mockito.mock()
@Mock
@MockBean

Mockito.mock

mock()方法可以创建类或接口的模拟对象。

我们可以使用mock来指定方法的行为,并验证它们是否被调用。

例子:

@TestpublicvoidgivenCountMethodMocked_WhenCountInvoked_ThenMockedValueReturned(){//mock对象
    UserRepository localMockRepository= Mockito.mock(UserRepository.class);//指定mock对象的行为
    Mockito.when(localMockRepository.count()).thenReturn(0L);//调用mock对象long userCount= localMockRepository.count();
 
    Assert.assertEquals(0L, userCount);//验证localMockRepository的方法被调用
    Mockito.verify(localMockRepository).count();}

在使用此方法之前,不需要执行任何其他操作。我们可以使用它在类中Mock字段或者在函数中Mock局部变量。

@Mock注解

Mock注解实际上是mockito.mock()方法的缩写,同样,我们应该只在测试类中使用它。

与mock()方法不同,我们需要启用mockito注解来使用此注解。我们可以通过使用mockitojunitrunner运行测试或显式调用mockitoannotations.initmocks()方法来实现这一点。

MockitoJUnitRunner

@RunWith(MockitoJUnitRunner.class)publicclassMockAnnotationUnitTest{@Mock
    UserRepository mockRepository;@TestpublicvoidgivenCountMethodMocked_WhenCountInvoked_ThenMockValueReturned(){
        Mockito.when(mockRepository.count()).thenReturn(123L);long userCount= mockRepository.count();
 
        Assert.assertEquals(123L, userCount);
        Mockito.verify(mockRepository).count();}}

相比较mock方法,Mock注解除了使代码更易读之外,还可以在出现故障时更容易找到问题,因为在错误消息中会出现字段名称。


Wanted but not invoked:
mockRepository.count();......
Actually, there were zero interactions withthis mock.

MockBean注解

我们可以使用@mockBean注解将Mock对象添加到Spring上下文中。

Mock将替换Spring上下文中任何相同类型的现有bean,如果没有定义相同类型的bean,将添加一个新的bean。

MockBean注解在集成测试中很有用,在集成测试中需要模拟特定bean,例如外部Service。

如果需要使用Mockbean注解,需要使用SpringRunner(Junit5 中是SpringExtention)

@RunWith(SpringRunner.class)publicclassMockBeanAnnotationIntegrationTest{//mock对象@MockBean
    UserRepository mockRepository;@Autowired
    ApplicationContext context;@TestpublicvoidgivenCountMethodMocked_WhenCountInvoked_ThenMockValueReturned(){//指定mock对象的行为
        Mockito.when(mockRepository.count()).thenReturn(123L);//通过应用程序上下文获取Mock的对象//也可以直接调用mockRepository.count()方法
        UserRepository userRepoFromContext= context.getBean(UserRepository.class);long userCount= userRepoFromContext.count();
 
        Assert.assertEquals(123L, userCount);//验证被调用的就是Mock的对象
        Mockito.verify(mockRepository).count();}}

Mockito和PowerMock用法

  • 作者:紫金小飞侠
  • 原文链接:https://blog.csdn.net/yangshengwei230612/article/details/104672719
    更新时间:2022-10-06 14:09:58