java - Junit - mockito 想要但未调用:
问题描述
我正在test class
为我的服务实现类写作。该服务实现类与我的数据库操作的存储库接口通信。
代码工作正常,但是当我为这个实现编写测试用例并从junit得到“想要但没有调用”错误时。
请在下面找到我的课程。
以下是我的存储库界面
public interface UserRepository extends JpaSpecificationExecutor<UserDeactivationThreshold>,CrudRepository<UserDeactivationThreshold,Long> {
@Query("SELECT bt from BusinessTypes bt where bt.code = :businessTypeCode")
BusinessTypes findByBusinessCodeId(@Param("businessTypeCode") String businessTypeCode);
}
以下是我的服务接口
public interface UserDeactivationThresholdService {
public List<UserDeactivationThreshold> getThresholdValue(String businessTypeName);
}
以下是我的服务实现类
@Service
@Transactional(readOnly = true)
public class UserDeactivationThresholdServiceImpl implements UserDeactivationThresholdService {
private UserRepository userRepository;
@Autowired
public UserDeactivationThresholdServiceImpl(UserRepository userRepository,SecurityClient securityClient, EmailDispatcherService emailDispatcherService) {
this.userRepository = userRepository;
}
@Override
public List<UserDeactivationThreshold> getThresholdValue(String businessTypeName){
return userRepository.findByBusinessType(businessTypeName);
}
}
请找到我的测试班
@RunWith(MockitoJUnitRunner.class)
public class UserDeactivationThresholdServiceImplTest {
@Mock
private UserRepository userRepository;
@Mock private UserDeactivationThresholdServiceImpl userDeactivationThresholdServiceImpl;
@Test
public void shouldGetThresholdValueTest() {
UserDeactivationThreshold userDeactivationThreshold = make(a(UserDeactivationThresholdMaker.BCP));
when(userRepository.findByBusinessType("BCP")).thenReturn(asList(userDeactivationThreshold));
verify(userRepository, times(1)).findByBusinessType("BCP");
}
}
出现以下错误:
Wanted but not invoked:
userDeactivationThresholdRepository.findByBusinessType(
"BCP"
);
-> at com.core.service.impl.UserDeactivationThresholdServiceImplTest.shouldGetThresholdValueTest(UserDeactivationThresholdServiceImplTest.java:58)
Actually, there were zero interactions with this mock.
* 更新 *
我也尝试过这种方式,但也没有奏效。
verify(userDeactivationThresholdServiceImpl, times(1)).getThresholdValue("BCP");
跟随我得到的错误。
-> at com.core.service.impl.UserDeactivationThresholdServiceImplTest.shouldGetThresholdValueTest(UserDeactivationThresholdServiceImplTest.java:58)
Actually, there were zero interactions with this mock.
* 更新 2 *
我已将以下代码添加到我的测试类中。但现在我遇到了不同的问题“方法是(整数)对于 UserDeactivationThresholdServiceImplTest 类型不明确”
List<UserDeactivationThreshold> userDeactivationThresholdList = userDeactivationThresholdServiceImpl.getThresholdValue("BCP");
assertThat(userDeactivationThresholdList.size(), is(1));
我收到以下消息is(1)
。
The method is(Integer) is ambiguous for the type UserDeactivationThresholdServiceImplTest
解决方案
推荐阅读
- python - 1行函数获取中间字母,语法错误
- ios - 如何处理词汇或处理器问题
- python-2.7 - 错误:找不到满足 imaplib 要求的版本(来自版本:无)
- excel - 选择单元格范围并保存到新文档
- php - Laravel app->make(ClassA) 类和 new ClassA() 有什么区别
- python-3.x - 我无法识别正确的 Xpath 与 chrome-webdriver 使用 python 来自动登录过程
- python - 比较一个 Excel 文件中的两个不同工作表
- asp.net-core - EntityFramework Core 2:使用过滤列表的一对多映射
- ms-word - 如何更改 Word Docx 的页码
- jestjs - 节点模块的 Jest 重置模拟