我正在尝试编写一些 JUnit 测试。在我的测试类中,我需要监视在实现中具有事务方法的服务。当我试图监视该服务时,出现此错误:
Mockito cannot mock/spy because :
- final class
如果我@Transactional从方法中删除,则间谍工作正常。我知道这是因为服务代理的问题。我该如何解决这个问题?
我的测试课:
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {CreateIfcClusterTaskTest.Config.class})
@DirtiesContext(classMode =
DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class CreateIfcClusterTaskTest {
static class Config extends SpringTestConfiguration {
@Bean
@Primary
public VirtualControllerService
virtualControllerService(VirtualControllerService
virtualControllerService) {
return Mockito.spy(virtualControllerService);
}
}
}
我的服务 - VirtualControllerService:
public interface VirtualControllerService {
JsonHost createVifcHostForWebApi(VirtualController virtualController);
void stopDocker(DockerClient docker, String joinerIpAddress) throws
DockerException, InterruptedException;
}
我的服务实现 - VirtualControllerServiceImpl:
@Service
public class VirtualControllerServiceImpl implements
VirtualControllerService {
@Override
@Transactional
public JsonHost createVifcHostForWebApi(VirtualController
virtualController) {
JsonHost jsonHost = new JsonHost();
jsonHost.setIP(virtualController.getIpAddress());
jsonHost.setUser(environment.getProperty("VIFC_WEBAPI_USER"));
jsonHost.setPassword(environment.getProperty("VIFC_WEBAPI_PASSWORD"));
return jsonHost;
}
}
智慧大石
冉冉说
相关分类