<dependency>
<groupId>org.powermockgroupId>
<artifactId>powermock-module-junit4artifactId>
<version>1.6.5version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.powermockgroupId>
<artifactId>powermock-api-mockitoartifactId>
<version>1.6.5version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.mockitogroupId>
<artifactId>mockito-coreartifactId>
<version>1.10.19version>
<scope>testscope>
dependency>
//返回值可以自己定义
PowerMockito.when(faceCollectMapper.qryFaceAttachInfo(Mockito.anyObject())).thenReturn(attachInfo);
//方法调用多次,且返回值不同
PowerMockito.when(jdbcTemplate.batchUpdate(Mockito.anyObject())).thenReturn(arr).thenReturn(arr).thenThrow(new IllegalArgumentException("参数缺失"));
记得要在@PrepareForTest注解后面加静态方法类
//先mock静态方法类,再mock调用方法返回值
PowerMockito.mockStatic(ServiceClient.class);
PowerMockito.when(ServiceClient.CallService(Mockito.anyObject())).thenReturn(json1);
@Mock
private BindingResult bindingResult;
@Test
public void test(){
PowerMockito.when(bindingResult.hasErrors()).thenReturn(true);
FieldError fieldError = PowerMockito.mock(FieldError.class);
PowerMockito.when(bindingResult.getFieldError()).thenReturn(fieldError);
PowerMockito.when(fieldError.getDefaultMessage()).thenReturn("参数缺失");
}
//两种方法
PowerMockito.doThrow(new IllegalArgumentException("Error Message")).when(busiPromoteMapper).getSeq();
PowerMockito.when(jdbcTemplate.batchUpdate(Mockito.anyObject())).thenThrow(new IllegalArgumentException("Error Message"));
记得要在@PrepareForTest注解后面加被注入类(被测试类,不是File)
//Mock文件、流等
File file = Mockito.mock(File.class);
FileInputStream fin = Mockito.mock(FileInputStream.class);
//参数传入使用Mock数据的入参进行注入
PowerMockito.whenNew(File.class).withArguments("10011001").thenReturn(file);
PowerMockito.whenNew(FileInputStream.class).withArguments(file).thenReturn(fin);
PowerMockito.when(fin.available()).thenReturn(3816);
PowerMockito.when(file.exists()).thenReturn(true);
@Test
public void testMockPrivateMethodWithStub() throws Exception {
Student student = new Student();
String mockPrivateMethod = "mockPrivateMethod";
PowerMockito.stub(MemberMatcher.method(Student.class, "getPrivateMethod")).toReturn(mockPrivateMethod);
assertThat(student.callPrivateMethod(), is(mockPrivateMethod));
}
@Test
public void testMockEnum(){
PersonType personType = PowerMockito.mock(PersonType.class);
Whitebox.setInternalState(PersonType.class, "N", personType);
when(personType.getType()).thenReturn("mockN");
assertThat(PersonType.N.getType(), is("mockN"));
assertThat(PersonType.S.getType(), is("student"));
}
注:这个不是使用PowerMockito,但是这里一并总结了
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-mockartifactId>
<version>1.1version>
dependency>
//MOCK httpServletRequest
//创建Mock对象
MockHttpServletRequest request = new MockHttpServletRequest();
//创建Mock对象
MockHttpServletResponse response = new MockHttpServletResponse();
//设置流的内容
request.setContent(reqStr.getBytes());
https://github.com/powermock/powermock/wiki/Mockito
https://blog.csdn.net/qisibajie/article/details/79068086
对于阿拉盯项目中的单元测试总结就到这里,应该可以覆盖项目中的大部分代码。有什么错误或者补充还请大家可以告知。