[英]Mock method return type in java
下面是主要代码,由一个实用程序 class 和使用它的服务 class 组成
@PropertySource("classpath:atlas-application.properties")
public class ApacheAtlasUtils {
@Value("${atlas.rest.address}")
private String atlasURL;
@Value("${atlas.rest.user}")
private String atlasUsername;
@Value("${atlas.rest.password}")
private String atlasPassword;
private AtlasClientV2 client;
public AtlasClientV2 createClient() {
if (client == null) {
return new AtlasClientV2(new String[] {atlasURL}, new String[] {atlasUsername, atlasPassword});
} else {
return client;
}
}
}
服务 Class 如下:-
@Override
public Page<SearchResultDto> findFilesWithPages(QueryParent queryParent, Pageable pageable)
throws AtlasServiceException {
// Some code
client = new ApacheAtlasUtils().createClient();
//some code
}
我正在为服务方法编写单元测试,并且我收到 createClient 方法的异常,要求输入 url 的值,用户名和密码不应该发生,因为这应该被模拟,但是 mocking 给了我以下错误
java.lang.IllegalArgumentException: Base URL cannot be null or empty.
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:141)
at org.apache.atlas.AtlasServerEnsemble.<init>(AtlasServerEnsemble.java:35)
at org.apache.atlas.AtlasBaseClient.determineActiveServiceURL(AtlasBaseClient.java:318)
at org.apache.atlas.AtlasBaseClient.initializeState(AtlasBaseClient.java:460)
at org.apache.atlas.AtlasBaseClient.initializeState(AtlasBaseClient.java:448)
at org.apache.atlas.AtlasBaseClient.<init>(AtlasBaseClient.java:132)
at org.apache.atlas.AtlasClientV2.<init>(AtlasClientV2.java:82)
at com.jlr.stratus.commons.utils.ApacheAtlasUtils.createClient(ApacheAtlasUtils.java:40)
at com.jlr.stratus.rest.service.impl.FileSearchService.findFilesWithPages(FileSearchService.java:49)
测试代码如下:-
private FileSearchService fileSearchService;
@Spy
private ApacheAtlasUtils apacheAtlasUtils;
@Mock
private AtlasClientV2 client;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
fileSearchService = new FileSearchService();
}
@Test
public void findFilesWithPages_searchAll() throws AtlasServiceException {
Mockito.doReturn(client).when(apacheAtlasUtils).createClient();
service.search(queryParent,pageable);
}
您对间谍的想法就足够了(如果您实际上不需要该类的任何真正实现,您甚至可以为 mocking 使用 go)。
问题在于实现:
// Some code
client = new ApacheAtlasUtils().createClient();
//some code
}
您可以动态创建实例,而不是将ApacheAtlasUtils
作为实例变量(或供应商方法)。
Mockito 不够聪明,无法捕捉到该操作并用您的间谍替换真正的 object。
使用供应商方法,您可以按如下方式设置测试:
@Spy
private FileSearchService fileSearchService = new FileSearchService();
@Spy
private ApacheAtlasUtils apacheAtlasUtils = new ApacheAtlasUtils();
@Mock
private AtlasClientV2 client;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
doReturn(apacheAtlasUtils).when(fileSearchService).getApacheUtils();
}
在您的 SUT 中:
@Override
public Page<SearchResultDto> findFilesWithPages(QueryParent queryParent, Pageable pageable)
throws AtlasServiceException {
// Some code
client = getApacheUtils().createClient();
//some code
}
ApacheAtlasUtils getApacheUtils(){
return new ApacheAtlasUtils();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.