简体   繁体   English

java 中的模拟方法返回类型

[英]Mock method return type in java

Below is main code consist of one util class and service class using it下面是主要代码,由一个实用程序 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;
    }
  }
}

Service Class is below:-服务 Class 如下:-

@Override
  public Page<SearchResultDto> findFilesWithPages(QueryParent queryParent, Pageable pageable)
    throws AtlasServiceException {
    //  Some code
    client = new ApacheAtlasUtils().createClient();
   //some code
      
      }

  

I am writing unit test for service method and I am getting exception for createClient method asking for values for url, username and password which should not happen as this should be mocked but the mocking is giving me below error我正在为服务方法编写单元测试,并且我收到 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)

The Test code is as follows:-测试代码如下:-

 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);

  } 
      

  
  
  

Your idea with spying is adequate (you can even go for mocking if you do not actually need any true implementation of that class).您对间谍的想法就足够了(如果您实际上不需要该类的任何真正实现,您甚至可以为 mocking 使用 go)。

The problem lies in the implementation:问题在于实现:

    //  Some code
    client = new ApacheAtlasUtils().createClient();
   //some code
      
      }

Instead of having the ApacheAtlasUtils as an instance variable (or a supplier method) you create the instance on the fly.您可以动态创建实例,而不是将ApacheAtlasUtils作为实例变量(或供应商方法)。

Mockito is not smart enough to catch that operation and replace the real object with you spy. Mockito 不够聪明,无法捕捉到该操作并用您的间谍替换真正的 object。

With the supplier method you can set up your test as follows:使用供应商方法,您可以按如下方式设置测试:

  @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();
  }

in your SUT:在您的 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM