我们使用SpringFramework4.3.14-在out项目中发布。我们的应用程序的结构如下:
parent \
module-one
module-two
...
module-n现在,我们正试图在其中一个模块中编写一些集成测试,我们面临的下一个问题是:需要监视Spring数据JPA存储库。在Spring中,我们有@SpyBean和@MockBean注释,但是它们在常规的Spring中是不可用的。我们无法迁移到Spring。
我们尝试将spring-boot-test和spring-boot-test-starter添加到我们的项目中,但是,显而易见的是,它没有起作用:类路径中没有出现依赖关系。
我的问题是:有没有办法在常规项目中使用这些注释?或者其他监视Spring数据JPA存储库的方法?
更新:示例
@Service
public void MegaService {
@Autowired
MegaRepository repository;
// ....
}
public interface MegaRepository extends JpaRepository<MegaModel, Long> {
// ...
}
@Configuration
public class TestRepoConfiguration {
@Bean
MegaRepository megaRepository(MegaRepository repository) {
return Mockito.spy(repository); // of course, it does not work
}
}
@RunWith(SpringJUnit4ClassRunner.class)
public class MegaIntegrationTest {
@Autowired
MegaRepository repository;
public void testMegaLogic() {
when(repository.findAll()).thenAnswer(invocation -> {
System.out.println("Hello, comrades");
return invocation;
});
}
}发布于 2018-10-15 15:31:06
https://stackoverflow.com/questions/52819500
复制相似问题