Although using container in unit test is categorized as an anti-pattern (, it is interesting to use pico to bootstrap your test.

The anti-pattern seems to blame when you use a real container with real dependencies for a unit tests. But I think it's acceptable when

Using a container provides multiple advantages :

Known disadvantages :

To lighten the container management in the test, a junit custom runner has been created which initialize the container and inject dependencies in the test class.

Usage of PicoRunner

PicoRunner is a simple junit custom runner which allows nice integration of picoContainer in Junit.

To get the full code of PicoRunner with examples, you can check IzPack sources.


Basically, in your test class, you define a container (implementing BindeableContainer) to use with the @Container annotation.

When Junit want to create the test class instance for each methods, the container is created and use to instanciate this test instance. Thus, your test class can behave like a component of the container and ask any dependencies it needs.

protected Object createTest() throws Exception    {
  Class<? extends BindeableContainer> containerClass =
  BindeableContainer installerContainer = getContainerInstance(containerClass);
  Object component = installerContainer.getComponent(klass);
  return component;


Simple test

The first step is to create your container for test purpose.

public class StupidTestContainer extends AbstractContainer
    * Init component bindings
    public void fillContainer(MutablePicoContainer pico)
        pico.addComponent( System.getProperties() )
            .addComponent(Mockito.mock(List.class) )

Here we have 2 components, a Properties and a mock list.

To use this container in your test, use the customRunner PicoRunner and configure the container with @Container.

You will be able to ask any components of the container in the test constructor. Moreover, an instance of the test is created for each test methods. So you don't have to worry about cleaning the container.

public class StupidTest

    private List mockedList;

    public StupidTest(Properties property, List list)
       this.mockedList = list;

    public void testList() throws Exception
       //using mock list


Panel display tests

The following examples are taken from the izpack-test-panel module which contains small and fast GUI tests

public class TestPanelDisplay

    private GUIInstallData guiInstallData;
    private FrameFixture frameFixture;
    private ResourceManager resourceManager;
    private InstallerFrame installerFrame;
    private UninstallDataWriter uninstallDataWriter;

    public TestPanelDisplay(GUIInstallData guiInstallData,
         InstallerFrame installerFrame, ResourceManager resourceManager,
         FrameFixture frameFixture, BindeableContainer container,
         UninstallDataWriter uninstallDataWriter)
        this.guiInstallData = guiInstallData;
        this.installerFrame = installerFrame;
        this.resourceManager = resourceManager;
        this.frameFixture = frameFixture;
        this.uninstallDataWriter = uninstallDataWriter;

The majority of instantiation logic is in the container. We have ready to use components and mocks for your test.

    public void htmlInfoPanelShouldDisplayText() throws Exception
        String textArea = frameFixture.textBox(;
        assertThat(textArea, StringContains.containsString("This is a test"));

    public void licencePanelShouldDisplayText() throws Exception
        String textArea = frameFixture.textBox(;
        assertThat(textArea, StringContains.containsString("This is a licenSe panel"));