refactor test code, and omni constructor test

This commit is contained in:
金戟
2021-03-18 23:51:06 +08:00
parent 206fc5486e
commit 606760d632
42 changed files with 364 additions and 148 deletions

View File

@@ -54,9 +54,9 @@ Executing the unit test again will print out the signatures of all mock methods,
```text
[DIAGNOSE] Handling test class com/alibaba/testable/demo/basic/DemoMockTest
[VERBOSE] Test case "should_able_to_mock_new_object"
[VERBOSE] Test case "should_mock_new_object"
... ...
[VERBOSE] Test case "should_able_to_set_mock_context"
[VERBOSE] Test case "should_set_mock_context"
[DIAGNOSE] Found 6 test cases
[DIAGNOSE] Handling mock class com/alibaba/testable/demo/basic/DemoMockTest$Mock
[VERBOSE] Mock constructor "createBlackBox" as "com.alibaba.demo.basic.model.BlackBox(java.lang.String)"

View File

@@ -19,7 +19,7 @@ public class DemoMockTest {
}
@Test
void should_able_to_mock_member_method() throws Exception {
void should_mock_member_method() throws Exception {
assertEquals("hello_world", demoMock.outerFunc());
verify("innerFunc").with("world");
}
@@ -39,7 +39,7 @@ public class DemoMockTest {
} // Add this line
@Test
void should_able_to_mock_member_method() throws Exception {
void should_mock_member_method() throws Exception {
assertEquals("hello_world", demoMock.outerFunc());
verify("innerFunc").with("world");
}

View File

@@ -67,7 +67,7 @@ private String substring(String self, int i, int j) {
}
```
For complete code examples, see the `should_able_to_mock_common_method()` test cases in the `java-demo` and `kotlin-demo` sample projects. (Because Kotlin has made magical changes to the String type, the method under test in the Kotlin example adds a layer of encapsulation to the `BlackBox` class)
For complete code examples, see the `should_mock_common_method()` test cases in the `java-demo` and `kotlin-demo` sample projects. (Because Kotlin has made magical changes to the String type, the method under test in the Kotlin example adds a layer of encapsulation to the `BlackBox` class)
#### 2. Mock the member method of the class under test itself
@@ -87,7 +87,7 @@ private String innerFunc(String text) {
Similarly, if the method in the above example needs to access the original tested object that initiated the call, it may not use the `targetClass` parameter, but when defining the mock method, add a parameter of type `DemoMock` to the first index of the method parameter list.
For complete code examples, see the `should_able_to_mock_member_method()` test case in the `java-demo` and `kotlin-demo` sample projects.
For complete code examples, see the `should_mock_member_method()` test case in the `java-demo` and `kotlin-demo` sample projects.
#### 3. Mock static methods of any class
@@ -102,7 +102,7 @@ private BlackBox secretBox() {
}
```
For complete code examples, see the `should_able_to_mock_static_method()` test case in the `java-demo` and `kotlin-demo` sample projects.
For complete code examples, see the `should_mock_static_method()` test case in the `java-demo` and `kotlin-demo` sample projects.
#### 4. Mock `new` operation of any type
@@ -123,7 +123,7 @@ private BlackBox createBlackBox(String text) {
> You can still use the `@MockMethod` annotation, and configure the `targetMethod` parameter value to `"<init>"`, and the rest is the same as above. The effect is the same as using the `@MockContructor` annotation
For complete code examples, see the `should_able_to_mock_new_object()` test case in the `java-demo` and `kotlin-demo` sample projects.
For complete code examples, see the `should_mock_new_object()` test case in the `java-demo` and `kotlin-demo` sample projects.
#### 5. Identify different invocation source in mock method
@@ -157,7 +157,7 @@ private Data mockDemo() {
}
```
For complete code examples, see the `should_able_to_get_source_method_name()` and `should_able_to_get_test_case_name()` test cases in the `java-demo` and `kotlin-demo` sample projects.
For complete code examples, see the `should_get_source_method_name()` and `should_get_test_case_name()` test cases in the `java-demo` and `kotlin-demo` sample projects.
#### 6. Verify the sequence and parameters of the mock method being invoked

View File

@@ -59,9 +59,9 @@ class DemoTest {
```text
[DIAGNOSE] Handling test class com/alibaba/testable/demo/basic/DemoMockTest
[VERBOSE] Test case "should_able_to_mock_new_object"
[VERBOSE] Test case "should_mock_new_object"
... ...
[VERBOSE] Test case "should_able_to_set_mock_context"
[VERBOSE] Test case "should_set_mock_context"
[DIAGNOSE] Found 6 test cases
[DIAGNOSE] Handling mock class com/alibaba/testable/demo/basic/DemoMockTest$Mock
[VERBOSE] Mock constructor "createBlackBox" as "com.alibaba.demo.basic.model.BlackBox(java.lang.String)"

View File

@@ -19,7 +19,7 @@ public class DemoMockTest {
}
@Test
void should_able_to_mock_member_method() throws Exception {
void should_mock_member_method() throws Exception {
assertEquals("hello_world", demoMock.outerFunc());
verify("innerFunc").with("world");
}
@@ -39,7 +39,7 @@ public class DemoMockTest {
} // 增加此行
@Test
void should_able_to_mock_member_method() throws Exception {
void should_mock_member_method() throws Exception {
assertEquals("hello_world", demoMock.outerFunc());
verify("innerFunc").with("world");
}

View File

@@ -67,7 +67,7 @@ private String substring(String self, int i, int j) {
}
```
完整代码示例见`java-demo``kotlin-demo`示例项目中的`should_able_to_mock_common_method()`测试用例。(由于Kotlin对String类型进行了魔改故Kotlin示例中将被测方法在`BlackBox`类里加了一层封装)
完整代码示例见`java-demo``kotlin-demo`示例项目中的`should_mock_common_method()`测试用例。(由于Kotlin对String类型进行了魔改故Kotlin示例中将被测方法在`BlackBox`类里加了一层封装)
#### 2. 覆写被测类自身的成员方法
@@ -87,7 +87,7 @@ private String innerFunc(String text) {
同样的,上述示例中的方法如需访问发起调用的原始被测对象,也可不使用`targetClass`参数而是在定义Mock方法时在方法参数列表首位加一个类型为`DemoMock`的参数(名字随意)。
完整代码示例见`java-demo``kotlin-demo`示例项目中的`should_able_to_mock_member_method()`测试用例。
完整代码示例见`java-demo``kotlin-demo`示例项目中的`should_mock_member_method()`测试用例。
#### 3. 覆写任意类的静态方法
@@ -104,7 +104,7 @@ private BlackBox secretBox() {
对于静态方法的Mock通常不使用方法参数列表的首位加参数来表示目标类型。但这种方法也依然适用只是实际传入的第一个参数值将始终是`null`
完整代码示例见`java-demo``kotlin-demo`示例项目中的`should_able_to_mock_static_method()`测试用例。
完整代码示例见`java-demo``kotlin-demo`示例项目中的`should_mock_static_method()`测试用例。
#### 4. 覆写任意类的new操作
@@ -123,7 +123,7 @@ private BlackBox createBlackBox(String text) {
}
```
完整代码示例见`java-demo``kotlin-demo`示例项目中的`should_able_to_mock_new_object()`测试用例。
完整代码示例见`java-demo``kotlin-demo`示例项目中的`should_mock_new_object()`测试用例。
#### 5. 在Mock方法中区分调用来源
@@ -157,7 +157,7 @@ private Data mockDemo() {
}
```
完整代码示例见`java-demo``kotlin-demo`示例项目中的`should_able_to_get_source_method_name()``should_able_to_get_test_case_name()`测试用例。
完整代码示例见`java-demo``kotlin-demo`示例项目中的`should_get_source_method_name()``should_get_test_case_name()`测试用例。
#### 6. 验证Mock方法被调用的顺序和参数