mirror of
https://github.com/alibaba/testable-mock.git
synced 2026-08-21 02:33:31 +08:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
db4e02a201 | ||
|
|
fda7ee23c7 | ||
|
|
140e54983b | ||
|
|
548d7ba21f |
@@ -9,7 +9,7 @@
|
||||
|
||||
阅读[这里](https://mp.weixin.qq.com/s/KyU6Eu7mDkZU8FspfSqfMw)了解更多故事。
|
||||
|
||||
<font size="5">**0.6版本已发布**</font>,从`0.5.x`升级到`0.6.x`版本请参考[版本升级指南](https://alibaba.github.io/testable-mock/#/zh-cn/doc/upgrade-guide)
|
||||
<font size="5">**0.7版本已发布**</font>,从`0.6.x`升级到`0.7.x`版本请参考[版本升级指南](https://alibaba.github.io/testable-mock/#/zh-cn/doc/upgrade-guide)
|
||||
|
||||
-----
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ dependencies {
|
||||
testImplementation 'androidx.test:runner:1.4.0-alpha05'
|
||||
testImplementation 'junit:junit:4.+'
|
||||
testImplementation 'org.robolectric:robolectric:4.5.1'
|
||||
testImplementation 'com.alibaba.testable:testable-all:0.6.10'
|
||||
testImplementation 'com.alibaba.testable:testable-all:0.7.1'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
||||
}
|
||||
@@ -13,8 +13,8 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
|
||||
testImplementation('com.alibaba.testable:testable-all:0.6.10')
|
||||
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.6.10')
|
||||
testImplementation('com.alibaba.testable:testable-all:0.7.1')
|
||||
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.7.1')
|
||||
}
|
||||
|
||||
tasks.withType(JavaCompile) {
|
||||
|
||||
@@ -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.6.10</testable.version>
|
||||
<testable.version>0.7.1</testable.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.alibaba.demo.basic.issue;
|
||||
|
||||
public abstract class AbstractServiceA {
|
||||
|
||||
public String put(Object input) {
|
||||
return "object";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.alibaba.demo.basic.issue;
|
||||
|
||||
public class ServiceA extends AbstractServiceA {
|
||||
|
||||
public String get(Object input) {
|
||||
return super.put(input);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.alibaba.demo.basic.issue;
|
||||
|
||||
import com.alibaba.testable.core.annotation.MockInvoke;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ServiceATest {
|
||||
|
||||
private ServiceA sa = new ServiceA();
|
||||
|
||||
public static class Mock {
|
||||
@MockInvoke(targetClass = AbstractServiceA.class, targetMethod = "put")
|
||||
public String put(Object input) {
|
||||
return "mocked";
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void get() {
|
||||
assertEquals("mocked", sa.get(123));
|
||||
}
|
||||
}
|
||||
@@ -17,8 +17,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.6.10")
|
||||
testAnnotationProcessor("com.alibaba.testable:testable-processor:0.6.10")
|
||||
testImplementation("com.alibaba.testable:testable-all:0.7.1")
|
||||
testAnnotationProcessor("com.alibaba.testable:testable-processor:0.7.1")
|
||||
}
|
||||
|
||||
tasks.withType<KotlinCompile> {
|
||||
|
||||
@@ -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.6.10</testable.version>
|
||||
<testable.version>0.7.1</testable.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -14,8 +14,8 @@ repositories {
|
||||
dependencies {
|
||||
testImplementation 'org.codehaus.groovy:groovy-all:3.0.7'
|
||||
testImplementation 'org.spockframework:spock-core:2.0-M5-groovy-3.0'
|
||||
testImplementation('com.alibaba.testable:testable-all:0.6.10')
|
||||
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.6.10')
|
||||
testImplementation('com.alibaba.testable:testable-all:0.7.1')
|
||||
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.7.1')
|
||||
}
|
||||
|
||||
tasks.withType(JavaCompile) {
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
<java.version>1.8</java.version>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<testable.version>0.6.10</testable.version>
|
||||
<testable.version>0.7.1</testable.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
# Release Note
|
||||
|
||||
## 0.7.1
|
||||
- fix an issue cause mock class unrecognized after package-mapping
|
||||
|
||||
## 0.7.0
|
||||
- fix an improper exception issue when mock method with `associated` scope invoked
|
||||
- rename type `InvokeVerifier`/`InvockeMatcher` to `InvocationVerifier`/`InvocationMatcher`
|
||||
|
||||
@@ -16,7 +16,7 @@ It is recommended to add a `property` field that identifies the TestableMock ver
|
||||
|
||||
```xml
|
||||
<properties>
|
||||
<testable.version>0.6.10</testable.version>
|
||||
<testable.version>0.7.1</testable.version>
|
||||
</properties>
|
||||
```
|
||||
|
||||
@@ -63,8 +63,8 @@ Add dependence of `TestableMock` in `build.gradle` file:
|
||||
|
||||
```groovy
|
||||
dependencies {
|
||||
testImplementation('com.alibaba.testable:testable-all:0.6.10')
|
||||
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.6.10')
|
||||
testImplementation('com.alibaba.testable:testable-all:0.7.1')
|
||||
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.7.1')
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -1,6 +1,15 @@
|
||||
Upgrade Guide
|
||||
---
|
||||
|
||||
#### Upgrade to 0.7
|
||||
|
||||
In version `0.7` mock related annotations and methods are renamed, in order to avoid naming conflict and misunderstanding.
|
||||
|
||||
1. The `verify()` used for check mock method invocation status has been renamed to `verifyInvoked()`, as there are several static methods also named `verify()` in `com.sun` package.
|
||||
2. The annotation `@MockMethod` and `@MockConstructor` used for mark mock replacement method has been renamed to `@MockInvoke` and `@MockNew`, which is more suitable for its real usage.
|
||||
|
||||
The usage of those annotations and method has no change for below and above version `0.7.0`, so during the upgrading only rename is required.
|
||||
|
||||
### Upgrade to v0.6
|
||||
|
||||
In version `0.6`,`TestableMock` provided a new [quick complicated class construction](en-us/doc/omni-constructor.md) capability. Meanwhile, it includes a modification that is incompatible with the `0.5` version: class `PrivateAccessor` have been moved from `com.alibaba.testable.core.accessor` package to `com.alibaba.testable.core.tool` package.
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
# Release Note
|
||||
|
||||
## 0.7.1
|
||||
- 修复包路径映射时后识别不到Mock类的问题(issue-248)
|
||||
|
||||
## 0.7.0
|
||||
- 修复当`scope`为`associated`的Mock方法被`null`对象调用,且上下文与测试用例未关联时抛错不合理的问题(issue-163)
|
||||
- 类型`InvokeVerifier`和`InvockeMatcher`更名为`InvocationVerifier`和`InvocationMatcher`
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
```xml
|
||||
<properties>
|
||||
<testable.version>0.6.10</testable.version>
|
||||
<testable.version>0.7.1</testable.version>
|
||||
</properties>
|
||||
```
|
||||
|
||||
@@ -63,8 +63,8 @@
|
||||
|
||||
```groovy
|
||||
dependencies {
|
||||
testImplementation('com.alibaba.testable:testable-all:0.6.10')
|
||||
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.6.10')
|
||||
testImplementation('com.alibaba.testable:testable-all:0.7.1')
|
||||
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.7.1')
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -1,6 +1,15 @@
|
||||
版本升级说明
|
||||
---
|
||||
|
||||
#### 升级到0.7.x版本
|
||||
|
||||
在`0.7`版本重命名了Mock相关的注解和方法,以避免命名冲突和由于名字带来的理解歧义,包括两部分变化。
|
||||
|
||||
1. 用于验证Mock方法调用的`verify()`方法与包括`com.sun`包在内的多个JVM内置静态方法重名,在IDE自动添加`import`时会有误选的情况,该方法在新版本中更名为`verifyInvoked()`,同时所属的类型更名为`InvocationVerifier`。
|
||||
2. 注解`@MockMethod`和`@MockConstructor`的实际作用是对方法调用和New操作进行Mock,而其名称容易使人误解为是对方法和构造器本身进行了Mock,在新版本中这两个注解分别已被分别更名为`@MockInvoke`和`@MockNew`。
|
||||
|
||||
这些注解和方法的使用与之前版本并无差异,从`0.6.x`版本升级到`0.7.0`及以上版本时,请全局替换相关名称即可。
|
||||
|
||||
### 升级到0.6.x版本
|
||||
|
||||
在`0.6`版本中,`TestableMock`提供了[快速构造复杂参数对象](zh-cn/doc/omni-constructor.md)的能力,同时包含一处与`0.5`版本不兼容的修改,`PrivateAccessor`类型的包路径从`com.alibaba.testable.core.accessor`移到了`com.alibaba.testable.core.tool`。
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>com.alibaba.testable</groupId>
|
||||
<artifactId>testable-parent</artifactId>
|
||||
<version>0.6.10</version>
|
||||
<version>0.7.1</version>
|
||||
<relativePath>../testable-parent</relativePath>
|
||||
</parent>
|
||||
<artifactId>testable-agent</artifactId>
|
||||
|
||||
@@ -63,15 +63,14 @@ public class TestableClassTransformer implements ClassFileTransformer {
|
||||
}
|
||||
|
||||
private byte[] transformMock(byte[] bytes, ClassNode cn) {
|
||||
String className = (GlobalConfig.getMockPackageMapping() == null) ? cn.name : mapPackage(cn.name);
|
||||
try {
|
||||
if (mockClassParser.isMockClass(cn)) {
|
||||
// it's a mock class
|
||||
bytes = new MockClassHandler(className).getBytes(bytes);
|
||||
bytes = new MockClassHandler(cn.name).getBytes(bytes);
|
||||
BytecodeUtil.dumpByte(cn, GlobalConfig.getDumpPath(), bytes);
|
||||
return bytes;
|
||||
}
|
||||
String mockClass = foundMockForSourceClass(className);
|
||||
String mockClass = foundMockForSourceClass(cn.name);
|
||||
if (mockClass != null) {
|
||||
// it's a source class with testable enabled
|
||||
List<MethodInfo> injectMethods = mockClassParser.getTestableMockMethods(mockClass);
|
||||
@@ -92,7 +91,7 @@ public class TestableClassTransformer implements ClassFileTransformer {
|
||||
LogUtil.error("Invalid mock method %s::%s - %s", e.getClassName(), e.getMethodName(), e.getMessage());
|
||||
System.exit(0);
|
||||
} catch (Throwable t) {
|
||||
LogUtil.warn("Failed to transform class " + className);
|
||||
LogUtil.warn("Failed to transform class " + cn.name);
|
||||
LogUtil.warn(t.toString());
|
||||
LogUtil.warn(ThreadUtil.getFirstRelatedStackLine(t));
|
||||
} finally {
|
||||
@@ -102,18 +101,8 @@ public class TestableClassTransformer implements ClassFileTransformer {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
private String mapPackage(String name) {
|
||||
String dotSeparatedName = ClassUtil.toDotSeparatedName(name);
|
||||
for (String prefix : GlobalConfig.getMockPackageMapping().keySet()) {
|
||||
if (dotSeparatedName.startsWith(prefix)) {
|
||||
return ClassUtil.toSlashSeparatedName(GlobalConfig.getMockPackageMapping().get(prefix))
|
||||
+ name.substring(prefix.length());
|
||||
}
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
private String foundMockForSourceClass(String className) {
|
||||
private String foundMockForSourceClass(String name) {
|
||||
String className = (GlobalConfig.getMockPackageMapping() == null) ? name : mapPackage(name);
|
||||
String mockClass = lookForMockWithAnnotationAsSourceClass(className);
|
||||
if (mockClass != null) {
|
||||
return mockClass;
|
||||
@@ -125,6 +114,17 @@ public class TestableClassTransformer implements ClassFileTransformer {
|
||||
return foundMockForInnerSourceClass(className);
|
||||
}
|
||||
|
||||
private String mapPackage(String name) {
|
||||
String dotSeparatedName = ClassUtil.toDotSeparatedName(name);
|
||||
for (String prefix : GlobalConfig.getMockPackageMapping().keySet()) {
|
||||
if (dotSeparatedName.startsWith(prefix)) {
|
||||
return ClassUtil.toSlashSeparatedName(GlobalConfig.getMockPackageMapping().get(prefix))
|
||||
+ name.substring(prefix.length());
|
||||
}
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
private String foundMockForInnerSourceClass(String className) {
|
||||
return (className.contains(DOLLAR) && !className.endsWith(KOTLIN_POSTFIX_COMPANION)) ?
|
||||
foundMockForTestClass(ClassUtil.getTestClassName(className.substring(0, className.indexOf(DOLLAR)))) : null;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>com.alibaba.testable</groupId>
|
||||
<artifactId>testable-parent</artifactId>
|
||||
<version>0.6.10</version>
|
||||
<version>0.7.1</version>
|
||||
<relativePath>../testable-parent</relativePath>
|
||||
</parent>
|
||||
<artifactId>testable-all</artifactId>
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>com.alibaba.testable</groupId>
|
||||
<artifactId>testable-parent</artifactId>
|
||||
<version>0.6.10</version>
|
||||
<version>0.7.1</version>
|
||||
<relativePath>../testable-parent</relativePath>
|
||||
</parent>
|
||||
<artifactId>testable-core</artifactId>
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>com.alibaba.testable</groupId>
|
||||
<artifactId>testable-parent</artifactId>
|
||||
<version>0.6.10</version>
|
||||
<version>0.7.1</version>
|
||||
<relativePath>../testable-parent</relativePath>
|
||||
</parent>
|
||||
<artifactId>testable-maven-plugin</artifactId>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.alibaba.testable</groupId>
|
||||
<artifactId>testable-parent</artifactId>
|
||||
<version>0.6.10</version>
|
||||
<version>0.7.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.6.10</testable.version>
|
||||
<testable.version>0.7.1</testable.version>
|
||||
</properties>
|
||||
|
||||
<profiles>
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>com.alibaba.testable</groupId>
|
||||
<artifactId>testable-parent</artifactId>
|
||||
<version>0.6.10</version>
|
||||
<version>0.7.1</version>
|
||||
<relativePath>../testable-parent</relativePath>
|
||||
</parent>
|
||||
<artifactId>testable-processor</artifactId>
|
||||
|
||||
Reference in New Issue
Block a user