# Setting up a Java project

This guide shows you how to configure a Java project to run on TURBO TEST. Here's an overview of the process:

It should take around 15 minutes to install and configure your project.

# Create a bash script to execute one test file

Firsly, create a bin/mvn_turbotest file in your repository root:

# cd $REPOSITORY_LOCAL_DIRECTORY
mkdir bin
touch bin/mvn_turbotest
chmod +x bin/mvn_turbotest

Then create this bash script to execute one test from a java file path:

#!/bin/bash

# Extract package and class names from file path
class_name=$(basename "$1" .java)
package_name=$(dirname "$1" | sed 's,^src/test/java/,,; s,/,.,g')

# Construct a pattern that matches the fully qualified class name
pattern="${package_name}.${class_name}"

# Run Maven test with the constructed pattern
mvn test -Dtest="${pattern}"

# Create configuration file

Create a .turbo_test.toml file in your repository root if you haven't already done so. For example, execute these commands from your command line terminal:

# cd $REPOSITORY_LOCAL_DIRECTORY
touch .turbo_test.toml

# Install Ubuntu 22.04.3 LTS packages

Copy the Java configuration below into .turbo_test.toml.

.turbo_test.toml
# The top level env specifies environment variables available globally.
# Environment variables are specified as key/value pairs.
[env]
RAILS_ENV=test

# A string specifying the command which executes one test file.
#
# This is where you install required programming languages, databases and other
# supporting libraries.
#
# Your repository source code is available in the /src directory.
#
# The installation is cached as a virtual machine image for subsequent test
# runs. When the install section changes, the virtual machine image is
# re-created.
#
# Installation commands are specified as a TOML multi-line string.
#
# N.B. Shell commands MUST execute non-interactively. For example, commands
# requiring user input will hang until the test run is cancelled or after a
# 15 minute timeout.
#
# You can find an overview of the configuration, packages and services available
# in the TURBO TEST Ubuntu 22.04 LTS environment here:
# http://turbo-test.com/installing_dependencies/ubuntu_2204_lts_environment

[base_image]
commands = '''
sudo apt update -y

# Install extra packages not already included in the base image
# sudo DEBIAN_FRONTEND=noninteractive apt install --no-install-recommends -yq \
#   EXTRA_APT_PACKAGES

# Install sdkman
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"

# Install specific Java version (e.g., OpenJDK 11)
sdk install java 17.0.1.temurin
sdk use java 17.0.1.temurin

sudo apt install maven -yq
mvn --batch-mode --update-snapshots verify

# (!) Re-build the operating system image by adding a adding a new UUID:
# clear-cache: 45f09fa5-5431-4319-8081-218fe9debf9e
'''

[cache]
directories = [ "~/.m2/repository" ]

# The before_test_run definition specifies commands to execute before running
# your test runs.
#
# Unlike the base_image definition, before_test_run is not cached and executes
# on every test run.
#
# Use this definition to:
# - update project dependencies
# - compile classes
# - start operating system services
[before_test_run]
commands = '''
# Uncomment services needed for your project:
# sudo systemctl start elasticsearch
# sudo systemctl start memcached
# sudo systemctl start mysql
# sudo systemctl start postgresql@15-main.service
# sudo systemctl start postgresql@16-main.service
# sudo systemctl start rabbitmq
# sudo systemctl start redis

mvn --batch-mode --update-snapshots verify

# Run migrations. For example:
# mvn flyway:migrate
'''

# Test runs are inferred from each TOML key starting with test_run.
#
# Specify multiple test runs by adding test_run keys. For example:
# [test_run."system tests"]
# [test_run.integration]
#
# Create a default test_run if you only require one test run.
[test_run.default]

# A string specifying the command which executes one test file.
#
# The test file is passed as the last argument to the command.
command = "bin/mvn_turbotest"

# An array of file path globs which expands to a list of files. File paths are
# relative to the project directory.
#
# Each file is passed as the last argument to your test command.
# Default Rails test
files = ["src/test/java/**/*.java"]

# An array of file path globs which expands to a list of files. File paths are
# relative to the project directory.
#
# No tests will be executed with any files matched in this definition section.
ignore = [
  "test/models/IGNORE_test.rb"
]

# Create additional test runs when the command or files are different to the
# default test run. For example:
#
# [test_run.features]
# command = "mvn_turbotest"
# files = ["src/test/java/features/**/*.java"]

Before you push your configuration to GitHub, verify your configuration locally and add required secrets from the web UI:

Verify Configuration Locally
../../configuration/verify_locally/

Add Secrets
../../configuration/managing_secrets/