【问题标题】:Junit - mockito Wanted but not invoked:Junit - mockito 想要但未调用:
【发布时间】: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

标签: java junit mockito


【解决方案1】:

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

  • 不要嘲笑class under test
  • 通过使用注释(@Mock@InjectMocks)或手动向 class under test 提供对模拟的引用,确保将您的模拟注入到您的 class under test
  • 在测试中使用class under test 的实例来实际测试一些东西。

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

【讨论】:

  • 谢谢。我已经添加了assertThat() 方法,现在正面临The method is(Integer) is ambiguous for the type UserDeactivationThresholdServiceImplTest 问题。我已经更新了问题中的详细信息。
  • 为什么不直接使用assertEquals呢? (歧义可能来自混合进口)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多