【发布时间】:2017-06-23 10:58:25
【问题描述】:
我正在使用 Mockito 测试我的 SpringBoot 服务。
问题是我正在测试的一些服务与其他服务和存储库有多重依赖关系,这使得在服务的更深层次上执行测试变得困难。
例如:
“TestService”类包含以下方法:
public Test addTagToTest(Long id, Long tagId) {
Tag tag = tagService.getById(tagId);
Test test = getById(id);
test.addTag(tag);
return update(test);
}
这个类依赖于“TagService”。 注意:每个服务都有自己的存储库。
这是 TestService 正在使用的服务类:
@Service
public class TagService extends GenericAbstractService<Tag, TagRepo> {
public Tag getTagByName(String tagName){
在包含 @Test 方法的 JUnit 测试类中,我有这样的东西:
@Autowired
private TestService testService;
@Mock
private TestRepo repository;
所以现在的问题是如何测试我的 TestService 有依赖关系有依赖关系?必须模拟所有存储库。
【问题讨论】:
-
您是在加载 spring 上下文进行测试还是只是单元测试?
标签: java spring spring-boot junit mockito