首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在普通Spring框架项目中使用@SpyBean和@MockBean

如何在普通Spring框架项目中使用@SpyBean和@MockBean
EN

Stack Overflow用户
提问于 2018-10-15 14:55:35
回答 1查看 3.3K关注 0票数 0

我们使用SpringFramework4.3.14-在out项目中发布。我们的应用程序的结构如下:

代码语言:javascript
复制
parent \
      module-one
      module-two
      ...
      module-n

现在,我们正试图在其中一个模块中编写一些集成测试,我们面临的下一个问题是:需要监视Spring数据JPA存储库。在Spring中,我们有@SpyBean@MockBean注释,但是它们在常规的Spring中是不可用的。我们无法迁移到Spring。

我们尝试将spring-boot-testspring-boot-test-starter添加到我们的项目中,但是,显而易见的是,它没有起作用:类路径中没有出现依赖关系。

我的问题是:有没有办法在常规项目中使用这些注释?或者其他监视Spring数据JPA存储库的方法?

更新:示例

代码语言:javascript
复制
@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;
        });
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-10-15 15:31:06

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52819500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档