concept test in category groovy

appears as: test, tests, test, tests, The tests, The test
Groovy in Action, Second Edition

This is an excerpt from Manning's book Groovy in Action, Second Edition.

  • Put your assumptions to the test. Write some assertions or unit tests to test your regex against static strings. Please don’t send us any more flowers for this advice; a tweet like “Assertions saved my life today! Thanks #ReGina.” will suffice.
  • Next, we follow the common unit-testing practice of writing a method to set up the variables we’ll need in the tests that follow:

    class CounterTest extends GroovyTestCase {
        private counter
        void setUp() {
            counter = new Counter()
        }
        ...
    }

    We’re now in a position to write a test:

    void testCounterWorks() {
      assertEquals(2, counter.biggerThan([5, 10, 15], 7))
    }

    We could continue adding tests in this way, but first let’s introduce constants that capture useful boundary case data and refactor out a helper method:

    We can run the tests to make sure they all still work and then check the coverage again to see how our coverage is going. The result is shown in figure 17.3.

    Figure 17.3. Cobertura code-coverage file report showing full coverage

    Is the code correct? The tests all pass, and we have 100% coverage—that means we don’t have any bugs, right? For fun, let’s add one more test:

    The test described in the previous section is a simple one. In the real world, the class you’re testing usually has dependencies on other classes that can make testing often a bit trickier. Luckily, Spock provides excellent mocking[21] support.

    A common approach with unit testing is to call the subject under test with static sample data that produces well-known results. That way, you can compare the calculated results against your expectations.