Connecting Bazel and JUnit5 by transforming arguments
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.
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:
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:
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:
And you will find that only one test will run.
Hopefully this helps any other folks working with JUnit5 and Bazel! Feedback welcome either here or on the GitHub pull request.
Recommend
-
56
Learn how to use the @RunWith annotation in the JUnit 5 framework.
-
64
JUnit4, JUnit5, and Spock: A Comparison This summary of a JUG meet...
-
47
README.md JUnit 5 This repository is the home of the next generation of JUnit, JUnit 5.
-
36
JUnit5 使用者:为何 Spock 值得你看它一眼 胡键 Posted at — Apr 15, 2020 阅读 221 这篇文章的诞生纯属巧合:一是早上看到微信群中有人问 Junit 的问题(往往这个时候我就忍不住向他...
-
8
Parameterized Tests using JUnit5
-
6
JUnit5 使用者:为何 Spock 值得你看它一眼 胡键 Posted at — Apr 15, 2020 阅读 236 这篇文章的诞生纯属巧合:一是早上看到微信群中有人问 Junit 的问题(往往这个时候我就忍不住...
-
25
用csv文件进行Junit5的参数化测试 2018-05-23 17:06:51 +08 字数:2321 标签: Java ...
-
5
JUnit5简介及其Gradle初始化 2018-03-30 15:03:43 +08 字数:1872 标签: Java Test
-
6
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等; 关于《JUnit...
-
8
示例代码:GitHub:https://github.com/Max-Qiu/demo-SpringBootGitee:https://gitee.com/Max-Qiu/demo-SpringBoot...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK