From f776deff0ee7791bc05bf8aef090ffe0414ba117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Tue, 16 Mar 2021 22:19:16 +0800 Subject: [PATCH] update faq about how to mock inner class --- docs/en-us/doc/frequently-asked-questions.md | 12 +++++++++--- docs/zh-cn/README.md | 4 ++-- docs/zh-cn/doc/frequently-asked-questions.md | 12 +++++++++--- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/docs/en-us/doc/frequently-asked-questions.md b/docs/en-us/doc/frequently-asked-questions.md index d618ca6..6c8f6c4 100644 --- a/docs/en-us/doc/frequently-asked-questions.md +++ b/docs/en-us/doc/frequently-asked-questions.md @@ -29,19 +29,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. Why mocking methods in the `String` class in the Kotlin project does not work? +#### 5. 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? 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. -#### 6. Can `TestableMock` be used for testing Android projects? +#### 7. 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. -#### 7. Meet "Command Line is too Long. Shorten command line for ..." error when triggering test in IntelliJ IDE? +#### 8. 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/README.md b/docs/zh-cn/README.md index 1a58ddc..2b57c0e 100644 --- a/docs/zh-cn/README.md +++ b/docs/zh-cn/README.md @@ -1,11 +1,11 @@ TestableMock简介 --- -单元测试中的Mock方法,通常是为了绕开那些依赖外部资源或无关功能的方法调用,使得测试重点能够集中在需要验证和保障的代码逻辑上。 +单元测试中的Mock方法,通常是为了绕开那些依赖外部资源或无关功能的方法调用,使得测试重点能够集中在需要验证和保障的代码逻辑上。某个调用需要被Mock,往往只与其自身特征有关,而与调用的来源无关。 在定义Mock方法时,开发者真正关心的只有一件事:"这个调用,在测试的时候要换成那个假的Mock方法"。 -然而当下主流的Mock框架在实现Mock功能时,需要开发者操心的事情实在太多:Mock框架如何初始化、与所用的服务框架是否兼容、要被Mock的方法是不是私有的、是不是静态的、被Mock对象是new出来的还是注入的、怎样把被测对象送回被测类里...这些非关键的额外工作极大分散了使用Mock工具应有的乐趣。 +当下主流的Mock框架在实现Mock功能时,需要开发者操心的事情实在太多:Mock框架如何初始化、与所用的服务框架是否兼容、要被Mock的方法是不是私有的、是不是静态的、被Mock对象是new出来的还是注入的、怎样把被测对象送回被测类里...这些非关键的额外工作极大分散了使用Mock工具应有的乐趣。 于是,我们开发了`TestableMock`,**一款特立独行的轻量Mock工具**。 diff --git a/docs/zh-cn/doc/frequently-asked-questions.md b/docs/zh-cn/doc/frequently-asked-questions.md index 547979e..8a7794d 100644 --- a/docs/zh-cn/doc/frequently-asked-questions.md +++ b/docs/zh-cn/doc/frequently-asked-questions.md @@ -33,19 +33,25 @@ > 由于JVM存在泛型擦除机制,对于Java项目也可以直接使用`Object`类型替代泛型参数,见Java版`DemoTemplateTest`测试类中被注释掉的"第二种写法"示例。 -#### 5. 在Kotlin项目对`String`类中的方法进行Mock不生效? +#### 5. 如何Mock在内部类代码里的调用? + +在其所在外部类对应的Mock容器中定义所需的Mock方法即可。 + +参见Java和Kotlin示例中`DemoInnerClass`测试类的用例。 + +#### 6. 在Kotlin项目对`String`类中的方法进行Mock不生效? Kotlin语言中的`String`类型实际上是`kotlin.String`,而非`java.lang.String`。但在构建生成自字节码的时候又会被替换为Java的`java.lang.String`类,因此无论将Mock目标写为`kotlin.String`或`java.lang.String`均无法正常匹配到原始的被调用方法。 实际场景中需要对`String`类中的方法进行Mock的场景很少,`TestableMock`暂未对这种情况做特别处理。 -#### 6. `TestableMock`能否用于Android项目的测试? +#### 7. `TestableMock`能否用于Android项目的测试? 结合[Roboelectric](https://github.com/robolectric/robolectric)测试框架可使用。 Android系统的`Dalvik`和`ART`虚拟机采用了与标准JVM不同的字节码体系,会影响`TestableMock`的正常工作。`Roboelectric`框架能在普通JVM虚拟机上运行Android单元测试,其速度比通过Android虚拟机运行单元测试快非常多,绝大多数Android App的单元测试都在使用`Roboelectric`框架。 -#### 7. 在IntelliJ运行测试报"Command Line is too Long. Shorten command line for ..."错误? +#### 8. 在IntelliJ运行测试报"Command Line is too Long. Shorten command line for ..."错误? 这个问题是由于系统ClassPath包含太多路径所致,与是否使用`TestableMock`无关。但需要注意的是,IntelliJ提供了两种辅助解决机制:`JAR manifest`和`classpath file`,若测试中使用了`TestableMock`,请选择`JAR manifest`。