mercredi 20 mai 2015

How do I mocking with Mockito?

I'm doing unit testing and I can't program the Mockito to cover a portion of the code.
How do I get Mockito return me something valid? I get an IllegalArgumentExpection that code when i get spec. Sorry if it's an ignorant question, I started writing tests recently.

My test

@Bean
        public SpecDBDAO getSpecDBDAO() {
            SpecDBDAO dao = Mockito.mock(SpecDBDAO.class);
            when(dao.findLastOne(new BasicDBObject("_id", "erro"))).thenReturn(new BasicDBObject());
            return dao;
        }

 @Test
    public void testAddLinha_validId() throws Exception {
        planilhaService.addLinha("123", new BasicDBObject("_id", "erro"));
    }

My code

public Planilha addLinha(String id, BasicDBObject body) {
        String idSpec = body.getString("_id", "");
        Planilha planilha = specDBPlanilhasDAO.get(id);
        if (planilha == null) {
            throw new NotFoundException("Planilha não encontrada.");
        }

        try {
            BasicDBObject spec = specDBDAO.findLastOne(new BasicDBObject("_id", new ObjectId(idSpec)));
            if (spec.isEmpty()) {
                throw new NotFoundException("Especificação não encontrada.");
            }
            planilha.addLinha(spec);
            planilha = specDBPlanilhasDAO.update(planilha);

            return planilha;
        } catch (IllegalArgumentException e) {
            throw new BadRequestException("Id inválido.");
        }
    }

Coverage enter image description here

Aucun commentaire:

Enregistrer un commentaire