How to create a unit test using mockite in a list of objects

Asked

Viewed 334 times

1

I know exactly what I need to mock about the object data, but I have no idea how to implement a test when it comes to a list of objects, I just need an example, someone could only show me an example by which I can have a brief notion of how to implement my unit test in relation to the context of that method below which is in the service class?

@Service
@TransactionalEntrada
public class IndicioService extends AbstractService<IndicioEntity, IndicioDAO> {

    @Autowired
    private HistoricoSituacaoService historicoSituacaoService;

    @Autowired
    private IndicioDAO indicioDAO;

    @Override
    protected IndicioDAO getDao() {

        return indicioDAO;
    }

 public void importarIndicios(List<IndicioEntity> indicios,
            UsuarioLogadoExterno usuarioLogado) throws PrincipalException {

        for (final IndicioEntity indicio : indicios) {
            indicio.setDataUltimaMovimentacao(getDataAtual());
            final SituacaoIndicioEntity situacao = new SituacaoIndicioEntity();
            situacao.setCodigo(1);
            indicio.setSituacaoAtual(situacao);
            this.inserir(indicio);// salvar indicios

            final HistoricoSituacaoEntity historicoSituacao =
                    new HistoricoSituacaoEntity();

            historicoSituacao.setCodigoUsuario(usuarioLogado.getCodigo());
            historicoSituacao.setCodigoIndicio(indicio.getCodigo());
            historicoSituacao.setCodigoSituacaoIndicio(
                    indicio.getSituacaoAtual().getCodigo());
            historicoSituacao
                    .setDataMovimentacao(indicio.getDataUltimaMovimentacao());

            historicoSituacaoService.inserir(historicoSituacao);
        }

    }
}

1 answer

2


In your test context, the most important thing to do is to validate whether the properties of HistoricoSituacaoEntity created within the method are in accordance with what you expect, and if the historicoSituacaoService.inserir was called as expected.

An example of what this test would look like:

public class IndicioServiceTest {

    @Test
    public void importarIndicios() {
        ArgumentCaptor<HistoricoSituacaoEntity> historicoCaptor = ArgumentCaptor.forClass(HistoricoSituacaoEntity.class); // Utilizado para capturar o valor passado à um método

        List<IndicioEntity> indicios = ... (Criar seus indicios manualmente);
        UsuarioLogadoExterno usuarioLogado = ... (Criar seu usuario logado manualmente);

        service.importarIndicios(indicios, usuarioLogado); // Chamada para sua função

        Mockito.verify(historicoSituacaoService).inserir(historicoCaptor.capture()); // Validação da chamada do serviço e captura do histórico criado

        HistoricoSituacaoEntity historicoCriado = historicoCaptor.getValue();

        Assert.assertEquals(usuarioLogado.getCodigo(), historicoCriado.getCodigoUsuario());
        ... Validar outras propriedades do indicio e usuário que devem estar no histórico que foi criado dentro do método ...
    }

    @Mock
    private IndicioDAO indicioDao;

    @Mock
    private HistoricoSituacaoService historicoSituacaoService;

    @InjectMocks
    private IndicioService service;
}

I suggest you always think about the most important part of your test, what you really need to test. It’s more important that you validate (assert) and verify what you really need than just write a test just for writing.

Documentation of Argumentcaptor

  • I understood perfectly your suggestion, I put your code and bumped into two things , line 5 the syntax for was not recognised as valid, and in line 16 of the code the method assertEquals the eclipse said that it accepts in the signature of the method an int and then an integer, maybe I should use another method. because the return is an integer and then another integer.

  • 1

    I apologize, I made the head code :) the correct is ArgumentCaptor.forClass

Browser other questions tagged

You are not signed in. Login or sign up in order to post.