This is an automated email from the ASF dual-hosted git repository.
terrymanu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 7cc859bfc9c Refine unit test static state cleanup standard (#39703)
7cc859bfc9c is described below
commit 7cc859bfc9c41389c806990424c0b6ec6a242968
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Sep 3 09:45:04 2026 +0800
Refine unit test static state cleanup standard (#39703)
---
CODE_OF_CONDUCT.md | 2 +-
docs/community/content/involved/conduct/code.cn.md | 2 +-
docs/community/content/involved/conduct/code.en.md | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md
index 08fcb432c1d..fdba3bd5e14 100644
--- a/CODE_OF_CONDUCT.md
+++ b/CODE_OF_CONDUCT.md
@@ -95,13 +95,13 @@ The following code of conduct is based on full compliance
with the [Apache Softw
- Error tolerance testing: Get expected results through incorrect inputs
such as illegal data, exception flows, etc.
- Use `assert` prefix for all test method names.
- Unit tests must exercise behavior through public APIs only. Reflection-based
invocation of private members is forbidden. If tests must access fields via
reflection, use `Plugins.getMemberAccessor()` and limit reflection to `Field`
access only.
+- Tests that modify static state must restore the original state after each
test.
- Obtain SPI implementations through the project loader by default. If the
class under test implements `TypedSPI` or `DatabaseTypedSPI`, instantiate it
through `TypedSPILoader` or `DatabaseTypedSPILoader`, not with `new`.
- Every unit-test class must directly test a corresponding production class
and be named `<ProductionClassName>Test`, using the exact simple name of the
production class. This class-name rule is mandatory and is independent of
scenario-focused test-method naming.
- When a production method is covered by only one test case, name that test
method `assert<MethodName>` without extra suffixes, and prefer isolating one
public production method per dedicated test method; when practical, keep test
method ordering aligned with the corresponding production methods.
- For parameterized tests, provide display names via parameters and use
`"{0}"` as the display-name template.
- Keep test names concise and scenario-focused; avoid `ReturnsXXX` and wording
that restates the expected result instead of naming the scenario.
- Assertions must directly express the tested contract. Use `not` or
`containsString` only when the contract requires inequality or substring
matching; do not use them when an exact value or a more specific matcher is
available.
-- Use JUnit 5 and Mockito. Keep environment preparation, test action, and
assertions distinct; reset static state between scenarios and reuse existing
swappers or helpers for complex configuration.
- Default to direct Mockito mocks. Use a private helper only for repeated
local setup and a standalone fixture only for a stable external or packaged
test boundary. Give fixtures the narrowest practical visibility, keep them in
the nearest owning test package or module, and do not create cross-module test
APIs for convenience. Delete or inline thin mock wrappers.
- Data assertion standards should follow:
- Boolean type assertions should use `assertTrue` and `assertFalse`;
diff --git a/docs/community/content/involved/conduct/code.cn.md
b/docs/community/content/involved/conduct/code.cn.md
index 2ce2dadf67b..a58a43f1ffa 100644
--- a/docs/community/content/involved/conduct/code.cn.md
+++ b/docs/community/content/involved/conduct/code.cn.md
@@ -99,6 +99,7 @@ chapter = true
- 容错性测试(Error):通过非法数据、异常流程等错误的输入,得到预期结果。
- 使用 `assert` 前缀命名所有的测试用例。
- 单元测试必须通过公共 API 验证行为,禁止通过反射调用私有成员。 若测试必须通过反射访问字段,应使用
`Plugins.getMemberAccessor()`,且反射仅限于 `Field` 访问。
+ - 测试修改静态状态时,必须在每个测试结束后恢复其原始状态。
- 默认通过项目加载器获取 SPI 实现。 如果被测类实现了 `TypedSPI` 或 `DatabaseTypedSPI`,应通过
`TypedSPILoader` 或 `DatabaseTypedSPILoader` 实例化,禁止使用 `new`。
- 每个单元测试类必须直接测试一个对应的生产类,测试类必须使用生产类的准确简单类名并命名为 `<ProductionClassName>Test`。
该测试类命名规则为强制要求,与面向场景的测试方法命名规则相互独立。
- 当某个生产方法只由一个测试用例覆盖时,测试方法命名为 `assert<MethodName>`,无额外后缀。
@@ -106,7 +107,6 @@ chapter = true
- 参数化测试需通过参数提供显示名,并使用 `"{0}"` 作为展示名模板。
- 测试名称应简洁并聚焦场景;避免使用 `ReturnsXXX`,也不要使用仅复述预期结果而未说明场景的措辞。
- 断言必须直接表达被测契约。 仅当被测契约是不相等或包含指定子串时,才可使用 `not` 或
`containsString`;可断言完整值或使用更具体的 matcher 时,不得使用这两个 matcher。
- - 使用 JUnit 5 和 Mockito。 明确区分环境准备、测试执行和断言;不同场景之间应重置静态状态,并复用现有的 swapper
或辅助工具处理复杂配置。
- 默认直接使用 Mockito mock。 仅对重复的局部准备使用私有辅助方法,仅对稳定的外部测试边界或打包测试边界使用独立测试夹具。
测试夹具应采用实际可行的最小可见性,并放在最近的所属测试包或模块中;不要为了方便而创建跨模块测试 API。 删除或内联内容单薄的 mock 包装器。
- 数据断言规范应遵循:
- 布尔类型断言应使用 `assertTrue` 和 `assertFalse`;
diff --git a/docs/community/content/involved/conduct/code.en.md
b/docs/community/content/involved/conduct/code.en.md
index f27dfa10b6f..dbc8f9a6b5d 100644
--- a/docs/community/content/involved/conduct/code.en.md
+++ b/docs/community/content/involved/conduct/code.en.md
@@ -99,6 +99,7 @@ The following code of conduct is based on full compliance
with the [Apache Softw
- Error tolerance testing: Get expected results through incorrect inputs
such as illegal data, exception flows, etc.
- Use `assert` prefix for all test method names.
- Unit tests must exercise behavior through public APIs only. Reflection-based
invocation of private members is forbidden. If tests must access fields via
reflection, use `Plugins.getMemberAccessor()` and limit reflection to `Field`
access only.
+- Tests that modify static state must restore the original state after each
test.
- Obtain SPI implementations through the project loader by default. If the
class under test implements `TypedSPI` or `DatabaseTypedSPI`, instantiate it
through `TypedSPILoader` or `DatabaseTypedSPILoader`, not with `new`.
- Every unit-test class must directly test a corresponding production class
and be named `<ProductionClassName>Test`, using the exact simple name of the
production class. This class-name rule is mandatory and is independent of
scenario-focused test-method naming.
- Except for simple `getter /setter` methods, unit tests need full coverage.
@@ -106,7 +107,6 @@ The following code of conduct is based on full compliance
with the [Apache Softw
- For parameterized tests, provide display names via parameters and use
`"{0}"` as the display-name template.
- Keep test names concise and scenario-focused; avoid `ReturnsXXX` and wording
that restates the expected result instead of naming the scenario.
- Assertions must directly express the tested contract. Use `not` or
`containsString` only when the contract requires inequality or substring
matching; do not use them when an exact value or a more specific matcher is
available.
-- Use JUnit 5 and Mockito. Keep environment preparation, test action, and
assertions distinct; reset static state between scenarios and reuse existing
swappers or helpers for complex configuration.
- Default to direct Mockito mocks. Use a private helper only for repeated
local setup and a standalone fixture only for a stable external or packaged
test boundary. Give fixtures the narrowest practical visibility, keep them in
the nearest owning test package or module, and do not create cross-module test
APIs for convenience. Delete or inline thin mock wrappers.
- Data assertion standards should follow:
- Boolean type assertions should use `assertTrue` and `assertFalse`;