Versions Compared

Key

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

...

Executing Cargo Tests with Maven

The example above has no maven profiles - that means the cargo tests are executed automatically during a maven build likea special maven profile for cargo tests:

Code Block
mvn clean install -Pcargo-examples

Writing Cargo Tests

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

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

@RunWith(JUnit4WithCargo.class)
public class DemoTestCase extends AbstractCargoTestAbstractSimpleCargoTest
{

    // 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
    }
}

In this case there is no CDI container bootstrapped for the test. So it's required to register view-configs manually.

Writing Cargo Tests with Dependency Injection.

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


@RunWith(JUnit4WithCargo.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());
    }
}

The availability of dependency injection means that a CDI container is bootstrapped for every test-method. Therefore, it isn't required to register artifacts like view-configs manually.

Note
titleHint

For debugging tests it's important to understand that the test doesn't run in the same VM as the running application which is under test. That also means, if you use an in-memory database you can't inject e.g. a DAO to check the stored result. You have to use a permanent DB or you have to load and check a page which provides the result.

Debugging Cargo Tests

Start Jetty

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

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