gpt4 book ai didi

java - Junit -mockito 需要但未调用 :

转载 作者:行者123 更新时间:2023-12-02 01:23:04 26 4
gpt4 key购买 nike

正在为我的服务实现类编写测试类。该服务实现类与我的存储库接口(interface)进行通信以进行数据库操作。

代码工作正常,但是当我为此实现编写测试用例时,并从 junit 收到“想要但未调用”错误。

请在下面找到我的类(class)。

以下是我的存储库界面

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);
}
}

请找到我的测试类(class)

@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

最佳答案

正如其他人指出的那样,您已经犯了常见的初学者错误:

  • 不要模拟被测类
  • 通过使用注释( @Mock@InjectMocks )或手动向 提供对模拟的引用,确保您的模拟被注入(inject)到您的被测类中被测类
  • 在测试中使用被测类的实例来实际测试某些内容。

请务必阅读 documentation和教程(例如 thisthisthis )。这里还可以找到很多类似的问题,其答案也提供了一些代码示例。

关于java - Junit -mockito 需要但未调用 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57345000/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com