4

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

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
  • 1
    `package src.test.groovy` is wrong, the classes in this dir have no package name – injecteer Mar 04 '22 at 09:22
  • @injecteer If I uncomment it (`package src.test.groovy`) I still get an error – Vertexwahn Mar 04 '22 at 13:13
  • 1
    @Vertexwahn, have you tried [groovy_junit_test](https://github.com/bazelbuild/rules_groovy#groovy_junit_test) instead of `groovy_test`? because it sounds like `groovy_test` is just a test runner when `groovy_junit_test` compiles sources before calling groovy_test... https://github.com/bazelbuild/rules_groovy/blob/master/groovy/groovy.bzl#L295 – daggett Mar 28 '22 at 08:11
  • @daggett I tried to use `groovy_junit_test` but it does not work - I update my question with this test and the results – Vertexwahn Mar 28 '22 at 16:16
  • 1
    ok, seems you don't have JAVA_HOME env var: https://github.com/bazelbuild/rules_groovy/blob/master/groovy/groovy.bzl#L228 – daggett Mar 28 '22 at 17:02
  • @daggett Sorry did the test run on a different system - now I executed it on Ubuntu again and the JAVA_HOME problem is gone – Vertexwahn Mar 29 '22 at 07:02

1 Answers1

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

daggett
  • 26,404
  • 3
  • 40
  • 56
  • Makes sense. As I pointed out in my answer I tried also `groovy_junit_test` (had first an issue with missing `JAVA_HOME`, but this issue was fixed and then a new issue popped up -> `Could not find class [HelloWorldTest]` - if you have any hints for me how to fix this I would be glad to donate the bounty to you – Vertexwahn Apr 01 '22 at 18:23