mercredi 23 décembre 2020

Mockito catch exception in void method

I have this method:

public void deleteTableEsiti() throws Exception {
        try{
            EsitiDAO esitiDAO =  new EsitiDAO();
            int deletedEsiti = esitiDAO.deleteTableEsiti(em);
            
        }
        catch(Exception e ){
            log.error(e);
        }
        finally{
            em.close();
        }
    }

I'm trying to make the test that cover the catch exception. I have tried many times but I can't cover that lines with Mockito.

This is the test that i have write:

@Test(expected=Exception.class)
    public void mockException(){
        ServiceG service = Mockito.mock(ServiceG.class);
         try {
            Mockito.doThrow(Exception.class).when(service).deleteTableEsiti();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        };
        //Mockito.when(service.deleteTableEsiti()).thenThrow(new Exception());
    }

I have always this error: [ERROR] Failures: [ERROR] JUnit.mockException Expected exception: java.lang.Exception

and lines are not covered by test.

Aucun commentaire:

Enregistrer un commentaire