mockito 无效使用匹配器异常错误

xdyibdwo  于 2022-11-23  发布在  其他
关注(0)|答案(1)|浏览(199)

我是mockito的新手,正在尝试为这个getEmployeePorfilebySsoId(String ssoId)编写我的测试用例。

以下是方法

public List<EcommProfile> getEmployeePorfilebySsoId(String ssoId) {
        List<EcommProfile> profilelist = null;

        try (Sqlsession sqlsession = sqlsessionFactory.openEcomSession()) {
            profilelist = sqlsession.selectList("org.mybatis.ecommprofile", ssoId);
        } catch (Exception e) {
            throw e;
        }
    }

这是此方法的测试用例

@Mock
    SqlSessionFactory sqlSessionFactory;
    @Mock
    Sqlsession sqlsession;
    @InjectMocks
    EcommDaoimpl ecommDao;

    @Test
    public List<EcommProfile> getEmployeePorfilebySsoIdTestException(String ssoId) {
        List<EcommProfile> profilelist = new ArrayList<>();
        EcommProfile ecommProfile = new EcommProfile();
        ecommProfile.setControlNumber("12345");
        profilelist.add(ecommProfile);
        Mockito.when(sqlsession.selectList(Mockito.eq("org.mybatis.ecommprofile"), Mockito.eq("12345")))
                .thenThrow(ApplicationException.class);
        Mockito.when(sqlsessionFactory.openEcommSession()).thenReturn(sqlsession);
        Assertions.assertTrue(ApplicationException.class, () -> ecomDao.getEmployeeProfileByssoId(Mockito.eq("12345")));

    }

下面是我得到的错误(引发意外的异常类型)

Expected: com.mycompany.exception.ApplicationException
    Actual:org.mockito.exceptions.misusing.InvalidUseofMatcherException```.

PS: Please at least give me the hint or something.
vltsax25

vltsax251#

只是在回答我自己的问题。
该行ecomDao.getEmployeeProfileByssoId(Mockito.eq("12345)被替换为该行ecomDao.getEmployeeProfileByssoId("12345);
愚蠢的错误是在Assertions.assertTrue()条件中也使用Mockito.eq()

相关问题