update docs and bump version to 0.4.1

This commit is contained in:
金戟
2020-12-12 00:05:53 +08:00
parent 2bdad3c1f6
commit 2dea6f9480
18 changed files with 38 additions and 36 deletions

View File

@@ -12,8 +12,8 @@ repositories {
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
testImplementation('com.alibaba.testable:testable-all:0.4.0')
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.4.0')
testImplementation('com.alibaba.testable:testable-all:0.4.1')
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.4.1')
}
test {

View File

@@ -12,7 +12,7 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>5.6.2</junit.version>
<testable.version>0.4.0</testable.version>
<testable.version>0.4.1</testable.version>
</properties>
<dependencies>

View File

@@ -16,8 +16,8 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
testImplementation("org.junit.jupiter:junit-jupiter:5.6.2")
testImplementation("com.alibaba.testable:testable-all:0.4.0")
testAnnotationProcessor("com.alibaba.testable:testable-processor:0.4.0")
testImplementation("com.alibaba.testable:testable-all:0.4.1")
testAnnotationProcessor("com.alibaba.testable:testable-processor:0.4.1")
}
tasks.withType<KotlinCompile> {

View File

@@ -14,7 +14,7 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>5.6.2</junit.version>
<testable.version>0.4.0</testable.version>
<testable.version>0.4.1</testable.version>
</properties>
<dependencies>

View File

@@ -0,0 +1,3 @@
与其他工具比较
---

View File

@@ -13,7 +13,7 @@ public test_case() {
}
```
这个用例会检查在执行被测方法`methodToTest()`时,名称是`mockMethod`的Mock方法应当被调用过,且调用时收到的参数值为123和"abc"假设被Mock的`mockMethod`方法有两个参数)。
这个用例会检查在执行被测方法`methodToTest()`时,名称是`mockMethod`的Mock方法是否有被调用过,且调用时收到的参数值是否为`123``"abc"`假设被Mock的`mockMethod`方法有两个参数)。
除了这种简单校验以外TestableMock当前已经支持了多种**校验器**,以及能够模糊匹配参数特征的**匹配器**。

View File

@@ -1,5 +1,8 @@
# Release Note
## 0.4.1
- deprecate @TestableMock annotation, use @MockMethod and @MockConstructor instead
## 0.4.0
- fix a jvm 9+ compatibility issue cause by default classloader change
- fix a conflict issue when testcase name duplicated between different class

View File

@@ -9,13 +9,13 @@
## 在Maven项目中使用
在项目`pom.xml`文件中,增加`testable-processor`依赖和`maven-surefire-plugin`配置,具体方法如下。
在项目`pom.xml`文件中,增加`testable-all`依赖和`maven-surefire-plugin`配置,具体方法如下。
建议先添加一个标识TestableMock版本的`property`,便于统一管理:
```xml
<properties>
<testable.version>0.4.0</testable.version>
<testable.version>0.4.1</testable.version>
</properties>
```
@@ -25,20 +25,14 @@
<dependencies>
<dependency>
<groupId>com.alibaba.testable</groupId>
<artifactId>testable-processor</artifactId>
<version>${testable.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba.testable</groupId>
<artifactId>testable-agent</artifactId>
<artifactId>testable-all</artifactId>
<version>${testable.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
```
最后在`build`区域的`plugins`列表里添加`maven-surefire-plugin`插件(如果已此插件则只需添加`<argLine>`部分配置):
最后在`build`区域的`plugins`列表里添加`maven-surefire-plugin`插件(如果已包含此插件则只需添加`<argLine>`部分配置):
```xml
<build>
@@ -68,8 +62,8 @@
```groovy
dependencies {
testImplementation('com.alibaba.testable:testable-all:0.4.0')
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.4.0')
testImplementation('com.alibaba.testable:testable-all:0.4.1')
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.4.1')
}
```

View File

@@ -12,13 +12,13 @@
5. ... ...
> 不返回任何值也不产生任何"副作用"的方法没有存在意义。
> 不返回任何值也不产生任何"副作用"的方法没有存在意义。
这些"副作用"归纳来说可分为两类:**修改外部变量**和**调用外部方法**。
这些"副作用"的本质归纳来说可分为两类:**修改外部变量**和**调用外部方法**。
通过TestableMock的私有字段访问和Mock校验器可以很方便的实现对"副作用"的结果检查。
#### 修改外部变量的void方法
### 1. 修改外部变量的void方法
例如,下面这个方法会根据输入修改私有成员变量`hashCache`
@@ -55,7 +55,7 @@ class DemoTest {
}
```
#### 调用外部方法的void方法
### 2. 调用外部方法的void方法
例如,下面这个方法会根据输入打印信息到控制台:
@@ -70,6 +70,7 @@ class Demo {
```
若要测试此方法可以利用TestableMock快速Mock掉`System.out.println`方法。在Mock方法体里可以继续执行原调用相当于并不影响本来方法功能仅用于做调用记录也可以直接留空相当于去除了原方法的副作用
在执行完被测的void类型方法以后`InvokeVerifier.verify()`校验传入的打印内容是否符合预期:
```java

View File

@@ -1,7 +1,7 @@
自助问题排查
---
相比Mockito等由开发者手工放置Mock类的做法TestableMock使用方法名和参数类型匹配自动寻找需Mock的调用。这种机制在带来方便的同时也有可能发生预料之外的Mock替换。
相比`Mockito`等由开发者手工放置Mock类的做法`TestableMock`使用方法名和参数类型匹配自动寻找需Mock的调用。这种机制在带来方便的同时也有可能发生预料之外的Mock替换。
若要排查Mock相关的问题只需在测试类上添加`@MockWith`注解,并配置参数`diagnose`值为`MockDiagnose.ENABLE`在运行测试时就会打印出详细的Mock方法替换过程。

View File

@@ -1,7 +1,7 @@
快速Mock被测类的任意方法调用
---
相比以往Mock工具以类为粒度的Mock方式TestableMock允许用户直接定义需要Mock的单个方法并遵循约定优于配置的原则按照规则自动在测试运行时替换被测方法中的指定方法调用。
相比以往Mock工具以类为粒度的Mock方式`TestableMock`允许用户直接定义需要Mock的单个方法并遵循约定优于配置的原则按照规则自动在测试运行时替换被测方法中的指定方法调用。
具体的Mock方法定义约定如下
@@ -60,7 +60,7 @@ private String innerFunc(DemoMock self, String text) {
#### 3. 覆写任意类的静态方法
对于静态方法的Mock与普通方法相同。但需要注意的是对于静态方法,传入Mock方法的第一个参数实际值始终是`null`
对于静态方法的Mock与普通方法相同。但需要注意的是静态方法Mock方法被调用时,传入的第一个参数实际值始终是`null`
例如,在被测类中调用了`BlackBox`类型中的静态方法`secretBox()`,改方法签名为`BlackBox secretBox()`则Mock方法如下
@@ -78,7 +78,7 @@ private BlackBox secretBox(BlackBox ignore) {
#### 4. 覆写任意类的new操作
在测试类里定义一个有`@MockContructor`注解的普通方法使该方法返回值类型为要被创建的对象类型且与要Mock的构造函数参数完全一致方法名称随意。
在测试类里定义一个有`@MockContructor`注解的普通方法,使该方法返回值类型为要被创建的对象类型,且方法参数与要Mock的构造函数参数完全一致方法名称随意。
此时被测类中所有用`new`创建指定类的操作并使用了与Mock方法参数一致的构造函数将被替换为对该自定义方法的调用。
@@ -93,7 +93,7 @@ private BlackBox createBlackBox(String text) {
}
```
> 也可以使用`@MockMethod`注解,并`targetMethod`参数为"<init>",其余同上效果与`@MockContructor`注解相同
> 也可以依然使用`@MockMethod`注解,并配置`targetMethod`参数为"`<init>`",其余同上效果与使用`@MockContructor`注解相同
完整代码示例见`java-demo``kotlin-demo`示例项目中的`should_able_to_mock_new_object()`测试用例。

View File

@@ -10,5 +10,6 @@
- [自助问题排查](zh-cn/doc/troubleshooting.md)
- [Testable Maven插件](zh-cn/doc/use-maven-plugin.md)
- 其他文档
- 技术参考
- [业界工具对比](zh-cn/doc/comparation.md)
- [Release Note](zh-cn/doc/release-note.md)

View File

@@ -6,7 +6,7 @@
<parent>
<groupId>com.alibaba.testable</groupId>
<artifactId>testable-parent</artifactId>
<version>0.4.0</version>
<version>0.4.1</version>
<relativePath>../testable-parent</relativePath>
</parent>
<artifactId>testable-agent</artifactId>

View File

@@ -6,7 +6,7 @@
<parent>
<groupId>com.alibaba.testable</groupId>
<artifactId>testable-parent</artifactId>
<version>0.4.0</version>
<version>0.4.1</version>
<relativePath>../testable-parent</relativePath>
</parent>
<artifactId>testable-all</artifactId>

View File

@@ -6,7 +6,7 @@
<parent>
<groupId>com.alibaba.testable</groupId>
<artifactId>testable-parent</artifactId>
<version>0.4.0</version>
<version>0.4.1</version>
<relativePath>../testable-parent</relativePath>
</parent>
<artifactId>testable-core</artifactId>

View File

@@ -6,7 +6,7 @@
<parent>
<groupId>com.alibaba.testable</groupId>
<artifactId>testable-parent</artifactId>
<version>0.4.0</version>
<version>0.4.1</version>
<relativePath>../testable-parent</relativePath>
</parent>
<artifactId>testable-maven-plugin</artifactId>

View File

@@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba.testable</groupId>
<artifactId>testable-parent</artifactId>
<version>0.4.0</version>
<version>0.4.1</version>
<packaging>pom</packaging>
<name>testable-parent</name>
<description>Unit test enhancement toolkit</description>
@@ -42,7 +42,7 @@
<plugin.gpg.version>1.6</plugin.gpg.version>
<plugin.staging.version>1.6.8</plugin.staging.version>
<plugin.maven.version>3.6.0</plugin.maven.version>
<testable.version>0.4.0</testable.version>
<testable.version>0.4.1</testable.version>
</properties>
<profiles>

View File

@@ -6,7 +6,7 @@
<parent>
<groupId>com.alibaba.testable</groupId>
<artifactId>testable-parent</artifactId>
<version>0.4.0</version>
<version>0.4.1</version>
<relativePath>../testable-parent</relativePath>
</parent>
<artifactId>testable-processor</artifactId>