Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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.

Code Block

@Override
protected Object createTest() throws Exception    {
  Class<? extends BindeableContainer> containerClass = getTestClass().getJavaClass().getAnnotation(Container.class).value();
  BindeableContainer installerContainer = getContainerInstance(containerClass);
  installerContainer.initBindings();
  installerContainer.addComponent(klass);
  Object component = installerContainer.getComponent(klass);
  return component;
}

...

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

Code Block

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

...

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.

Code Block

@RunWith(PicoRunner.class)
@Container(StupidTestContainer.class)
public class StupidTest
{

    private List mockedList;

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

    @Test
    public void testList() throws Exception
    {
       //using mock list
       mockedList.add("one");
       mockedList.clear();

        //verification
       verify(mockedList).add("one");
       verify(mockedList).clear();
    }  
 
}

Panel display tests

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

Code Block

@RunWith(PicoRunner.class)
@Container(TestPanelContainer.class)
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.

Code Block

    @Test
    public void htmlInfoPanelShouldDisplayText() throws Exception
    {
        addPanelAndShow("com.izforge.izpack.panels.htmlinfo.HTMLInfoPanel");
        String textArea = frameFixture.textBox(GuiId.HTML_INFO_PANEL_TEXT.id).text();
        assertThat(textArea, StringContains.containsString("This is a test"));
    }

    @Test
    public void licencePanelShouldDisplayText() throws Exception
    {
        addPanelAndShow("com.izforge.izpack.panels.licence.LicencePanel");
        String textArea = frameFixture.textBox(GuiId.LICENCE_TEXT_AREA.id).text();
        assertThat(textArea, StringContains.containsString("This is a licenSe panel"));
    }