首页 > 解决方案 > 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

标签: javajunitmockito

解决方案


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

  • 不要嘲笑class under test.
  • 确保您的模拟被注入class under test到您@Mock的.@InjectMocksclass under test
  • 在测试中使用你的实例class under test来实际测试一些东西。

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


推荐阅读