JAVA 8 junit 单元测试——静态方法单元测试 Mockito.mockStatic
静态方法的单元测试Mockito直接上例子方法public ArrayList<SearchFields> convertToSearchFieldsList(String ruleJsonString) {return JSON.parseObject(ruleJsonString, new TypeReference<ArrayList<SearchFields>
·
静态方法的单元测试Mockito
直接上例子
方法
public ArrayList<SearchFields> convertToSearchFieldsList(HashMap<String, ArrayList<SearchFieldValue>> filter) {
return ESUtils.convertToSearchFieldsList(filter);
}
convertToSearchFieldsList方法就是静态方法,可看下:
单测
import org.junit.Assert;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
@Test
public void testConvertToSearchFieldsList() {
SearchFields searchFields = new SearchFields();
searchFields.setOperation("2");
ArrayList<SearchFields> combineRule = new ArrayList<>();
combineRule.add(searchFields);
HashMap<String, ArrayList<SearchFieldValue>> filter = new HashMap<>();
ArrayList<SearchFieldValue> list = new ArrayList<>();
SearchFieldValue searchFieldValue = new SearchFieldValue();
searchFieldValue.setVal("er");
list.add(searchFieldValue);
filter.put("1",list);
try(MockedStatic<ESUtils> theMock = Mockito.mockStatic(ESUtils.class)) {
theMock.when(() -> ESUtils.convertToSearchFieldsList(Mockito.any())).thenReturn(combineRule);
}
Assertions.assertThat(ucompanyServiceImpl.convertToSearchFieldsList(filter));
}
【加粗提示】
try(MockedStatic<ESUtils> theMock = Mockito.mockStatic(ESUtils.class)) {
theMock.when(() -> ESUtils.convertToSearchFieldsList(Mockito.any())).thenReturn(combineRule);
}
更多推荐
已为社区贡献1条内容
所有评论(0)