From 46706ba56c908963a0fc95d06036983ed3c5f2d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Sat, 3 Apr 2021 10:02:03 +0800 Subject: [PATCH] add supported test framwork --- docs/en-us/doc/frequently-asked-questions.md | 20 ++++++++++++++------ docs/zh-cn/doc/frequently-asked-questions.md | 20 ++++++++++++++------ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/docs/en-us/doc/frequently-asked-questions.md b/docs/en-us/doc/frequently-asked-questions.md index 6c8f6c4..e72ff34 100644 --- a/docs/en-us/doc/frequently-asked-questions.md +++ b/docs/en-us/doc/frequently-asked-questions.md @@ -11,7 +11,15 @@ Create the class under test object directly, and then use the ability of `Testab For mock tools that modify the class loader or the bytecode of the class under test, such as `PowerMock` and `JMockit`, there is no yet case to prove that they will conflict with `TestableMock`, but in principle, there may be a risk of incompatibility between the two, Please use with caution. -#### 3. How to implement the mock method when the parent class variable points to the child class object? +#### 3. Which test frameworks does `TestableMock` support? + +The `PrivateAccessor` / `OmniConstructor` / `OmniAccessor` and basic functionality of `TestableMock` are theoretically test-frameworks-independent, and should work with any test frameworks out-of-box. + +The only test-framework-related part is the mock invocation verifier, which currently supports `JUnit 4` / `JUnit 5` / `TestNG` and `Spock` appropriately. + +If you encounter compatibility problem under a specific test framework, or want to add mock invocation verifier support for any other frameworks, please raise an [issue](https://github.com/alibaba/testable-mock/issues) to tell us. + +#### 4. How to implement the mock method when the parent class variable points to the child class object? In the code, there are often cases of using interface variables or parent class variables to point to an instance of subclasses and calling methods provided by the parent class or subclass. @@ -21,7 +29,7 @@ Therefore, regardless of whether the actually called method comes from the paren See the use case of the `DemoInheritTest` test class in the Java and Kotlin examples. -#### 4. How to mock generic methods (template methods)? +#### 5. How to mock generic methods (template methods)? Just use the same generic parameters directly on the mock method. @@ -29,25 +37,25 @@ See the use case of the `DemoTemplateTest` test class in the Java and Kotlin exa > Because JVM has a generic erasure mechanism, you can also directly use the `Object` type to replace generic parameters for Java projects, see the commented out "Second solution" example in the Java version of the `DemoTemplateTest` test class. -#### 5. How to mock invocation inside an inner class? +#### 6. How to mock invocation inside an inner class? Just put mock methods in the mock container class of its outer class, it works for code in all inner classes. See the use case of the `DemoInnerClass` test class in the Java and Kotlin examples. -#### 6. Why mocking methods in the `String` class in the Kotlin project does not work? +#### 7. Why mocking methods in the `String` class in the Kotlin project does not work? The `String` type in Kotlin language is actually `kotlin.String` instead of `java.lang.String`. However, when this type is built from bytecode, it will be replaced with Java's `java.lang.String` class, so no matter if the mock target is written as `kotlin.String` or `java.lang.String`, it cannot match the original called method. In actual scenarios, there are very few scenarios where methods in the `String` class need to be mocked, so `TestableMock` has not dealt with this situation specifically. -#### 7. Can `TestableMock` be used for testing Android projects? +#### 8. Can `TestableMock` be used for testing Android projects? It can be used in combination with [Roboelectric](https://github.com/robolectric/robolectric) testing framework. The `Dalvik` and `ART` virtual machines of the Android system use a bytecode system different from the standard JVM, which will affect the normal functionality of `TestableMock`. The `Roboelectric` framework can run Android unit tests on a standard JVM virtual machine, which is much faster than running unit tests through the Android virtual machine. Recently, most Android App unit tests are written with the `Roboelectric` framework. -#### 8. Meet "Command Line is too Long. Shorten command line for ..." error when triggering test in IntelliJ IDE? +#### 9. Meet "Command Line is too Long. Shorten command line for ..." error when triggering test in IntelliJ IDE? This problem is caused by the system `Class Path` content is too long, and has nothing to do with `TestableMock`. However, it should be noted that IntelliJ provides two auxiliary solutions: `JAR manifest` and `classpath file`. If `TestableMock` is used in the test, please select `JAR manifest`. diff --git a/docs/zh-cn/doc/frequently-asked-questions.md b/docs/zh-cn/doc/frequently-asked-questions.md index 8a7794d..ba042f0 100644 --- a/docs/zh-cn/doc/frequently-asked-questions.md +++ b/docs/zh-cn/doc/frequently-asked-questions.md @@ -15,7 +15,15 @@ 对于会修改类加载器或被测类字节码的Mock工具,譬如`PowerMock`和`JMockit`,尚无案例证明会与`TestableMock`发生冲突,但从原理来说二者可能存在不兼容风险,请谨慎使用。 -#### 3. 父类变量指向子类对象时,如何实现Mock方法? +#### 3. `TestableMock`支持哪些测试框架? + +`TestableMock`的`PrivateAccessor`/`OmniConstructor`/`OmniAccessor`以及基础Mock功能均与测试框架无关,理论上适用于任何测试框架。 + +唯独Mock方法的调用校验器是与测试框架相关的,目前已支持`JUnit 4`/`JUnit 5`/`TestNG`/`Spock`四款主流框架。 + +若亲遇到在特定测试框架下的兼容问题,或希望增加其他框架的支持,请通过[Issues](https://github.com/alibaba/testable-mock/issues)告诉我们。 + +#### 4. 父类变量指向子类对象时,如何实现Mock方法? 在代码中,经常会有使用接口变量或父类变量指向子类实例,调用父类或子类方法的情况。 @@ -25,7 +33,7 @@ 参见Java和Kotlin示例中`DemoInheritTest`测试类的用例。 -#### 4. 如何Mock对于泛型方法(模板方法)? +#### 5. 如何Mock对于泛型方法(模板方法)? 与普通方法的Mock方法相同,直接在Mock方法上使用相同的泛型参数即可。 @@ -33,25 +41,25 @@ > 由于JVM存在泛型擦除机制,对于Java项目也可以直接使用`Object`类型替代泛型参数,见Java版`DemoTemplateTest`测试类中被注释掉的"第二种写法"示例。 -#### 5. 如何Mock在内部类代码里的调用? +#### 6. 如何Mock在内部类代码里的调用? 在其所在外部类对应的Mock容器中定义所需的Mock方法即可。 参见Java和Kotlin示例中`DemoInnerClass`测试类的用例。 -#### 6. 在Kotlin项目对`String`类中的方法进行Mock不生效? +#### 7. 在Kotlin项目对`String`类中的方法进行Mock不生效? Kotlin语言中的`String`类型实际上是`kotlin.String`,而非`java.lang.String`。但在构建生成自字节码的时候又会被替换为Java的`java.lang.String`类,因此无论将Mock目标写为`kotlin.String`或`java.lang.String`均无法正常匹配到原始的被调用方法。 实际场景中需要对`String`类中的方法进行Mock的场景很少,`TestableMock`暂未对这种情况做特别处理。 -#### 7. `TestableMock`能否用于Android项目的测试? +#### 8. `TestableMock`能否用于Android项目的测试? 结合[Roboelectric](https://github.com/robolectric/robolectric)测试框架可使用。 Android系统的`Dalvik`和`ART`虚拟机采用了与标准JVM不同的字节码体系,会影响`TestableMock`的正常工作。`Roboelectric`框架能在普通JVM虚拟机上运行Android单元测试,其速度比通过Android虚拟机运行单元测试快非常多,绝大多数Android App的单元测试都在使用`Roboelectric`框架。 -#### 8. 在IntelliJ运行测试报"Command Line is too Long. Shorten command line for ..."错误? +#### 9. 在IntelliJ运行测试报"Command Line is too Long. Shorten command line for ..."错误? 这个问题是由于系统ClassPath包含太多路径所致,与是否使用`TestableMock`无关。但需要注意的是,IntelliJ提供了两种辅助解决机制:`JAR manifest`和`classpath file`,若测试中使用了`TestableMock`,请选择`JAR manifest`。