0

I'm having problems with the java_test rule.

Given a very simple scenario of a class called Code, and a test case called Test, after running the test and either failing or succeeding, if I change the code in Test, any run of Bazel uses the cached "old" code for Test. To make Bazel run the updated code in Test I have to make a change to Code.

My Bazel config looks (roughly) like below. I've tried 2 patterns. In both cases the code under test is built as a separate library. In the first pattern I build the test case as a java_test with the test code built as part of the test. In the second pattern I build the test case as a library of its own, and then have a java_test which has runtime dependencies on both libraries.

In all cases when I do something like bazel test //package/TestDirectly or bazel test //package/TestViaLibrary the first run through will compile the Test.java, and no subsequent executes will compile it until I also change Code.java.

Is there some setting, misunderstanding of expectations, or something else going on here? My expectation is that a change to Test.java should be detected and thus cause the test code to be rebuilt rather than cached as part of running the test and no change to Code.java should be necessary.

java_library(
   name = "Code",
   srcs = ["Code.java"],
)

java_library(
    name = "TestsLibrary",
    srcs = ["Test.java"],
    deps = [
        ":Code",
    ],
)

java_test(
    name = "TestDirectly",
    srcs = ["Test.java"],
    test_class = "package.Test",
    deps = [
        ":Code",
    ],
)

java_test(
    name = "TestViaLibrary",
    test_class = "package.Test",
    runtime_deps = [
        ":Code",
        ":TestsLibrary",
    ],
)
2
  • 1
    Sound like you editor does not store the change Test.java file. Can you do cat Test.java to check if the changes where really stored? Commented Jun 24, 2024 at 13:36
  • I use intellij idea, i can see the file contents are changed because if I do a bazel build then both libraries build. As a very weird addition, I have added macro around the java_test, and based it on junit5, and a testng engine, and now changes to Test.java are causing the test to run. Odd, but given I want to use the macro anyway, this is no longer a concern Commented Jun 26, 2024 at 6:02

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.