@needfspeed/skills-library 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CATALOG.md +19 -0
- package/README.md +46 -0
- package/docs/java/README.md +3 -0
- package/examples/java/README.md +3 -0
- package/package.json +31 -0
- package/skills/java/java-mockito-tests/SKILL.md +44 -0
- package/skills/java/java-mockito-tests/agents/openai.yaml +4 -0
- package/skills/java/java-mockito-tests/references/jmockit-to-mockito.md +118 -0
- package/templates/README.md +3 -0
package/CATALOG.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# 资料目录
|
|
2
|
+
|
|
3
|
+
## Skills
|
|
4
|
+
|
|
5
|
+
| 领域 | 名称 | 入口 | 用途 |
|
|
6
|
+
| --- | --- | --- | --- |
|
|
7
|
+
| Java | java-mockito-tests | `skills/java/java-mockito-tests/SKILL.md` | JMockit 到 Mockito 迁移、JDK 17 兼容和测试验证 |
|
|
8
|
+
|
|
9
|
+
## Docs
|
|
10
|
+
|
|
11
|
+
当前暂无独立文档。与某个 skill 强相关的资料放在该 skill 的 `references/` 中。
|
|
12
|
+
|
|
13
|
+
## Examples
|
|
14
|
+
|
|
15
|
+
当前暂无示例项目。
|
|
16
|
+
|
|
17
|
+
## Templates
|
|
18
|
+
|
|
19
|
+
当前暂无模板。
|
package/README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# @needfspeed/skills-library
|
|
2
|
+
|
|
3
|
+
个人维护的 AI skills 与工程资料集合。资料按用途分类,安装后可从 `node_modules/@needfspeed/skills-library` 读取。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install --save-dev @needfspeed/skills-library
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 目录
|
|
12
|
+
|
|
13
|
+
```text
|
|
14
|
+
skills/ 可直接复制到 AI 工具技能目录的完整 skill
|
|
15
|
+
docs/ 按技术领域分类的独立文档
|
|
16
|
+
examples/ 示例项目与用法
|
|
17
|
+
templates/ 可复用模板
|
|
18
|
+
CATALOG.md 全部资料索引
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
当前收录 `java-mockito-tests`,用于将 Java 项目的 JMockit 单元测试迁移到兼容 JDK 17 的 Mockito 实现。
|
|
22
|
+
|
|
23
|
+
## 使用 skill
|
|
24
|
+
|
|
25
|
+
把完整 skill 目录复制到目标 AI 工具支持的技能目录。例如 Codex:
|
|
26
|
+
|
|
27
|
+
```powershell
|
|
28
|
+
Copy-Item -Recurse node_modules/@needfspeed/skills-library/skills/java/java-mockito-tests $env:USERPROFILE/.codex/skills/
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
通义灵码若使用不同的技能目录,复制同一完整目录到其要求的位置;请保留 `SKILL.md`、`agents/` 和 `references/` 的相对结构。
|
|
32
|
+
|
|
33
|
+
## 维护约定
|
|
34
|
+
|
|
35
|
+
- 每个 skill 使用 `skills/<领域>/<skill-name>/`。
|
|
36
|
+
- 独立知识文档使用 `docs/<领域>/`,不要复制 skill 内部 reference。
|
|
37
|
+
- 示例按领域放入 `examples/<领域>/`。
|
|
38
|
+
- 模板放入 `templates/<类型>/`。
|
|
39
|
+
- 新增资料时同步更新 `CATALOG.md` 并提升包版本。
|
|
40
|
+
|
|
41
|
+
发布前运行:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
npm run check
|
|
45
|
+
npm run pack:check
|
|
46
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@needfspeed/skills-library",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A categorized collection of reusable AI skills and engineering reference materials.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"codex-skill",
|
|
7
|
+
"documentation",
|
|
8
|
+
"java",
|
|
9
|
+
"mockito"
|
|
10
|
+
],
|
|
11
|
+
"license": "UNLICENSED",
|
|
12
|
+
"private": false,
|
|
13
|
+
"files": [
|
|
14
|
+
"skills/",
|
|
15
|
+
"docs/",
|
|
16
|
+
"examples/",
|
|
17
|
+
"templates/",
|
|
18
|
+
"CATALOG.md"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"check": "node scripts/check-package.mjs",
|
|
22
|
+
"pack:check": "npm pack --dry-run"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public",
|
|
26
|
+
"registry": "https://registry.npmjs.org/"
|
|
27
|
+
},
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=18"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: java-mockito-tests
|
|
3
|
+
description: Create, repair, or migrate Java unit tests with Mockito, especially when replacing JMockit for JDK 17 compatibility. Use for JUnit 4 or 5 projects; do not change production behavior merely to make tests pass.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Java Mockito Tests
|
|
7
|
+
|
|
8
|
+
Work from the project's existing build, Java, JUnit, Mockito, and Spring versions. Inspect the relevant production code and tests before editing. Preserve the project's current test style unless migration requires a focused change.
|
|
9
|
+
|
|
10
|
+
## Workflow
|
|
11
|
+
|
|
12
|
+
1. Identify the actual test runner, build modules, test source sets, and commands used by the repository.
|
|
13
|
+
2. Find JMockit dependencies, imports, annotations, `-javaagent` configuration, and test patterns in the affected scope.
|
|
14
|
+
3. Convert behavior in small groups while preserving assertions and the scenario each test protects.
|
|
15
|
+
4. Run the narrowest affected test first, then the owning module's full test task.
|
|
16
|
+
5. Confirm the test count is non-zero and that the intended test class appears in the output. A successful build with skipped or undiscovered tests is not evidence of success.
|
|
17
|
+
6. After the affected scope passes, remove JMockit configuration only when no remaining tests depend on it.
|
|
18
|
+
|
|
19
|
+
For concrete mappings from JMockit APIs to Mockito and ordinary Java techniques, read [references/jmockit-to-mockito.md](references/jmockit-to-mockito.md).
|
|
20
|
+
|
|
21
|
+
## Framework choices
|
|
22
|
+
|
|
23
|
+
- For JUnit 5, prefer `@ExtendWith(MockitoExtension.class)` when extension-based initialization fits the existing project.
|
|
24
|
+
- For JUnit 4, prefer `@RunWith(MockitoJUnitRunner.class)` or a scoped `MockitoRule`. Use `MockitoAnnotations.openMocks(this)` only when runner or extension integration is unsuitable, and close the returned resource.
|
|
25
|
+
- Use the Mockito capabilities supported by the project's existing dependency line. Add or upgrade dependencies only when the required behavior is unavailable and the change is compatible with the repository.
|
|
26
|
+
- Prefer dependency injection and observable behavior. Use static or construction mocking only for legacy seams that cannot reasonably be supplied as dependencies within the task.
|
|
27
|
+
- Use Spring test facilities only when the test actually needs a Spring context. Keep ordinary unit tests context-free.
|
|
28
|
+
|
|
29
|
+
## Correctness constraints
|
|
30
|
+
|
|
31
|
+
- Do not delete, weaken, or broadly replace meaningful assertions just to obtain a passing build.
|
|
32
|
+
- Do not make all mocks lenient. Remove unnecessary stubs or apply leniency only to the specific shared stub whose use legitimately varies.
|
|
33
|
+
- Do not mock the class under test. Use a spy only when partial real behavior is part of the scenario and a smaller seam is unavailable.
|
|
34
|
+
- Avoid deep stubs unless the external API shape makes them unavoidable; prefer explicit collaborators and values.
|
|
35
|
+
- Match arguments precisely enough to preserve the test's intent. Do not replace meaningful values with `any()` indiscriminately.
|
|
36
|
+
- Use `verify` for meaningful interactions, not for every implementation detail. Prefer state or return-value assertions where those express the contract.
|
|
37
|
+
- Do not introduce sleeps for asynchronous behavior when the project has a deterministic synchronization mechanism.
|
|
38
|
+
- Never report migration complete while JMockit imports, dependencies, plugin arguments, or `-javaagent` settings remain in the claimed scope.
|
|
39
|
+
|
|
40
|
+
## JDK 17 checks
|
|
41
|
+
|
|
42
|
+
Check build plugins and test runtime options as well as source compatibility. Remove JMockit agents only after confirming they are unused. Treat `--add-opens` and similar module flags as evidence of a compatibility workaround: keep flags still required by other libraries and remove only those proven to belong solely to the retired setup.
|
|
43
|
+
|
|
44
|
+
Report which tests ran, the command used, any remaining JMockit usage, and any compatibility limitation that could not be resolved in the requested scope.
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# JMockit 到 Mockito 迁移参考
|
|
2
|
+
|
|
3
|
+
只在迁移或审查 JMockit 测试时读取本文件。先确认项目的 JUnit 和 Mockito 版本,再选择可用 API。
|
|
4
|
+
|
|
5
|
+
## 常见映射
|
|
6
|
+
|
|
7
|
+
| JMockit | Mockito 或 Java 替代方式 |
|
|
8
|
+
| --- | --- |
|
|
9
|
+
| `@Mocked` | `@Mock`,或局部 `mock(Type.class)` |
|
|
10
|
+
| `@Injectable` | 显式创建 mock 并传入被测对象;适合时使用 `@InjectMocks` |
|
|
11
|
+
| `@Tested` | 显式构造被测对象;简单构造器注入可使用 `@InjectMocks` |
|
|
12
|
+
| `new Expectations() {{ ... }}` | `when(...).thenReturn(...)`、`thenThrow(...)` 或 `doReturn/throw(...)` |
|
|
13
|
+
| `new Verifications() {{ ... }}` | `verify(...)`、`times(...)`、`never()`、`InOrder` |
|
|
14
|
+
| `result = value` | `thenReturn(value)` |
|
|
15
|
+
| `result = exception` | `thenThrow(exception)` |
|
|
16
|
+
| `times/minTimes/maxTimes` | `times/atLeast/atMost` |
|
|
17
|
+
| `withAny/withEqual/withCapture` | `any/eq/ArgumentCaptor` |
|
|
18
|
+
| `MockUp<T>` | 普通 mock、spy,或受作用域保护的 static/construction mock |
|
|
19
|
+
| `Deencapsulation.getField/setField` | 优先构造器或 setter;必要时使用项目已有的受控反射工具 |
|
|
20
|
+
| `Deencapsulation.invoke` | 通过公共行为测试;不要默认直接测试私有方法 |
|
|
21
|
+
|
|
22
|
+
## Stubbing
|
|
23
|
+
|
|
24
|
+
普通 mock:
|
|
25
|
+
|
|
26
|
+
```java
|
|
27
|
+
when(repository.findById(id)).thenReturn(Optional.of(entity));
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Spy 调用真实方法,使用 `when(spy.call())` 可能在设桩时就执行真实代码,因此优先:
|
|
31
|
+
|
|
32
|
+
```java
|
|
33
|
+
doReturn(value).when(spy).call();
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Void 方法:
|
|
37
|
+
|
|
38
|
+
```java
|
|
39
|
+
doThrow(new IOException("failed")).when(client).close();
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## 参数与验证
|
|
43
|
+
|
|
44
|
+
若一次调用中使用 matcher,则该次调用的所有参数都应使用 matcher:
|
|
45
|
+
|
|
46
|
+
```java
|
|
47
|
+
verify(repository).save(eq(accountId), any(Order.class));
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
需要检查传入对象内容时使用 `ArgumentCaptor`,不要把复杂断言塞进自定义 matcher。
|
|
51
|
+
|
|
52
|
+
## 静态方法
|
|
53
|
+
|
|
54
|
+
仅在当前 Mockito 版本支持且遗留设计没有更小替代方案时使用。务必用 try-with-resources 限定作用域,避免污染其它测试:
|
|
55
|
+
|
|
56
|
+
```java
|
|
57
|
+
try (MockedStatic<ClockProvider> mocked = mockStatic(ClockProvider.class)) {
|
|
58
|
+
mocked.when(ClockProvider::now).thenReturn(fixedInstant);
|
|
59
|
+
assertEquals(expected, service.execute());
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
不要并行共享同一个静态 mock。
|
|
64
|
+
|
|
65
|
+
## 构造器拦截
|
|
66
|
+
|
|
67
|
+
优先把对象创建移动到可注入的工厂或 supplier。无法在任务范围内调整遗留 seam 时,可用 `mockConstruction`,并检查构造次数或参数是否属于业务契约。
|
|
68
|
+
|
|
69
|
+
## Final 类和方法
|
|
70
|
+
|
|
71
|
+
现代 Mockito 通常可 mock final 类型,但是否可用取决于项目依赖与 mock maker 配置。先验证现有配置;不要同时添加相互冲突的 mock maker 文件。
|
|
72
|
+
|
|
73
|
+
## 私有成员
|
|
74
|
+
|
|
75
|
+
Mockito 不提供 `Deencapsulation` 的一比一替代。按以下顺序处理:
|
|
76
|
+
|
|
77
|
+
1. 通过公共方法断言可观察结果。
|
|
78
|
+
2. 将复杂私有逻辑提取到具备清晰职责、可直接测试的协作者,仅当生产设计因此改善。
|
|
79
|
+
3. 使用已有包级 API 或测试 fixture。
|
|
80
|
+
4. 只有维护遗留代码且范围受控时才用反射,并记录原因。
|
|
81
|
+
|
|
82
|
+
不要为了让测试方便而把私有成员普遍改为 public。
|
|
83
|
+
|
|
84
|
+
## JUnit 生命周期
|
|
85
|
+
|
|
86
|
+
JUnit 5:
|
|
87
|
+
|
|
88
|
+
```java
|
|
89
|
+
@ExtendWith(MockitoExtension.class)
|
|
90
|
+
class ServiceTest {
|
|
91
|
+
@Mock Repository repository;
|
|
92
|
+
@InjectMocks Service service;
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
JUnit 4:
|
|
97
|
+
|
|
98
|
+
```java
|
|
99
|
+
@RunWith(MockitoJUnitRunner.class)
|
|
100
|
+
public class ServiceTest {
|
|
101
|
+
@Mock Repository repository;
|
|
102
|
+
@InjectMocks Service service;
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
若现有 JUnit 4 测试已有不可替换 runner,可使用 `MockitoRule` 或显式初始化,不要叠加两个 runner。
|
|
107
|
+
|
|
108
|
+
## 依赖和构建清理
|
|
109
|
+
|
|
110
|
+
迁移结束前搜索:
|
|
111
|
+
|
|
112
|
+
- `mockit.` imports
|
|
113
|
+
- `jmockit` Maven/Gradle dependency
|
|
114
|
+
- Surefire、Failsafe、Gradle test task 或 IDE 配置里的 `-javaagent`
|
|
115
|
+
- JMockit 专用启动参数与覆盖率组合参数
|
|
116
|
+
- 被禁用、被排除或改名后无法被测试框架发现的测试
|
|
117
|
+
|
|
118
|
+
Maven 多模块项目从拥有该测试的模块开始,再运行受影响聚合范围。Gradle 项目确认正确的 `test` source set 和过滤参数。每次都核对执行数量,避免把 `No tests found`、全量跳过或错误的模块当作通过。
|