You are here
Home > Groovy >

How to Write Groovy Code in STS: Groovy Support in STS to Create Spock Tests

How to Write Groovy Code in STS: Install Groovy Support in STS and Create Spock Tests

How to inplement Groovy support in STS using Groovy-Eclipse and create Spock tests.
Step-by-step guide to installing Groovy support in STS and writing Spock tests

If you are a Java developer working with Spring Boot and Spock Framework, you might encounter an unexpected problem while writing your first Spock test: Spring Tool Suite (STS) does not recognize Groovy files properly.

You may be able to add Groovy dependencies to your Maven project, execute Maven commands, and configure Spock Framework. However, when you try to create a ‘.groovy’ file in STS, you might not get proper syntax highlighting, code completion, error detection, or a dedicated Groovy editor.

This happens because Spring Tool Suite is primarily an Eclipse-based development environment with Java and Spring tooling. Groovy language support is provided separately through the Groovy-Eclipse plugin.

The good news is that you do not need to switch your IDE or rewrite your existing Java application.

By installing the appropriate Groovy-Eclipse plugin and configuring your project correctly, you can write and maintain Groovy test cases directly inside STS.

In this tutorial, we will configure Groovy support in Spring Tool Suite step by step. We will also create a Groovy test class and write a practical Spock test against an existing Java service.

If you have not configured Spock Framework in a Spring Boot project yet, you can first refer to our companion tutorial: Spock Framework with Spring Boot: A Practical Java Testing Guide.

Let’s get started.

Table of Contents

Why Can’t We Write Groovy Code Directly in STS?

Before installing anything, it is important to understand why this problem occurs.

Groovy is a JVM-based programming language. Although it is closely integrated with Java, Groovy is not simply another Java syntax.

For example, consider the following Java class:

public class Employee {

    private String name;

    public Employee(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

The equivalent Groovy class can be considerably shorter:

class Employee {

    String name

    Employee(String name) {
        this.name = name
    }
}

Groovy provides its own language syntax, compiler, and runtime.

STS provides excellent Java and Spring development facilities, but you need additional language tooling to work comfortably with Groovy source files. This is where Groovy-Eclipse comes into the picture.

Groovy-Eclipse provides Groovy Development Tools (GDT) for Eclipse-based IDEs.

It integrates Groovy development with Eclipse’s Java Development Tools (JDT) and provides facilities such as:

  • Groovy editor and syntax highlighting.
  • Code completion and navigation.
  • Groovy project support.
  • Compilation and error detection.
  • Integration with Java projects.
  • Support for mixed Java and Groovy projects.

Therefore, installing Groovy-Eclipse is the first important step toward writing Groovy code in STS.

Prerequisites

Before proceeding, make sure you have the following:

Requirement Purpose
Spring Tool Suite (STS) IDE for Spring Boot and Java development
JDK Required for Java and Groovy development
Maven Manages project dependencies and compilation
Existing Spring Boot project We will use this project for demonstration
Groovy-Eclipse plugin Provides Groovy development support in STS

You do not need to create a new Spring Boot application. We will use an existing Maven-based Spring Boot project containing Java application classes and Spock dependencies. For this tutorial, we will follow the project structure used in our previously published Spock Framework tutorial.

Which versions of STS and Groovy-Eclipse should you use?

Version compatibility is particularly important when installing Eclipse plugins.

Groovy-Eclipse is distributed through update sites corresponding to particular Eclipse platform versions. Installing a plugin intended for an incompatible Eclipse release can result in installation or dependency-resolution errors.

For reference, the current releases at the time of writing are:

Component Version
Spring Tools for Eclipse 5.4.0
Eclipse platform 2026-09 (4.41)
Groovy-Eclipse 6.3.0
Groovy compiler supported by Groovy-Eclipse 6.3.0 6.0.0

Groovy-Eclipse 6.3.0 supports Eclipse 4.41 and includes Groovy 6.0.0 as its default bundled compiler.

This is particularly useful when working with a modern Spock project using Groovy 6.

Important: These versions are a reference, not a requirement to upgrade your existing STS installation. Always choose the Groovy-Eclipse update site compatible with your installed Eclipse platform.

Official release information: https://github.com/groovy/groovy-eclipse/wiki/6.3.0-Release-Notes

Identify Your STS/Eclipse Version

Before installing Groovy-Eclipse, identify the Eclipse platform version on which your STS installation is based.

Follow these steps:

Step#1: Open Spring Tool Suite.

Step#2: Navigate to:

Help → About Spring Tool Suite

Step#3: Open Installation Details if you need to inspect the installed platform features.

Identify the Eclipse Platform version from the installed software information.

For example, Spring Tools 5.4.0 is based on Eclipse 2026-09, which corresponds to Eclipse platform version 4.41.

You will use this information to select the correct Groovy-Eclipse update site.

Here are some examples of compatible release update sites:

Eclipse platform Update site
4.41 https://groovy.jfrog.io/artifactory/plugins-release/e4.41
4.40 https://groovy.jfrog.io/artifactory/plugins-release/e4.40
4.39 https://groovy.jfrog.io/artifactory/plugins-release/e4.39
4.38 https://groovy.jfrog.io/artifactory/plugins-release/e4.38
4.37 https://groovy.jfrog.io/artifactory/plugins-release/e4.37

For other versions, refer to the official compatibility list: https://github.com/groovy/groovy-eclipse/wiki/

My recommendation: Do not randomly select an update site. Always match the update site with the Eclipse platform version installed in your STS.

Install Groovy-Eclipse Plugin in STS

Now let’s install the Groovy-Eclipse plugin.

This is the most important step because it enables STS to understand and work with Groovy source files.

Step#1: Open Install New Software

Launch STS and navigate to:

Help → Install New Software…

This opens the Available Software installation window.

Step#2: Add the Groovy-Eclipse Update Site

Click the Add button. You will see two fields: Name & Location

Enter the following details if you are using Eclipse 4.41:

Name: Groovy-Eclipse 6.3.0

Location: https://groovy.jfrog.io/artifactory/plugins-release/e4.41

Click Add.

STS will connect to the update site and retrieve the available Groovy-Eclipse components.

For other Eclipse versions, replace e4.41 with the corresponding compatible update site.

Step#3: Select Groovy Development Tools

After the update site loads, locate the following feature:

Main Package
    Eclipse Groovy Development Tools

Select the Eclipse Groovy Development Tools checkbox. This is the main feature you need for Groovy development.

Click Next.

Step#4: Review and Install

STS will display the components that will be installed. Review the selected components and proceed by clicking Next.

Accept the license agreement when prompted. Click Finish to begin the installation.

Depending on your internet connection and the IDE configuration, installation may take a few minutes.

Step#5: Restart STS

Once installation completes, STS may ask you to restart the IDE. Choose Restart Now.

Restarting is important because the newly installed Groovy tooling needs to be loaded into the Eclipse environment.

Congratulations! You have now installed Groovy development support in STS.

Convert Your Existing Spring Boot Project into a Groovy Project

Installing the plugin does not necessarily mean that every existing Java project has already been configured with Groovy project support.

For an existing Maven-based Spring Boot application, you should also enable the Groovy project nature.

Follow these steps.

Step#1: Open Project Explorer.

Step#2: Locate your existing Spring Boot Maven project.

Step#3: Right-click the project.

Step#4: Navigate to:

Configure → Convert to Groovy Project

Select this option. STS will add the Groovy project nature to your existing Java project.

What does this mean?

A Groovy project nature enables Eclipse’s Java builder to process Groovy source files alongside Java source files in configured source folders.

You can now work with both Java and Groovy code within the same project.

For example:

spring-boot-spock-demo
│
├── src
│   ├── main
│   │   └── java
│   │       └── com.example.catalog
│   │
│   └── test
│       └── groovy
│           └── com.example.catalog
│
└── pom.xml

Your production classes remain in Java, while your Spock test classes are written in Groovy.

Note: If your project already has Groovy nature, you do not need to convert it again.

Create a Groovy Source Folder

Now we need to create a dedicated folder for Groovy test files.

In a Maven project, the recommended location for Groovy test source files is:

src/test/groovy

The standard Maven project structure separates production code from test code.

For example:

src/main/java       → Java production classes
src/test/java       → Java test classes
src/test/groovy     → Groovy test classes

Follow these steps to create the Groovy folder.

Step#1: Right-click your existing project’s ‘src/test’ directory.

Step#2: Select: New → Folder

Step#3: Enter the folder name: ‘groovy’

Click Finish.

Your project structure should now look like this:

src
├── main
│   └── java
│
└── test
    ├── java
    └── groovy

If STS does not recognize this folder as a source folder, check the Java Build Path settings.

Navigate to:

Right-click Project
    → Properties
    → Java Build Path
    → Source

Check whether ‘src/test/groovy’ is registered as a source folder. Add it if necessary.

For Maven projects, prefer configuring source-folder recognition through the Maven project configuration wherever possible, so that project updates do not overwrite manual build-path changes.

Create Your First Groovy File in STS: Groovy Support in STS

Now comes the step we were actually trying to accomplish: writing Groovy code inside STS.

After installing Groovy-Eclipse and configuring your project, you should be able to create Groovy source files.

Let’s create a simple Groovy class.

Option#1: Create a Groovy Class Using the New Wizard

Step#1: Right-click the newly created ‘src/test/groovy’ folder.

Step#2: Navigate to: New → Other…

Step#3: Expand the Groovy category.

Step#4: Select Groovy Class. Click Next.

Step#5: Enter the package name: “com.example.catalog.service”

Step#6: Enter the class name: Employee

Click Finish.

STS should generate a new file named: Employee.groovy

Option#2: Create a Groovy File Manually

If the Groovy Class wizard is unavailable, you can create a regular file with the ‘.groovy’ extension.

For example: Employee.groovy

The important point is that the file must have the correct .groovy extension.

After installing Groovy-Eclipse, you can open the file using:

Right-click File
    → Open With
    → Groovy Editor

This is another way to confirm whether Groovy-Eclipse has been installed properly.

Write Your First Groovy Program

Enter the following code into Employee.groovy:

package com.example.catalog.service

class Employee {

    String name
    int age

    Employee(String name, int age) {
        this.name = name
        this.age = age
    }

    String getEmployeeDetails() {
        return "Name: ${name}, Age: ${age}"
    }
}

Notice a few interesting things:

  1. We do not need to write public access modifiers explicitly for ordinary Groovy classes and methods.
  2. Groovy provides property syntax, which automatically supports property access.
  3. String interpolation is supported using ${}.
  4. Semicolons are optional.
  5. We can write Groovy code in a familiar object-oriented style without following all Java syntax requirements.

You should now be able to edit Groovy files directly within STS.

Write Your First Spock Test in STS

Now that Groovy support is available, let’s use it for the purpose we originally intended: writing Spock tests for our Spring Boot application.

We will use the Product Inventory application from our companion Spock tutorial.

In that application, we already have a Java class named: ‘ProductService.java’

It contains business operations such as retrieving a product and reserving stock.

We will write a Groovy specification to test the service.

Step#1: Create the Spock Specification

Navigate to: src/test/groovy

Create the package: com.example.catalog.service

Create a Groovy class named: ProductServiceSpec.groovy

The file structure should look like this:

src
└── test
    └── groovy
        └── com
            └── example
                └── catalog
                    └── service
                        └── ProductServiceSpec.groovy

Step#2: Extend the Spock Specification Class

Every ordinary Spock specification should extend: spock.lang.Specification

This base class provides the fundamental Spock testing capabilities. Add the following imports:

package com.example.catalog.service

import com.example.catalog.model.Product
import com.example.catalog.repository.ProductRepository
import spock.lang.Specification

import java.math.BigDecimal
import java.util.Optional

Step#3: Write the Test Case

Let’s write a test that verifies whether ProductService correctly returns a product when the product exists.

class ProductServiceSpec extends Specification {

    ProductRepository productRepository = Mock()

    ProductService productService =
            new ProductService(productRepository)

    def "should return product when product exists"() {

        given:
        def product = new Product(
                1L,
                "Mechanical Keyboard",
                new BigDecimal("3499.00"),
                12
        )

        when:
        def result = productService.getProduct(1L)

        then:
        1 * productRepository.findById(1L) >>
                Optional.of(product)

        result.id() == 1L
        result.name() == "Mechanical Keyboard"
        result.availableQuantity() == 12
    }
}

Let’s understand what we have written.

1. Specification

class ProductServiceSpec extends Specification

This makes the class a Spock specification.

2. Mock Object

ProductRepository productRepository = Mock()

Spock provides built-in mocking functionality.

We do not need to create a Mockito mock for this particular test.

3. Given Block

given:
def product = new Product(...)

This block prepares the test data.

4. When Block

when:
def result = productService.getProduct(1L)

This executes the actual business operation.

5. Then Block

then:
1 * productRepository.findById(1L) >>
        Optional.of(product)

This defines the expected repository interaction.

It means that findById(1L) must be called exactly once and should return the specified product. The remaining statements verify the returned product details. This is a practical example of using Groovy syntax to test an existing Java class.

You do not need to convert ProductService.java, Product.java, or any other production classes into Groovy.

You can continue developing your entire application in Java while writing your tests in Groovy.

Configure Maven to Compile Groovy Test Files

One important thing to understand is that Groovy-Eclipse installation and Maven Groovy compilation are two different configurations.

Installing the Groovy-Eclipse plugin enables your IDE to understand Groovy files. However, Maven must also know how to compile Groovy source files during the build process.

If you followed our previous Spock Framework tutorial, you should already have the GMavenPlus plugin configured in your pom.xml.

For reference, the configuration looks like this:

<build>
    <plugins>

        <plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <version>4.2.1</version>

            <executions>
                <execution>
                    <goals>
                        <goal>addTestSources</goal>
                        <goal>compileTests</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

Here is what these goals do:

Goal Purpose
addTestSources Registers Groovy test source files with Maven
compileTests Compiles Groovy test source files

The default test source location used by GMavenPlus is: src/test/groovy

Therefore, our newly created Groovy test class can be picked up by the Maven build. You should also ensure that the project has compatible Groovy and Spock dependencies.

For example, the dependency versions used in our companion tutorial are based on Spock 2.5 with Groovy 6.0:

<properties>
    <groovy.version>6.0.0</groovy.version>
    <spock.version>2.5-groovy-6.0</spock.version>
</properties>

The Groovy runtime dependency and the Spock variant must be compatible. Do not mix different Groovy major versions without checking their compatibility. For complete Maven dependency configuration, refer to the Spock Framework with Spring Boot tutorial.

Update the Maven Project in STS

After making changes to the pom.xml, refresh the project configuration.

Follow these steps:

  1. Right-click your Spring Boot project.
  2. Select Maven.
  3. Click Update Project.
  4. Select your project.
  5. Click OK.

Alternatively, use the shortcut: Alt + F5

This allows STS to refresh the Maven dependencies and project configuration.

Run Your First Spock Test

Now we are ready to execute the test. The safest initial verification is to run the Maven test lifecycle. Open the STS Terminal or your system command prompt.

Navigate to the project’s root directory and execute: mvn clean test

Maven should perform the following operations:

  1. Clean the existing build output.
  2. Resolve the configured dependencies.
  3. Register the Groovy test source folder.
  4. Compile Groovy test classes.
  5. Execute the tests through the configured test infrastructure.

If everything is configured correctly, your ProductServiceSpec should be compiled and executed.

You can also execute Maven from within STS:

Right-click Project
    → Run As
    → Maven build...

Enter the goal: clean test

Click Run.

Expected outcome: Your Spock test should execute successfully, confirming that your STS project structure, Groovy source files, and Maven configuration are working together.

If the test is not discovered, check the Surefire configuration in your Maven project. For example, the previous tutorial configures discovery of classes ending with Spec.

Remember that running successfully through Maven is a stronger initial check than relying only on the IDE’s editor or launcher.

Common Problems and Their Solutions

Even after installing Groovy-Eclipse, you might encounter some issues while working with Groovy files. Let’s look at the most common problems and their solutions.

Problem#1: Groovy Class Option Is Not Available in STS

Possible reason:

The Groovy-Eclipse plugin might not have been installed correctly, or STS might not have been restarted.

Solution:

  • Open Help → Installation Details.
  • Verify that Groovy Development Tools are installed.
  • Restart STS.
  • Check whether the project has Groovy nature enabled.

You can also create a .groovy file manually and open it using Groovy Editor.

Problem#2: STS Opens .groovy Files as Plain Text

Possible reason:

The file association is not configured correctly.

Solution:

Right-click the Groovy file and select:

Open With → Groovy Editor

If required, check:

Window
    → Preferences
    → General
    → Content Types

Verify that Groovy source files are recognized by the IDE.

Problem#3: Groovy Classes Are Showing Compilation Errors

Possible reason:

The Groovy runtime classes might not be available on the project’s build path.

Solution:

First, check whether your Maven project includes a compatible Groovy dependency.

For example:

<dependency>
    <groupId>org.apache.groovy</groupId>
    <artifactId>groovy</artifactId>
    <scope>test</scope>
</dependency>

The version should be managed consistently with your Spock configuration.

If you are working with a non-Maven Groovy project, Groovy-Eclipse also provides a way to add Groovy Runtime Libraries through the project’s Java Build Path.

For Maven projects, prefer using Maven-managed dependencies rather than adding a second, potentially incompatible Groovy runtime manually.

Problem#4: Maven Is Not Compiling Groovy Test Files

Possible reason:

Maven does not automatically compile arbitrary Groovy source files merely because Groovy dependencies are present.

Solution:

Verify the following:

  • Your Spock files are inside src/test/groovy.
  • GMavenPlus is configured correctly.
  • The addTestSources goal is configured.
  • The compileTests goal is configured.
  • Maven has been refreshed after modifying the pom.xml.

Then execute: mvn clean test

Problem#5: Spock Tests Are Not Discovered

Possible reason:

Maven Surefire might not be configured to recognize your Spock specification classes.

Solution:

Verify that your Spock test class follows the expected naming convention.

For example: ProductServiceSpec.groovy

If your Maven configuration includes:

<includes> 
   <include>**/*Spec.class</include> 
</includes>

Your specification should follow the *Spec naming convention.

Also verify that the Spock JUnit Platform engine and Maven test infrastructure are correctly configured.

Problem#6: Different Errors Appear in STS and Maven

Possible reason:

Your IDE compiler configuration and Maven compiler configuration may be using different Groovy versions or settings.

Solution:

Check the following:

  1. The Groovy version selected by Groovy-Eclipse.
  2. The Groovy dependency version in pom.xml.
  3. The Groovy variant used by your Spock dependency.
  4. The Eclipse platform compatibility of your Groovy-Eclipse plugin.

Use compatible versions across the IDE and build environment.

For example, the current Groovy-Eclipse 6.3.0 release supports Groovy 6.0.0, which is particularly relevant to projects using the Groovy 6 variant of Spock 2.5.

Problem#7: Project Shows Errors After Installing the Plugin

Possible reason:

The IDE may need to refresh its project configuration after installing new tooling.

Solution:

Try the following sequence:

Right-click Project
    → Maven
    → Update Project

Then:

Project
    → Clean

If errors remain, inspect the Problems view and verify that the installed plugin matches your Eclipse platform version.

Do not immediately upgrade or downgrade your Java and Groovy dependencies without identifying the actual compatibility problem.

Best Practices for Writing Groovy Code in STS

Once Groovy support is enabled, follow these recommendations to maintain a clean Java and Groovy development environment.

1. Keep production code in Java

If your application is already developed in Java, there is no need to convert it into Groovy just because you have adopted Spock.

Keep production code in Java and use Groovy for expressive test cases.

2. Maintain separate test source folders

Use the following standard structure:

src/main/java
src/test/java
src/test/groovy

This makes it easier for developers to understand which language is used for each purpose.

3. Use descriptive Spock specification names

Prefer:

ProductServiceSpec.groovy
EmployeeServiceSpec.groovy
OrderServiceSpec.groovy

Descriptive names make test discovery and maintenance easier.

4. Keep Groovy and Spock versions compatible

Do not select arbitrary Groovy and Spock versions.

Use the correct Spock artifact variant for your chosen Groovy version.

5. Verify your tests through Maven

Always verify that your tests execute through: mvn clean test

This ensures that the tests work with the project’s actual build configuration rather than only appearing correct inside the IDE.

6. Avoid unnecessary IDE and runtime configurations

Use Groovy-Eclipse for IDE support and Maven dependencies for project runtime and compilation requirements.

Avoid introducing multiple conflicting Groovy runtime versions into the same project.

Frequently Asked Questions (FAQs)

Q1. Can we write Groovy code in Spring Tool Suite?

Yes. You can write Groovy code in STS by installing the Groovy-Eclipse plugin.

It provides Groovy editor support, syntax highlighting, code assistance, and integration with Eclipse Java tooling.

Q2. Is Groovy-Eclipse required to use Spock Framework?

No. Groovy-Eclipse is not mandatory for compiling and executing Spock tests through Maven.

You can use other editors and run your tests through Maven or Gradle.

However, Groovy-Eclipse is highly useful if you want to write and maintain Groovy code comfortably inside STS.

Q3. Do we need to convert our Java Spring Boot application into Groovy?

No. You can continue writing your production application entirely in Java.

Groovy is required for Spock specifications, but your business services, repositories, entities, and controllers can remain unchanged.

Q4. Where should we create Groovy test files in a Maven project?

The recommended location is: src/test/groovy

This separates Groovy tests from production Java code and standard Java test classes.

Make sure your Maven configuration registers and compiles this source folder.

Q5. Can we use Java and Groovy in the same Spring Boot project?

Yes. Java and Groovy can coexist in the same project. Groovy-Eclipse supports mixed Java and Groovy projects, while Maven or Gradle can be configured to compile the required source files.

This makes Spock an attractive option for existing Java applications.

Q6. Is a separate Groovy installation required on Windows?

Not necessarily.

For Groovy development inside STS, the Groovy-Eclipse plugin provides the IDE tooling. For Maven-based applications, the required Groovy runtime can be managed through Maven dependencies.

A standalone Groovy installation is useful for other activities, such as executing Groovy scripts from a terminal, but it is not a prerequisite for this tutorial.

Q7. Can we execute Spock tests directly from STS?

Yes, provided your IDE’s JUnit Platform integration and project configuration support the Spock engine.

You can also execute tests through the Maven integration in STS.

For initial troubleshooting, Maven execution is a reliable way to verify the complete build configuration.

Conclusion

Writing Groovy code in Spring Tool Suite becomes straightforward once the correct language tooling is installed and configured.

The primary requirement is the Groovy-Eclipse plugin, which extends Eclipse’s Java development environment with Groovy language support.

In this tutorial, we covered the complete setup, including:

  • Identifying the correct Eclipse platform version.
  • Installing the compatible Groovy-Eclipse plugin.
  • Converting an existing Java project into a Groovy project.
  • Creating the src/test/groovy source folder.
  • Creating and editing .groovy files inside STS.
  • Writing a practical Spock specification for a Java service.
  • Configuring Maven to compile and execute Groovy tests.
  • Troubleshooting common installation and compilation problems.

The most important takeaway is that you do not need to abandon your existing Java development workflow to adopt Spock Framework.

You can continue using Spring Tool Suite for Spring Boot development, retain Java for production code, and use Groovy exclusively for expressive and maintainable test cases.

Once Groovy-Eclipse and Maven are configured properly, you can develop your Java application and Spock test specifications within the same STS workspace.


Official Documentation and References

  1. Groovy-Eclipse Official Documentation and Installation Guide
  2. Groovy-Eclipse 6.3.0 Release Notes
  3. Groovy-Eclipse Catalog of Features
  4. GMavenPlus Plugin Documentation
  5. Spock Framework Reference Documentation
  6. Spring Tools for Eclipse

 

Leave a Reply



Top