Skip to content
Tutorials Center
My learning

Topics

Groovy Tutorial

Learn Groovy step by step: 38 lessons in 9 chapters with examples, quizzes, diagrams and projects.

Start learning →

of lessons completed

Getting Started

  1. 1 What Is Groovy? Learn what Groovy is, where it came from, how it relates to Java, what it is used for (Gradle, Jenkins, scripting, testing) and how Groovy code runs on the JVM.
  2. 2 Install and Run Groovy Install Groovy with SDKMAN or a package manager, then run code with groovy, groovysh and the Groovy console. Learn scripts, shebang lines, arguments and the classpath.
  3. 3 Groovy vs Java Compare Groovy and Java side by side: boilerplate, default imports, public by default, properties, equality, closures, GStrings and the few places Java code behaves differently.

Language Basics

  1. 4 Groovy Syntax Basics Learn core Groovy syntax: scripts versus classes, optional semicolons and parentheses, comments, default imports, packages, println, the power assert and method definitions.
  2. 5 Variables and Typing in Groovy Learn Groovy variables: def, explicit types, var, script scope and the binding, multiple assignment, type coercion with as, and opt-in static checking with @TypeChecked.
  3. 6 Numbers and Math in Groovy Understand Groovy numeric types: automatic promotion, BigDecimal literals, the power operator, integer division with intdiv, number suffixes, number methods and rounding.
  4. 7 Strings and GStrings in Groovy Master Groovy strings: single, double, triple and slashy quotes, GString interpolation with $ and ${}, lazy evaluation, useful GDK methods, indexing, and regex operators.
  5. 8 Groovy Operators Learn Groovy operators: safe navigation ?., Elvis ?:, spread *., spaceship <=>, in and as, method pointers, operator overloading and the precedence rules you need.
  6. 9 Control Flow and Groovy Truth Learn Groovy conditions and loops: Groovy truth rules, if/else, ternary, the powerful switch with classifiers, for-in, while, break/continue, and iteration with each and times.

Collections and Closures

  1. 10 Groovy Lists Learn Groovy lists: literal syntax, indexing with negative numbers, slicing, adding and removing, sorting, uniqueness, flatten, spread, immutability and common mistakes.
  2. 11 Groovy Maps Learn Groovy maps: literal syntax, property and bracket access, default values with withDefault, iteration with each, entries, grouping, ordering, sorting and the GString key trap.
  3. 12 Ranges and Sets in Groovy Learn Groovy ranges (inclusive, exclusive, reverse, character, step) and sets (literals, uniqueness, union, intersection, difference, TreeSet, immutability) with examples.
  4. 13 Groovy Closures Understand Groovy closures: syntax, the implicit it parameter, multiple and default parameters, calling closures, returning them, capturing variables and passing them to methods.
  5. 14 Closure Delegate, Currying and Composition Go deeper with Groovy closures: this, owner and delegate, resolve strategies, currying and partial application, composition with << and >>, memoize and trampoline.
  6. 15 Collection Methods in Depth Master the Groovy GDK collection methods: each, collect, find, findAll, inject, any, every, groupBy, sum, min, max, sort, take, drop, partition and lazy chains, with a pipeline example.

Classes and Traits

  1. 16 Classes and Properties in Groovy Learn how to write Groovy classes (POGOs): properties versus fields, generated getters and setters, custom accessors, visibility, static members, packages, and a safe way to read fields.
  2. 17 Constructors and Named Arguments Learn how Groovy objects are created: the default map-based constructor, explicit constructors, positional arguments, list coercion with as, initializer blocks, and @TupleConstructor.
  3. 18 Inheritance and Interfaces in Groovy Learn Groovy inheritance: extends, super, method overriding, abstract classes, interfaces with default methods, duck typing, multi-methods, enums and casting with instanceof.
  4. 19 Groovy Traits Learn Groovy traits: declaring and implementing them, concrete methods, state, abstract methods, multiple traits, conflict resolution, super calls, runtime traits and when to choose traits.
  5. 20 AST Transformations and Annotations Cut boilerplate with Groovy AST transformations: @ToString, @EqualsAndHashCode, @TupleConstructor, @Immutable, @Builder, @Lazy, @Memoized, @Delegate, @Singleton, @Sortable and records.

Dynamic Features and Metaprogramming

  1. 21 The MetaClass and Dynamic Dispatch in Groovy See how Groovy decides which method to run: the MetaClass registry, runtime dispatch, respondsTo, hasProperty, and adding methods to existing classes.
  2. 22 Expando and ExpandoMetaClass in Groovy Build objects whose properties and methods are added on the fly with Expando, and extend real classes with ExpandoMetaClass, including constructors and statics.
  3. 23 methodMissing and propertyMissing in Groovy Intercept calls to methods and properties that do not exist: methodMissing, propertyMissing, invokeMethod, caching generated methods, and dynamic finders.
  4. 24 Categories and Runtime Mixins in Groovy Add methods to classes only inside a scope with Groovy categories and use(), build reusable extension modules, and compose behavior with traits and runtime mixins.

Builders, DSLs and Templates

  1. 25 Closure Delegates and Writing DSLs in Groovy Build a readable domain-specific language in Groovy with closure delegates, resolve strategies, @DelegatesTo, command chains and operator overloading.
  2. 26 MarkupBuilder, JsonBuilder and Custom Builders in Groovy Generate HTML, XML and JSON in Groovy with MarkupBuilder, StreamingMarkupBuilder and JsonBuilder, then write your own builder with BuilderSupport.
  3. 27 Parsing XML and JSON in Groovy Read XML and JSON in Groovy with XmlSlurper, XmlParser and JsonSlurper: GPath navigation, attributes, findAll, large files, type checks and safe parsing.
  4. 28 Template Engines in Groovy Generate text, emails and code from templates in Groovy: GString, SimpleTemplateEngine, GStringTemplateEngine, MarkupTemplateEngine and escaping rules.

Testing with Spock

  1. 29 Your First Spock Specification Learn Spock, the Groovy testing framework: Specification classes, given-when-then blocks, expect, thrown(), setup and cleanup, @Shared and readable failure output.
  2. 30 Data-Driven Testing with where: in Spock Run one Spock feature against many inputs with where: data tables, data pipes, multi-variable pipes, computed data, readable test names and database-style sources.
  3. 31 Mocking and Stubbing in Spock Test collaborating objects with Spock: Mock(), Stub(), Spy(), interaction counts, argument matchers, stubbed return values, ordering and verifying with then:.

Build Tools and Ecosystem

  1. 32 Gradle Build Scripts with Groovy Understand build.gradle, the Gradle Groovy DSL: plugins, dependencies, tasks, configuration versus execution, custom tasks, properties and multi-project builds.
  2. 33 Grape and Dependency Management in Groovy Scripts Use libraries from Groovy scripts without a build file: @Grab, @GrabResolver, @GrabExclude, the local Ivy cache, Maven coordinates, and when to switch to Gradle.
  3. 34 Groovy with Java and Spring Mix Groovy and Java in one project: joint compilation in Gradle, calling each way, GroovyShell and scripting, Spring Boot with Groovy, Micronaut, Grails and Jenkins.

Projects and Practice

  1. 35 Project: A Command-Line To-Do Manager in Groovy Build a complete Groovy command-line to-do manager: CliBuilder options, a Task class with @Canonical, JSON file storage with JsonSlurper, and Spock tests.
  2. 36 Project: A Log File Analyzer in Groovy Build a log analyzer in Groovy: parse access-log lines with regex and named groups, group and count with collections, report top pages and error rates, and write JSON.
  3. 37 Project: A Build-Pipeline DSL in Groovy Design and build a small pipeline DSL in Groovy with closure delegates: stages, steps, dependencies, validation, a script runner using GroovyShell, and Spock tests.
  4. 38 Groovy Practice Exercises with Solutions Twelve Groovy exercises from collections and closures to metaprogramming, builders and Spock, each with a hint, a solution and a runnable Java check of the logic.