Mockito 的用法
本文为博主原创,转载请注明出处:
Mockito 是一个基于MIT协议的开源java测试框架。
Mockito区别于其他模拟框架的地方主要是允许开发者在没有建立“预期”时验证被测系统的行为。
对于mock对象的一个评价是测试系统的测试代码是一个高耦合的,由于 Mockito试图通过移除“期望规范”去除expect-run-verify(期望验证模式)的模式,
因此在耦合度上有所降低。这样的结果是简化了测试代码,使他更易读和修改了。
步骤一:引用相关的jar依赖
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.8.5</version> <scope>test</scope> </dependency>
步骤 2: 添加java源码
public class Person { private final Integer personID; private final String personName; public Person( Integer personID, String personName ) { this.personID = personID; this.personName = personName; } public Integer getPersonID() { return personID; } public String getPersonName() { return personName; } }
public interface PersonDao { public Person fetchPerson( Integer personID ); public void update( Person person ); }
public class PersonService { private final PersonDao personDao; public PersonService( PersonDao personDao ) { this.personDao = personDao; } public boolean update( Integer personId, String name ) { Person person = personDao.fetchPerson( personId ); if( person != null ) { Person updatedPerson = new Person( person.getPersonID(), name ); personDao.update( updatedPerson ); return true; } else { return false; } } }
--添加单元测试类
public class PersonServiceTest { @Mock private PersonDao personDAO; private PersonService personService; @Before public void setUp() throws Exception { MockitoAnnotations.initMocks( this ); personService = new PersonService( personDAO ); } @Test public void shouldUpdatePersonName() { Person person = new Person( 1, "Phillip" ); when( personDAO.fetchPerson( 1 ) ).thenReturn( person ); boolean updated = personService.update( 1, "David" ); assertTrue( updated ); verify( personDAO ).fetchPerson( 1 ); ArgumentCaptor<Person> personCaptor = ArgumentCaptor.forClass( Person.class ); verify( personDAO ).update( personCaptor.capture() ); Person updatedPerson = personCaptor.getValue(); assertEquals( "David", updatedPerson.getPersonName() ); // asserts that during the test, there are no other calls to the mock object. verifyNoMoreInteractions( personDAO ); } @Test public void shouldNotUpdateIfPersonNotFound() { when( personDAO.fetchPerson( 1 ) ).thenReturn( null ); boolean updated = personService.update( 1, "David" ); assertFalse( updated ); verify( personDAO ).fetchPerson( 1 ); verifyZeroInteractions( personDAO ); verifyNoMoreInteractions( personDAO ); } }
项目当中使用的另一种写法如下:
ApacheHttpClientService apacheHttpClientService = Mockito.mock(ApacheHttpClientService.class); ReflectionTestUtils.setField(wsDomainConfService, "apacheHttpClientService", apacheHttpClientService); ApacheHttpClientResult result = new ApacheHttpClientResult(); int statusCode = 200; result.setStatusCode(statusCode); result.setResponseContent(responseContent); // 获取厂商鉴权1 Mockito.when(apacheHttpClientService.getForObject(Mockito.any(String.class), Mockito.any(Map.class))) .thenReturn(result);