10

Connecting Bazel and JUnit5 by transforming arguments

 3 years ago
source link: https://flexport.engineering/connecting-bazel-and-junit5-by-transforming-arguments-46440c6ea068
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Connecting Bazel and JUnit5 by transforming arguments

When we started developing with Java in Flexport, we found writing tests painful because there was no way to run a single test in isolation. We quickly discovered the root cause: our build system (Bazel) does not natively integrate with our testing framework (JUnit5), as described in this issue: https://github.com/bazelbuild/bazel/issues/6681

To address this, we’ve written a small utility that allows these two tools to work together, which we are now open sourcing.

Deciding on an approach

One of our colleagues initially solved the problem by customizing the Bazel plugin for IntelliJ IDEA, but it was a bit complex to configure and difficult to share with other engineers. I did not adopt it, because I believe this is the gap between Bazel and JUnit5 — none of IntelliJ IDEA’s business!

Given that Bazel does not support JUnit5, but Bazel supports JUnit4 well, and I suspected the problem must be at the boundary of the two systems. And good products always have good boundaries, so it must have happened at the good boundaries of two good systems. So a way to connect the two boundaries must be there! This was the conclusion of my reasoning process.

I have been an engineer for a long time, and there are two things that are important. The first is: can it be solved? With the IntelliJ plugin, my colleague gave me a clear answer: yes! The second is: how can we scale down the solution set to just a single best one? The idea to connect Bazel and JUnit5 at their existing boundaries seemed promising, so I investigated further.

Investigating with the Java debugger

To start, I went over the source code of junit5.bzl. The class org.junit.platform.console.ConsoleLauncher caught my attention immediately. I started to debug it by specifying just running one test case.

Oops, looks like the arguments are not passed down through the command:

Image for post
Image for post

However, I knew Bazel must have passed something in, or JUnit4 would fail to work if not. Since it’s Java, I went to check the environment variables and the Java properties:

Image for post
Image for post

Bingo, it’s there! So I did a little more background research into the cool TESTBRIDGE_TEST_ONLY environment variable.

Once I understood the inputs expected by JUnit5, I decided to build an adapter to translate TESTBRIDGE_TEST_ONLY from Bazel into command arguments for JUnit5. That’s exactly what this adapter code does.

Using the utility

I created a PR for junit5-samples/junit5-jupiter-starter-bazel. To integrate with it into your project:

Then you can run a single test method or class by:

Image for post
Image for post

And you will find that only one test will run.

Image for post
Image for post

Hopefully this helps any other folks working with JUnit5 and Bazel! Feedback welcome either here or on the GitHub pull request.


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK