首页 > 解决方案 > 即使使用 doReturn,Mockito 也会调用存根方法

问题描述

我想创建一个集成测试,其中在控制器上调用 put 方法并更新某个对象。在这个过程中,涉及到一个服务类,它调用第三方 API 来做一些事情。就我而言,我想存根调用第三方所涉及的服务方法,因为这不是测试第三方的重点。

话虽如此,我将展示我的代码,并等待关于为什么它不能按预期工作和/或任何其他解决方法的答案。

这是我的服务类,其中是我想要存根的方法调用。

public class ProjectService implements SomeInterfance {

// the third party service
private final DamConnector damConnector;
// some other fields

    public ProjectDTO save(ProjectDTO projectDTO) {
        log.debug("Request to save Project : {}", projectDTO);
        // some operations
        synchronizeWithDamAndSave(project, parentChanging); //this is the method call I want to be skiped
        //other operations
        return projectMapper.toDto(project, this);
    }

    //the method that I want to stub
    public Asset synchronizeWithDamAndSave(Project project, boolean includeDocuments) {
        Asset asset = synchronizeWithDam(project, includeDocuments);
        projectRepository.save(project);
        return asset;
    }

}

还有我的集成测试类:

@SpringBootTest(classes = SppApp.class)
public class ProjectResourceIT {

//other fields

//my service use autowire as it needs to make the service calls
@Autowired
private ProjectService projectService;

//this is my setup method where I create the spy of project service and define the doReturn behavior when my method is called
@BeforeEach
public void setup() {
    ProjectService spyProjectService = Mockito.spy(projectService);
    Mockito.doReturn(new Asset()).when(spyProjectService).synchronizeWithDamAndSave(Mockito.any(Project.class),Mockito.anyBoolean());
    MockitoAnnotations.initMocks(this);


    final ProjectResource projectResource = new ProjectResource(spyProjectService, clientService, securityService);
    this.restProjectMockMvc = MockMvcBuilders.standaloneSetup(projectResource)
        .setCustomArgumentResolvers(pageableArgumentResolver)
        .setControllerAdvice(exceptionTranslator)
        .setConversionService(createFormattingConversionService())
        .setMessageConverters(jacksonMessageConverter)
        .setValidator(validator).build();
    }

}
...

public void updateProject() throws Exception {
    // initialization of the test

    // this is where I call my controller
    restProjectMockMvc.perform(put("/api/projects")
        .contentType(TestUtil.APPLICATION_JSON_UTF8)
        .content(TestUtil.convertObjectToJsonBytes(projectDTO)))
        .andExpect(status().isOk());
    }

}

在我的情况下的问题是,mockito 在synchronizeWithDamAndSave方法之后进入

Mockito.doReturn(new Asset()).when(spyProjectService).synchronizeWithDamAndSave(Mockito.any(Project.class),Mockito.anyBoolean());

在从其余 api 调用的方法之前调用此行。

我应该怎么办?关于为什么会发生这种情况的任何提示?

标签: javaspring-bootmockitointegration-testing

解决方案


Spring Boot 的代理不适用于 Mockito。使用@SpyBean而不是@Autowired。


推荐阅读