简体   繁体   English

即使使用 doReturn,Mockito 也会调用存根方法

[英]Mockito calls stubbed method even if using doReturn

I want to create an integration test where a put method is called on a controller and it updates a certain object.我想创建一个集成测试,其中在控制器上调用 put 方法并更新某个对象。 During this process, a service class is involved which calls a third party API to do some stuff.在这个过程中,涉及到一个服务类,它调用第三方 API 来做一些事情。 In my case, I want to stub the service method which is involved in calling the third party as it is not the point to test the third party.就我而言,我想存根调用第三方所涉及的服务方法,因为这不是测试第三方的重点。

Having that said, I will present my code and I wait for an answer about why this does not work as expected and/or any other workaround.话虽如此,我将展示我的代码,并等待有关为什么这不能按预期工作和/或任何其他解决方法的答案。

This is my service class in which is the method call I want to stub.这是我的服务类,其中是我想要存根的方法调用。

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

}

And my integration test class:我的集成测试类:

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

}

The problem in my case is that mockito enters in synchronizeWithDamAndSave method just after在我的情况下的问题是,mockito 紧接着进入synchronizeWithDamAndSave方法

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

this line is called, before the method to be called from the rest api.在要从其余 api 调用的方法之前调用此行。

What should I do?我该怎么办? Any hints about why this is happening?关于为什么会发生这种情况的任何提示?

Spring Boot's proxies are not working wit Mockito. Spring Boot 的代理在 Mockito 中不起作用。 Use @SpyBean instead of @Autowired.使用@SpyBean而不是@Autowired。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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