示例
public List<Authen> selectAuthenList(Authen authen) {
//静态方法
LoggedInUser loginUser = WebUtil.getLoggedInUser();
String tenantId = loginUser.getTenantId();
authen.setTenantId(Long.parseLong(tenantId));
//业务
return authenMapper.selectAuthenList(authen);
}
静态方法示例
public class WebUtil {
public static LoggedInUser getLoggedInUser() {
//方法逻辑
return;
}
}
测试方法
@RunWith(PowerMockRunner.class)
//要mock的静态类
@PrepareForTest(WebUtil.class)
@PowerMockIgnore("jdk.internal.reflect.*")
public class AuthenServiceImplTest extends BaseMockitoTest {
@InjectMocks
private AuthenServiceImpl authenService;
@Mock
private AuthenMapper authenMapper;
@Test
public void selectAuthenListTest() {
Authen authen = new Authen();
authen.setAppid("appId");
authen.setId(1L);
//返回需要的参数
LoggedInUser loggedInUser = new LoggedInUser();
loggedInUser.setTenantId("1234");
loggedInUser.setLoginName("loginName");
//注入要mock的类
PowerMockito.mockStatic(WebUtil.class);
//mock静态方法
PowerMockito.when(WebUtil.getLoggedInUser()).thenReturn(loggedInUser);
when(authenMapper.selectAuthenList(anyObject())).thenReturn(list);
List<Authen> authens = authenService.selectAuthenList(authen);
assertThat(authens.get(0).getAppid(),is("appId"));
}
}
标签:WebUtil,selectAuthenList,Mockito,class,static,authen,public,mock From: https://www.cnblogs.com/Dshzs17/p/17362243.html