vendredi 1 mai 2015

Autowired beans into java List, and mock it

Most of you probably will say that I should google it, since it's quite simple, but I can't find a truly correct solution that works.

There is one class that use ArrayList of specific objects that implement a interface.

The first problem is that actually in the list there is only one item.

The bean definition (There is only one bean that currently implement this interface):

@Component
@Scope("prototype") 
public class MC implements IMC 
{ ... }

The class with the @Autowired:

@Component
public class CheckM {   

    private List<IMC> MCsList;

    @Autowired
    public void setMCsList(List<IMC> MCsList)
    {
        this.MCsList = MCsList;
    } 
    ...
}

When I check MCsList there is only one item (MCsList.get(0)) that have been initialized. How can I initialize more items in the list (of course without new, but by Autowired)?

The second challenge is how can I mock this list with Mockito and inject it to CheckM for CheckM's unit-testing.

Thank you.

Aucun commentaire:

Enregistrer un commentaire