'groovy_test reports could not find class

I run into some problems when trying to use rules_groovy.

Minimal steps to reproduce:

git clone https://github.com/Vertexwahn/BazelDemos
cd BazelDemos
cd GroovyDemo
bazel test //...

In the case you have not installed Bazel you can installed it on a Ubuntu 20.04 machine this way:

echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
sudo apt-get update && sudo apt-get install -y bazel

The problem:

When I run bazel test //... I get the following error:

JUnit version 4.12
.E
Time: 0.001
There was 1 failure:
1) initializationError(org.junit.runner.JUnitCommandLineParseResult)
java.lang.IllegalArgumentException: Could not find class [HelloWorldTest]
    at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:102)
    at org.junit.runner.JUnitCommandLineParseResult.parseArgs(JUnitCommandLineParseResult.java:50)
    at org.junit.runner.JUnitCommandLineParseResult.parse(JUnitCommandLineParseResult.java:44)
    at org.junit.runner.JUnitCore.runMain(JUnitCore.java:72)
    at org.junit.runner.JUnitCore.main(JUnitCore.java:36)
Caused by: java.lang.ClassNotFoundException: HelloWorldTest
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:398)
    at org.junit.internal.Classes.getClass(Classes.java:16)
    at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:100)
    ... 4 more

FAILURES!!!
Tests run: 1,  Failures: 1

Details to setup:

My setup looks like this:

.bazelverion

5.1.0

WORKSPACE.bazel

# rules_groovy
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "io_bazel_rules_groovy",
    sha256 = "a0714e766ba6584432a85697849b2b4f8f56ac634777441d8854a122f07798d2",
    strip_prefix = "rules_groovy-e110fbce55bad3b2db067198f339e719244f91b7",
    url = "https://github.com/bazelbuild/rules_groovy/archive/e110fbce55bad3b2db067198f339e719244f91b7.tar.gz",
)

load("@io_bazel_rules_groovy//groovy:repositories.bzl", "rules_groovy_dependencies")

rules_groovy_dependencies()

src/test/groovy/BUILD.bazel

load("@io_bazel_rules_groovy//groovy:groovy.bzl", "groovy_test")

groovy_test(
    name = "HelloWorldTest",
    srcs = ["HelloWorldTest.groovy"],
)

src/test/groovy/HelloWorldTest.groovy

package src.test.groovy

import groovy.util.GroovyTestCase

class HelloWorldTest extends GroovyTestCase {
    void testAssertions() {
        assertTrue(1 == 1)
    }
}

It reports that class HelloWorldTest can not be found. Any hints what I am doing wrong here? What do I need to change to get a working Groovy unit test working?

My expectation is that the unit test succeeds.

(BTW: I am using Ubuntu 20.04)

EDIT:

I also tried to make use of groovy_junit_test by modifing the BUILD.bazel file this way:

load("@io_bazel_rules_groovy//groovy:groovy.bzl", "groovy_junit_test")

groovy_junit_test(
    name = "HelloWorldTest",
    tests = ["HelloWorldTest.groovy"],
)

bazel test //... gives me:

exec ${PAGER:-/usr/bin/less} "$0" || exit 1 Executing tests from //src/test/groovy:HelloWorldTest ----------------------------------------------------------------------------- JUnit version 4.12 .E Time: 0 There was 1 failure: 1) initializationError(org.junit.runner.JUnitCommandLineParseResult) java.lang.IllegalArgumentException: Could not find class [HelloWorldTest] at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:102) at org.junit.runner.JUnitCommandLineParseResult.parseArgs(JUnitCommandLineParseResult.java:50) at org.junit.runner.JUnitCommandLineParseResult.parse(JUnitCommandLineParseResult.java:44) at org.junit.runner.JUnitCore.runMain(JUnitCore.java:72) at org.junit.runner.JUnitCore.main(JUnitCore.java:36) Caused by: java.lang.ClassNotFoundException: HelloWorldTest at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:398) at org.junit.internal.Classes.getClass(Classes.java:16) at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:100) ... 4 more

FAILURES!!! Tests run: 1, Failures: 1



Solution 1:[1]

use groovy_junit_test instead of groovy_test

according to documentation and source code groovy_test is just a test runner

when groovy_junit_test compiles sources and then calls groovy_test

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 daggett