我是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.
1条答案
按热度按时间vltsax251#
只是在回答我自己的问题。
该行
ecomDao.getEmployeeProfileByssoId(Mockito.eq("12345)
被替换为该行ecomDao.getEmployeeProfileByssoId("12345);
。愚蠢的错误是在
Assertions.assertTrue()
条件中也使用Mockito.eq()
。