Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

Code Block
mvn clean install -Pcargo-examples
Note
titleHint

Make sure that the cargo tests are excluded for the test phase! The have to be executed in the integration-test phase. See the example for more details.

Writing Cargo Tests

The JUnit/Cargo support module provided by CODI offers some helpers for easier testing.

Code Block
java
java
titleSimple Cargo Test with the JUnit/Cargo support-module of CODI

@RunWith(JUnit4WithCargoJUnit4.class)
public class DemoTestCase extends AbstractSimpleCargoTest
{

    // NOTE that new @Test means new WebClient means new WindowContext

    @Test
    public void checkSubmittedValueAfterNavigation() throws Exception
    {
        SimplePageInteraction pageInteraction = new SimplePageInteraction(getTestConfiguration()) //the default config
                .with(Pages.Page1.class)   //adds view-config for /pages/page1.xhtml to the current context
                .with(Pages.Page2.class)   //adds view-config for /pages/page2.xhtml to the current context
                .start(Pages.Page1.class); //start the test with /pages/page2.xhtml

        pageInteraction
                .useForm("form1") //activate form1
                .setValue("form1:input1", "1") //set a value
                .click("form1:button1"); //click on a button

        pageInteraction
                .checkState(Pages.Page2.class); //check the result - due to a redirect we have a new url - also the window-id is checked
                .checkTextValue("output1", "1"); //check the value of a text output
    }
}

...

Code Block
java
java
titleSimple Cargo Test with the JUnit/Cargo support-module of CODI and dependency injection

@RunWith(JUnit4WithCargoJUnit4.class)
public class DemoTestCase extends AbstractContainerAwareCargoTest
{
    @Inject
    private UserRepository userRepository;

    @Test
    public void testSimpleConversation()
    {
        SimplePageInteraction pageInteraction = new SimplePageInteraction(getTestConfiguration())
                .start(Pages.Overview.class)
                .useDefaultForm();

        String inputId = "userName";
        pageInteraction.setValue(inputId, "CODI")
                .clickOk()
                .checkState(Pages.Result.class);

        pageInteraction.click("home")
                .checkState(Pages.Overview.class)
                .checkInputValue(inputId, "CODI");

        pageInteraction.clickOk()
                .checkState(Pages.Result.class);

        pageInteraction.click("finish")
                .checkState(Pages.Overview.class)
                .checkInputValue(inputId, "");

        assertEquals("CODI", this.userRepository.findByUserName("CODI").getUserName());
    }
}

...

Code Block
mvn clean jetty:run-exploded -Pdebug-cargo-examples -Dmaven.test.skip=true

debug-cargo-examples is the name of the profile which is used in the example mentioned at the beginning of this page.

and afterwards it's possible to run the JUnit test in your IDE (in the debug-mode).