Free preview.You're sampling one lesson — enroll free to unlock all 8 lessons and track your progress.
Enroll free
lesson

Why and What to Test

Why and What to Test

In this lesson — part of Testing Fundamentals — you'll learn why and what to test in Groovy and why it matters in real work.

Why it matters

Tests prove your code works and keep it working as you change it.

Key ideas

  • What to test
  • Arrange-Act-Assert
  • Running a test suite
  • Good vs. brittle tests

In practice

Here's how it looks in idiomatic Groovy:

// Spock specification (the de-facto Groovy test framework)
import spock.lang.Specification

class MathSpec extends Specification {
    def "adding #a and #b gives #c"() {
        expect: a + b == c
        where:
        a | b | c
        1 | 2 | 3
        5 | 5 | 10
    }
}

Groovy note: Idiomatic Groovy testing uses Spock, whose given/when/then/expect/where blocks and data-driven where: tables (with #token placeholders in the method-name) are powered by Groovy AST transforms, not plain JUnit.

Try it yourself

Exercise: In Groovy, write three tests for a function that reverses a string.

Recap

You now understand why and what to test and can apply it in Groovy. Mark this lesson complete and continue to the next one.