Search code examples
spring-bootmockitojunit5

Invalid Use of Matcher Exception


I am very new to mockito and trying to write my test cases for this getEmployeePorfilebySsoId(String ssoId).

Below is the method

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

Here is my Testcase for this method

 @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")));

    }

Below is the error i am getting (Unexpected exception type thrown)

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

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

Solution

  • Just answering my own question here.

    This line ecomDao.getEmployeeProfileByssoId(Mockito.eq("12345) is replace to this one ecomDao.getEmployeeProfileByssoId("12345);.

    Silly mistake was using Mockito.eq() inside the Assertions.assertTrue() condition as well.