【发布时间】:2019-08-05 03:13:43
【问题描述】:
我正在为我的服务实现类写 test class。该服务实现类与我的数据库操作的存储库接口进行通信。
代码工作正常,但是当我为此实现编写测试用例时,从 junit 收到“想要但未调用”错误。
请在下面找到我的课程。
下面是我的Repository界面
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);
}
下面是我的Service接口
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 *
我已将以下代码添加到我的测试类中。但是现在遇到了不同的问题“方法 is(Integer) 对于 UserDeactivationThresholdServiceImplTest 类型不明确”
List<UserDeactivationThreshold> userDeactivationThresholdList = userDeactivationThresholdServiceImpl.getThresholdValue("BCP");
assertThat(userDeactivationThresholdList.size(), is(1));
收到is(1) 的以下消息。
The method is(Integer) is ambiguous for the type UserDeactivationThresholdServiceImplTest
【问题讨论】:
-
测试中在哪里调用getThresholdValue方法?
-
@MaciejKowalski - 我尝试调用
getThresholdValue()方法。那个时候是Actually, there were zero interactions with this mock.。我的问题也更新了。 -
错误信息非常明确:模拟没有被调用。您希望模拟被调用。因此,测试失败。根本原因是你连接你的模拟,但没有调用被测系统。
-
你完全错过了测试的重点。您的类名为 UserDeactivationThresholdServiceImplTest。给定这个名字,它应该测试 UserDeactivationThresholdServiceImpl 类的代码是否在做它应该做的事情。所以测试应该使用,即调用这个类的方法。但事实并非如此。那么,你在这里测试什么?什么都没有,除了 Mockito。
-
@Karthikeyan 在您的第一次尝试中,您甚至没有使用被测类。在第二次尝试中,您模拟了被测类,从而将您要测试的代码替换为模拟代码。同样:要测试一些代码,您需要执行该代码。不要嘲笑被测类。模拟它的依赖。阅读此内容以获得详细说明:stackoverflow.com/a/28783849/571407