【发布时间】:2019-01-09 11:04:21
【问题描述】:
我想问你几个问题并征求你的意见:
我想测试我的公共方法(我使用 Spring Boot、Mockito、JUnit):
@Service
public class MyClass{
public Long getClientId(List<String> nameSurname) throws AuthorizationException {
Long operatorId;
if(...){
(... something not interesting ...)
User user = getUserByLogin("AnthonyGates2");
operatorId = nonNull(user) ? user.getOperatorId() : null;
} else {
List<User> users = getUserListByLogin("AnthinyGates");
operatorId = isNotEmpty(users) ? return operatorId;
return operatorId;
}
如何测试方法getClientId?
方法 getUserByLogin 和 getUserListByLogin 在此类 (MyClass) 中是私有的,但我必须模拟这些私有方法的结果,因为这些方法从外部服务检索数据。
这些私有方法看起来像:
User user = DelegateImpl.getDelegate().getUserByLogin(nameAndSurname);
DelegateImpl.getDelegate().getUserByLogin 从数据库中获取数据,并且必须像这样模拟数据:
when(DelegateImpl.getDelegate().getUserByLogin(any())).thenReturn(user);
如何测试我的公开课?我应该使用 PowerMock/PowerMockito 吗?在我看来,公开这些方法很难看,因为这些方法只在MyClass 中调用。我在 Internet 上找不到适合我的案例的好的教程(Spring Boot、Mockito、JUnit)。
非常感谢您的所有提示!
最好的问候 马修
【问题讨论】:
-
不要模拟你的私有方法,模拟他们使用的外部服务。
标签: spring-boot junit mockito powermock powermockito