concept test in category griffon

appears as: tests, test, test, tests
Griffon in Action

This is an excerpt from Manning's book Griffon in Action.

  • What about tests?
  • The remaining code of the test resets the application’s state by changing property values in the model. It may seem unimportant at this point, because you only have a single test; but this will become a key aspect for additional tests, because the same application instance is shared across all integration tests. This means tests must be good citizens and clean up after themselves—otherwise you could end up with a red bar caused by a false result.

    You’ll fill out the code for the test first; this means you’ll get a failing test as a baseline. Then you can fill in the blanks as much as possible to make the test turn green. With a new test state, you can safely refactor the production code: as long as the tests stay green, you’re doing it right. Your first attempt verifies some error conditions triggered by insufficient or wrong input, as shown in the following listing.

    Listing 9.1. Testing for error outputs in DictionaryService

    Before you run the test, you have to define two constants and one method in your service. Locate the DictionaryService.groovy file under griffon-app/services/dictionary, and open it in your editor. Make sure its contents resemble the following listing.

    Listing 9.2. Adding enough code to DictionaryService to turn the test green
    package dictionary class DictionaryService { static String INPUT_ERROR_MESSAGE = "Please enter a valid word" static String FIND_ERROR_MESSAGE = "Word doesn't exist in dictionary" String findDefinition(String word) { null } }

    Now you can run the test and see it fail. Execute the test-app command, targeting the unit phase and type; in this way, you avoid running the default integration test created when the application structure was initialized. After the code has been compiled and the test has been run, you should see output similar to the following (minus the power assert and exception information) in your console:

    -------------------------------------------------------
    Running 2 unit tests...
    Running test dictionary.DictionaryServiceTests...
                        testServiceCantFindWord...FAILED
                        testInvalidInput...FAILED
    Tests Completed in 419ms ...
    -------------------------------------------------------
    Tests passed: 0
    Tests failed: 2
    -------------------------------------------------------

    You knew the test was bound to fail from the start. Next you’ll fix the code and make the test succeed.