Compare commits
212 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fd3b088f59 | ||
|
|
e812aa6126 | ||
|
|
b949f57d54 | ||
|
|
0551240c7d | ||
|
|
8a3a0cf6bf | ||
|
|
d3ab246c78 | ||
|
|
142b0e5a1e | ||
|
|
38221a67ef | ||
|
|
993c178adf | ||
|
|
3786c6251e | ||
|
|
e5f8e5244c | ||
|
|
e13dce7ac6 | ||
|
|
3265803dec | ||
|
|
ef3891bd57 | ||
|
|
fcbdb7275b | ||
|
|
ea4c6c5b3e | ||
|
|
381ff52515 | ||
|
|
c5f39dfae3 | ||
|
|
7f8ef70e57 | ||
|
|
23af64475a | ||
|
|
7181b49211 | ||
|
|
cd7b9cc922 | ||
|
|
46288fa0e9 | ||
|
|
e61597ba8b | ||
|
|
c577301493 | ||
|
|
a33908db41 | ||
|
|
4ea97fe13f | ||
|
|
7cda8ffd36 | ||
|
|
1da98a0364 | ||
|
|
c6d892da2d | ||
|
|
d1f2c3da97 | ||
|
|
f40cfe929a | ||
|
|
96d1f9d633 | ||
|
|
350802fc9b | ||
|
|
0ae5163435 | ||
|
|
0e581a7eeb | ||
|
|
b68ff99800 | ||
|
|
7754f828cc | ||
|
|
3d62c91291 | ||
|
|
14c3d841c5 | ||
|
|
5f6cf3888c | ||
|
|
75c6a67b8f | ||
|
|
d11402dd6c | ||
|
|
db771b9c66 | ||
|
|
338c7828c8 | ||
|
|
8ebe2a5d0c | ||
|
|
4f3e3f0093 | ||
|
|
9924d7d2fc | ||
|
|
9f5867ed76 | ||
|
|
be0a580a04 | ||
|
|
78d7ee21a6 | ||
|
|
af409a65a8 | ||
|
|
a67619d574 | ||
|
|
b89daa22c3 | ||
|
|
e948e625ea | ||
|
|
d463f863db | ||
|
|
31140bef87 | ||
|
|
d2944a88fb | ||
|
|
9d6e4dd1fa | ||
|
|
9c09bf9653 | ||
|
|
6d87653fa2 | ||
|
|
eec88f4bd9 | ||
|
|
5921ccab07 | ||
|
|
8dde402a8c | ||
|
|
d10478f95f | ||
|
|
0c063e9b1f | ||
|
|
b8771f0dac | ||
|
|
42dd58584b | ||
|
|
ce55bb4c9a | ||
|
|
cff2d1ae5a | ||
|
|
5904ad99cb | ||
|
|
74fbc5b53f | ||
|
|
f6b5bab250 | ||
|
|
f2feb3cc68 | ||
|
|
b41923bfa0 | ||
|
|
7c1f18de46 | ||
|
|
c393d40d0d | ||
|
|
d4a69528a7 | ||
|
|
a46e73a421 | ||
|
|
01e9890234 | ||
|
|
93cdef1d6c | ||
|
|
e2e986e50d | ||
|
|
f5475aa18e | ||
|
|
844323af3a | ||
|
|
cc2deeff1c | ||
|
|
14e4d2db5b | ||
|
|
c9c8ba6c24 | ||
|
|
b5c0afb3da | ||
|
|
617d372545 | ||
|
|
58da25de53 | ||
|
|
46706ba56c | ||
|
|
1ced04bf29 | ||
|
|
7b2f967626 | ||
|
|
e0a4a39d55 | ||
|
|
b4da23a90e | ||
|
|
c1659242b6 | ||
|
|
929abffb66 | ||
|
|
e370114b01 | ||
|
|
26d3178840 | ||
|
|
55ccc385d5 | ||
|
|
20b346ed34 | ||
|
|
00055a4626 | ||
|
|
27174afa93 | ||
|
|
3d6d3c9dd1 | ||
|
|
df7e641a0c | ||
|
|
3acf341d1f | ||
|
|
52878364ee | ||
|
|
9871a96db0 | ||
|
|
9b56e3d64a | ||
|
|
ea8f305152 | ||
|
|
3feba7fdfa | ||
|
|
9064f7a582 | ||
|
|
b062dbc592 | ||
|
|
a623bd0a33 | ||
|
|
a54bb33c09 | ||
|
|
bbc41f2987 | ||
|
|
7b9a0964bc | ||
|
|
a0dae0dfe1 | ||
|
|
f54a6ce022 | ||
|
|
606760d632 | ||
|
|
206fc5486e | ||
|
|
af3af6b272 | ||
|
|
fe7420c99a | ||
|
|
7857782125 | ||
|
|
f776deff0e | ||
|
|
328296ae3a | ||
|
|
a5038320b4 | ||
|
|
bdd16c0948 | ||
|
|
dce1accafb | ||
|
|
2865093fef | ||
|
|
5f69b21532 | ||
|
|
250a1beaf9 | ||
|
|
323ab398b2 | ||
|
|
edd4c24d88 | ||
|
|
4995bbd9a9 | ||
|
|
121536a337 | ||
|
|
b163152c2f | ||
|
|
5897e087c8 | ||
|
|
6f079db3f3 | ||
|
|
40073ad784 | ||
|
|
e46ec3d98a | ||
|
|
1849cf85c5 | ||
|
|
f8450d7047 | ||
|
|
196dffc73d | ||
|
|
1ff6ec5961 | ||
|
|
e50633b884 | ||
|
|
2976ca0211 | ||
|
|
24c6a9cc5c | ||
|
|
9e7ceb2dc1 | ||
|
|
8dfd0b4516 | ||
|
|
190cf4cc3c | ||
|
|
d49f526376 | ||
|
|
f01ff10fba | ||
|
|
f64b07e9da | ||
|
|
f96d272f97 | ||
|
|
b1e6ad58cc | ||
|
|
48d6ef328b | ||
|
|
84df2a0fd8 | ||
|
|
f9621a6f2e | ||
|
|
55ccabbb39 | ||
|
|
cfc28467bb | ||
|
|
23a0a0cd90 | ||
|
|
a1086ff402 | ||
|
|
6237e19a32 | ||
|
|
df73dac54f | ||
|
|
f660ccb88f | ||
|
|
82d87f3447 | ||
|
|
65cc631d39 | ||
|
|
bdd99577c4 | ||
|
|
8be5550331 | ||
|
|
6a5c817a3a | ||
|
|
c76ed2d84a | ||
|
|
14549040d9 | ||
|
|
03ec857cd0 | ||
|
|
b4a29e7e8e | ||
|
|
02c74299f4 | ||
|
|
4c6fc5b228 | ||
|
|
6d6e2ecb7f | ||
|
|
c364e96f77 | ||
|
|
dab1d36a81 | ||
|
|
01bd676df7 | ||
|
|
02d7fb6e91 | ||
|
|
d8ffcacdaf | ||
|
|
be53ea2d9c | ||
|
|
9eb8682ec0 | ||
|
|
2a54fe70dd | ||
|
|
790409a336 | ||
|
|
189ef43565 | ||
|
|
34f1dc8f73 | ||
|
|
b60ebf2f2c | ||
|
|
d95cba6d37 | ||
|
|
5ef06c4bde | ||
|
|
8814276d63 | ||
|
|
374a4d5442 | ||
|
|
9a22f361f3 | ||
|
|
94c5aa8621 | ||
|
|
74e3cf16a4 | ||
|
|
19f28c32db | ||
|
|
affa56d057 | ||
|
|
f1edf92626 | ||
|
|
6f9accf319 | ||
|
|
cefc5fb1df | ||
|
|
948878687c | ||
|
|
d626906d28 | ||
|
|
705ba796f1 | ||
|
|
7e44a66205 | ||
|
|
d57dedb6fc | ||
|
|
d860684e91 | ||
|
|
650b385785 | ||
|
|
f9be92e9d0 | ||
|
|
019e407a4f | ||
|
|
453908cd46 |
1
.gitignore
vendored
@@ -1,5 +1,6 @@
|
|||||||
# maven ignore
|
# maven ignore
|
||||||
target/
|
target/
|
||||||
|
.mvn/
|
||||||
|
|
||||||
# gradle ignore
|
# gradle ignore
|
||||||
build/
|
build/
|
||||||
|
|||||||
18
README.md
@@ -2,16 +2,16 @@
|
|||||||
|
|
||||||
换种思路写Mock,让单元测试更简单。
|
换种思路写Mock,让单元测试更简单。
|
||||||
|
|
||||||
无需初始化,不挑测试框架,甭管要换的是私有方法、静态方法、构造方法还是其他任何类的任何方法,也甭管要换的对象是怎么创建的。写好Mock定义,加个`@MockMethod`注解,一切统统搞定。
|
无需初始化,不挑服务框架,甭管要换的是私有方法、静态方法、构造方法还是其他任何类的任何方法,也甭管要换的对象是怎么创建的。写好Mock定义,加个`@MockMethod`注解,一切统统搞定。
|
||||||
|
|
||||||
- 文档:https://alibaba.github.io/testable-mock/
|
- 文档:https://alibaba.github.io/testable-mock/
|
||||||
- 国内文档镜像:http://freyrlin.gitee.io/testable-mock/
|
- 国内文档镜像:http://freyrlin.gitee.io/testable-mock/
|
||||||
|
|
||||||
阅读[这里](https://mp.weixin.qq.com/s/KyU6Eu7mDkZU8FspfSqfMw)了解更多故事。
|
阅读[这里](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)
|
||||||
> 1. 如遇到"Attempt to access non-static member in mock method"错误,参见[常见问题](https://alibaba.github.io/testable-mock/#/zh-cn/doc/frequently-asked-questions)第8条
|
|
||||||
> 2. 如果有遇到其他任何使用问题和建议,请直接在[Issue](https://github.com/alibaba/testable-mock/issues)中提出,也可通过[Pull Request](https://github.com/alibaba/testable-mock/pulls)提交您的代码,我们将在24小时内回复并处理
|
如果有遇到其他任何使用问题和建议,请直接在[Issues](https://github.com/alibaba/testable-mock/issues)中提出,也可通过[Pull Request](https://github.com/alibaba/testable-mock/pulls)提交您的代码,我们将在24小时内回复并处理
|
||||||
|
|
||||||
-----
|
-----
|
||||||
|
|
||||||
@@ -19,9 +19,12 @@
|
|||||||
|
|
||||||
`TestableMock`正在持续迭代演进,以下版本计划可能在开发过程中发生调整
|
`TestableMock`正在持续迭代演进,以下版本计划可能在开发过程中发生调整
|
||||||
|
|
||||||
- `0.4` 当前版本,进行中的工作内容参考[Issue](https://github.com/alibaba/testable-mock/issues)清单
|
- `0.6` 当前版本,正在开发中的功能包括:
|
||||||
- `0.5` 实现以"独立Mock类"为单元的Mock方法复用机制,让测试类之间可以方便复用相同的Mock方法
|
- 支持Mock Lambada语句中的方法引用 [#36](https://github.com/alibaba/testable-mock/issues/36)
|
||||||
- `0.6` 实现将Mock方法的默认生效范围缩小至当前被测类,避免Mock方法在测试类之间相互影响
|
- 其他进行中的工作内容参考[Issue](https://github.com/alibaba/testable-mock/issues)清单
|
||||||
|
- `0.7` 主要计划包括:
|
||||||
|
- 增加DAO层逻辑的单元测试辅助 [介绍](https://alibaba.github.io/testable-mock/#/zh-cn/doc/verify-sql)
|
||||||
|
- 支持快速Mock指定类型的所有方法 [#82](https://github.com/alibaba/testable-mock/issues/82)
|
||||||
- `1.0` 功能稳定,一个崭新的开始
|
- `1.0` 功能稳定,一个崭新的开始
|
||||||
|
|
||||||
## 目录结构
|
## 目录结构
|
||||||
@@ -33,6 +36,7 @@
|
|||||||
|-- testable-agent ➜ JavaAgent模块,提供Mock测试相关功能
|
|-- testable-agent ➜ JavaAgent模块,提供Mock测试相关功能
|
||||||
|-- testable-core ➜ 基础功能模块,提供Mock相关注解和工具类
|
|-- testable-core ➜ 基础功能模块,提供Mock相关注解和工具类
|
||||||
|-- testable-maven-plugin ➜ Maven插件模块,用于简化JavaAgent注入
|
|-- testable-maven-plugin ➜ Maven插件模块,用于简化JavaAgent注入
|
||||||
|
|-- tool ➜ 项目开发过程中的工具脚本
|
||||||
|-- demo
|
|-- demo
|
||||||
| |-- java-demo ➜ Java语言的示例代码
|
| |-- java-demo ➜ Java语言的示例代码
|
||||||
| `-- kotlin-demo ➜ Kotlin语言的示例代码
|
| `-- kotlin-demo ➜ Kotlin语言的示例代码
|
||||||
|
|||||||
@@ -7,13 +7,12 @@ Write a mock method, add an `@MockMethod` annotation, everything is done.
|
|||||||
|
|
||||||
Usage Document: https://alibaba.github.io/testable-mock/#/en-us/
|
Usage Document: https://alibaba.github.io/testable-mock/#/en-us/
|
||||||
|
|
||||||
## Loadmap
|
## Roadmap
|
||||||
|
|
||||||
`TestableMock` is still under heavy development, the following version plans may be adjusted during the iteration
|
`TestableMock` is still under heavy development, the following version plans may be adjusted during the iteration
|
||||||
|
|
||||||
- `v0.4` it's the current version, refer to the [issue](https://github.com/alibaba/testable-mock/issues) list for the work in progress
|
- `v0.6` it's the current version, refer to the [issue](https://github.com/alibaba/testable-mock/issues) list for the work in progress
|
||||||
- `v0.5` implementation mock class inherit mechanism, so that the same mock method can be reused between test classes conveniently
|
- `v0.7` better anti-refactoring support of mocking, allow quick mock all method in specified class
|
||||||
- `v0.6` narrows the default effective scope of mock methods to the class under test, to avoid interaction of mocking cross classes
|
|
||||||
- `v1.0` all functions are stable, a brand-new start
|
- `v1.0` all functions are stable, a brand-new start
|
||||||
|
|
||||||
## Directory Structure
|
## Directory Structure
|
||||||
@@ -25,6 +24,7 @@ Usage Document: https://alibaba.github.io/testable-mock/#/en-us/
|
|||||||
|-- testable-agent ➜ JavaAgent module, provides test mocking related functions
|
|-- testable-agent ➜ JavaAgent module, provides test mocking related functions
|
||||||
|-- testable-core ➜ Basic function module, provides mock related class and annotation
|
|-- testable-core ➜ Basic function module, provides mock related class and annotation
|
||||||
|-- testable-maven-plugin ➜ Maven plugin module, for simplify JavaAgent injection
|
|-- testable-maven-plugin ➜ Maven plugin module, for simplify JavaAgent injection
|
||||||
|
|-- tool ➜ Scripts for project maintain
|
||||||
|-- demo
|
|-- demo
|
||||||
| |-- java-demo ➜ Java code example
|
| |-- java-demo ➜ Java code example
|
||||||
| `-- kotlin-demo ➜ Kotlin code example
|
| `-- kotlin-demo ➜ Kotlin code example
|
||||||
|
|||||||
15
demo/android-demo/.gitignore
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
*.iml
|
||||||
|
.gradle
|
||||||
|
/local.properties
|
||||||
|
/.idea/caches
|
||||||
|
/.idea/libraries
|
||||||
|
/.idea/modules.xml
|
||||||
|
/.idea/workspace.xml
|
||||||
|
/.idea/navEditor.xml
|
||||||
|
/.idea/assetWizardSettings.xml
|
||||||
|
.DS_Store
|
||||||
|
/build
|
||||||
|
/captures
|
||||||
|
.externalNativeBuild
|
||||||
|
.cxx
|
||||||
|
local.properties
|
||||||
1
demo/android-demo/app/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/build
|
||||||
55
demo/android-demo/app/build.gradle
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
plugins {
|
||||||
|
id 'com.android.application'
|
||||||
|
id 'kotlin-android'
|
||||||
|
}
|
||||||
|
|
||||||
|
android {
|
||||||
|
compileSdkVersion 30
|
||||||
|
buildToolsVersion "30.0.3"
|
||||||
|
|
||||||
|
defaultConfig {
|
||||||
|
applicationId "com.alibaba.testable.demo"
|
||||||
|
minSdkVersion 30
|
||||||
|
targetSdkVersion 30
|
||||||
|
versionCode 1
|
||||||
|
versionName "1.0"
|
||||||
|
|
||||||
|
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||||
|
}
|
||||||
|
|
||||||
|
buildTypes {
|
||||||
|
release {
|
||||||
|
minifyEnabled false
|
||||||
|
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
compileOptions {
|
||||||
|
sourceCompatibility JavaVersion.VERSION_1_8
|
||||||
|
targetCompatibility JavaVersion.VERSION_1_8
|
||||||
|
}
|
||||||
|
kotlinOptions {
|
||||||
|
jvmTarget = '1.8'
|
||||||
|
}
|
||||||
|
testOptions {
|
||||||
|
unitTests {
|
||||||
|
includeAndroidResources = true
|
||||||
|
all {
|
||||||
|
jvmArgs "-javaagent:${classpath.find { it.name.contains("testable-agent") }.absolutePath}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||||
|
implementation 'androidx.core:core-ktx:1.2.0'
|
||||||
|
implementation 'androidx.appcompat:appcompat:1.3.0-rc01'
|
||||||
|
implementation 'com.google.android.material:material:1.1.0'
|
||||||
|
testImplementation 'androidx.test:core:1.4.0-alpha05'
|
||||||
|
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.7'
|
||||||
|
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
||||||
|
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
||||||
|
}
|
||||||
21
demo/android-demo/app/proguard-rules.pro
vendored
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
# Add project specific ProGuard rules here.
|
||||||
|
# You can control the set of applied configuration files using the
|
||||||
|
# proguardFiles setting in build.gradle.
|
||||||
|
#
|
||||||
|
# For more details, see
|
||||||
|
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||||
|
|
||||||
|
# If your project uses WebView with JS, uncomment the following
|
||||||
|
# and specify the fully qualified class name to the JavaScript interface
|
||||||
|
# class:
|
||||||
|
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||||
|
# public *;
|
||||||
|
#}
|
||||||
|
|
||||||
|
# Uncomment this to preserve the line number information for
|
||||||
|
# debugging stack traces.
|
||||||
|
#-keepattributes SourceFile,LineNumberTable
|
||||||
|
|
||||||
|
# If you keep the line number information, uncomment this to
|
||||||
|
# hide the original source file name.
|
||||||
|
#-renamesourcefileattribute SourceFile
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.alibaba.testable.demo
|
||||||
|
|
||||||
|
import androidx.test.platform.app.InstrumentationRegistry
|
||||||
|
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||||
|
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
|
||||||
|
import org.junit.Assert.*
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instrumented test, which will execute on an Android device.
|
||||||
|
*
|
||||||
|
* See [testing documentation](http://d.android.com/tools/testing).
|
||||||
|
*/
|
||||||
|
@RunWith(AndroidJUnit4::class)
|
||||||
|
class ExampleInstrumentedTest {
|
||||||
|
@Test
|
||||||
|
fun useAppContext() {
|
||||||
|
// Context of the app under test.
|
||||||
|
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
|
||||||
|
assertEquals("com.alibaba.testable.demo", appContext.packageName)
|
||||||
|
}
|
||||||
|
}
|
||||||
13
demo/android-demo/app/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
package="com.alibaba.testable.demo">
|
||||||
|
|
||||||
|
<application
|
||||||
|
android:allowBackup="true"
|
||||||
|
android:icon="@mipmap/ic_launcher"
|
||||||
|
android:label="@string/app_name"
|
||||||
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
|
android:supportsRtl="true"
|
||||||
|
android:theme="@style/Theme.TestableAndroidDemo" />
|
||||||
|
|
||||||
|
</manifest>
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package com.alibaba.testable.demo;
|
||||||
|
|
||||||
|
import com.alibaba.testable.demo.model.BlackBox;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 演示基本的Mock功能
|
||||||
|
* Demonstrate basic mock functionality
|
||||||
|
*/
|
||||||
|
public class DemoBasic {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* method with new operation
|
||||||
|
*/
|
||||||
|
public String newFunc() {
|
||||||
|
BlackBox component = new BlackBox("something");
|
||||||
|
return component.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* method with member method invoke
|
||||||
|
*/
|
||||||
|
public String outerFunc(String s) throws Exception {
|
||||||
|
return "{ \"res\": \"" + innerFunc(s) + staticFunc() + "\"}";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* method with common method invoke
|
||||||
|
*/
|
||||||
|
public String commonFunc() {
|
||||||
|
return "anything".trim() + "__" + "anything".substring(1, 2) + "__" + "abc".startsWith("ab");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* method with static method invoke
|
||||||
|
*/
|
||||||
|
public BlackBox getBox() {
|
||||||
|
return BlackBox.secretBox();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* two methods invoke same private method
|
||||||
|
*/
|
||||||
|
public String callerOne() {
|
||||||
|
return callFromDifferentMethod();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String callerTwo() {
|
||||||
|
return callFromDifferentMethod();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String staticFunc() {
|
||||||
|
return "_STATIC_TAIL";
|
||||||
|
}
|
||||||
|
|
||||||
|
private String innerFunc(String s) {
|
||||||
|
return "Nothing";
|
||||||
|
}
|
||||||
|
|
||||||
|
private String callFromDifferentMethod() {
|
||||||
|
return "realOne";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package com.alibaba.testable.demo;
|
||||||
|
|
||||||
|
import android.app.Service;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.os.IBinder;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 演示与Robolectric配合使用
|
||||||
|
* Demonstrate using mock along with Robolectric
|
||||||
|
*/
|
||||||
|
public class DemoService extends Service {
|
||||||
|
|
||||||
|
static final String TAG = "DemoService";
|
||||||
|
SharedPreferences mPreference;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IBinder onBind(Intent intent) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
|
if(intent != null && "start_foreground".equalsIgnoreCase(intent.getAction())) {
|
||||||
|
Log.d(TAG, "start service.");
|
||||||
|
} else if(intent != null && "stop_foreground".equalsIgnoreCase(intent.getAction())) {
|
||||||
|
Log.d(TAG, "stop service.");
|
||||||
|
}
|
||||||
|
return START_REDELIVER_INTENT;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:aapt="http://schemas.android.com/aapt"
|
||||||
|
android:width="108dp"
|
||||||
|
android:height="108dp"
|
||||||
|
android:viewportWidth="108"
|
||||||
|
android:viewportHeight="108">
|
||||||
|
<path android:pathData="M31,63.928c0,0 6.4,-11 12.1,-13.1c7.2,-2.6 26,-1.4 26,-1.4l38.1,38.1L107,108.928l-32,-1L31,63.928z">
|
||||||
|
<aapt:attr name="android:fillColor">
|
||||||
|
<gradient
|
||||||
|
android:endX="85.84757"
|
||||||
|
android:endY="92.4963"
|
||||||
|
android:startX="42.9492"
|
||||||
|
android:startY="49.59793"
|
||||||
|
android:type="linear">
|
||||||
|
<item
|
||||||
|
android:color="#44000000"
|
||||||
|
android:offset="0.0" />
|
||||||
|
<item
|
||||||
|
android:color="#00000000"
|
||||||
|
android:offset="1.0" />
|
||||||
|
</gradient>
|
||||||
|
</aapt:attr>
|
||||||
|
</path>
|
||||||
|
<path
|
||||||
|
android:fillColor="#FFFFFF"
|
||||||
|
android:fillType="nonZero"
|
||||||
|
android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z"
|
||||||
|
android:strokeWidth="1"
|
||||||
|
android:strokeColor="#00000000" />
|
||||||
|
</vector>
|
||||||
@@ -0,0 +1,170 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="108dp"
|
||||||
|
android:height="108dp"
|
||||||
|
android:viewportWidth="108"
|
||||||
|
android:viewportHeight="108">
|
||||||
|
<path
|
||||||
|
android:fillColor="#3DDC84"
|
||||||
|
android:pathData="M0,0h108v108h-108z" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M9,0L9,108"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M19,0L19,108"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M29,0L29,108"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M39,0L39,108"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M49,0L49,108"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M59,0L59,108"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M69,0L69,108"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M79,0L79,108"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M89,0L89,108"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M99,0L99,108"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M0,9L108,9"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M0,19L108,19"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M0,29L108,29"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M0,39L108,39"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M0,49L108,49"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M0,59L108,59"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M0,69L108,69"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M0,79L108,79"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M0,89L108,89"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M0,99L108,99"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M19,29L89,29"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M19,39L89,39"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M19,49L89,49"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M19,59L89,59"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M19,69L89,69"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M19,79L89,79"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M29,19L29,89"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M39,19L39,89"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M49,19L49,89"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M59,19L59,89"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M69,19L69,89"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
<path
|
||||||
|
android:fillColor="#00000000"
|
||||||
|
android:pathData="M79,19L79,89"
|
||||||
|
android:strokeWidth="0.8"
|
||||||
|
android:strokeColor="#33FFFFFF" />
|
||||||
|
</vector>
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<background android:drawable="@drawable/ic_launcher_background" />
|
||||||
|
<foreground android:drawable="@drawable/ic_launcher_foreground" />
|
||||||
|
</adaptive-icon>
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<background android:drawable="@drawable/ic_launcher_background" />
|
||||||
|
<foreground android:drawable="@drawable/ic_launcher_foreground" />
|
||||||
|
</adaptive-icon>
|
||||||
BIN
demo/android-demo/app/src/main/res/mipmap-hdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 3.5 KiB |
|
After Width: | Height: | Size: 5.2 KiB |
BIN
demo/android-demo/app/src/main/res/mipmap-mdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 2.6 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
BIN
demo/android-demo/app/src/main/res/mipmap-xhdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 4.8 KiB |
|
After Width: | Height: | Size: 7.3 KiB |
BIN
demo/android-demo/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
Normal file
|
After Width: | Height: | Size: 7.7 KiB |
|
After Width: | Height: | Size: 12 KiB |
|
After Width: | Height: | Size: 10 KiB |
|
After Width: | Height: | Size: 16 KiB |
16
demo/android-demo/app/src/main/res/values-night/themes.xml
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<resources xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
<!-- Base application theme. -->
|
||||||
|
<style name="Theme.TestableAndroidDemo" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
|
||||||
|
<!-- Primary brand color. -->
|
||||||
|
<item name="colorPrimary">@color/purple_200</item>
|
||||||
|
<item name="colorPrimaryVariant">@color/purple_700</item>
|
||||||
|
<item name="colorOnPrimary">@color/black</item>
|
||||||
|
<!-- Secondary brand color. -->
|
||||||
|
<item name="colorSecondary">@color/teal_200</item>
|
||||||
|
<item name="colorSecondaryVariant">@color/teal_200</item>
|
||||||
|
<item name="colorOnSecondary">@color/black</item>
|
||||||
|
<!-- Status bar color. -->
|
||||||
|
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
|
||||||
|
<!-- Customize your theme here. -->
|
||||||
|
</style>
|
||||||
|
</resources>
|
||||||
10
demo/android-demo/app/src/main/res/values/colors.xml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<resources>
|
||||||
|
<color name="purple_200">#FFBB86FC</color>
|
||||||
|
<color name="purple_500">#FF6200EE</color>
|
||||||
|
<color name="purple_700">#FF3700B3</color>
|
||||||
|
<color name="teal_200">#FF03DAC5</color>
|
||||||
|
<color name="teal_700">#FF018786</color>
|
||||||
|
<color name="black">#FF000000</color>
|
||||||
|
<color name="white">#FFFFFFFF</color>
|
||||||
|
</resources>
|
||||||
3
demo/android-demo/app/src/main/res/values/strings.xml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
<resources>
|
||||||
|
<string name="app_name">Testable Android Demo</string>
|
||||||
|
</resources>
|
||||||
16
demo/android-demo/app/src/main/res/values/themes.xml
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<resources xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
<!-- Base application theme. -->
|
||||||
|
<style name="Theme.TestableAndroidDemo" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
|
||||||
|
<!-- Primary brand color. -->
|
||||||
|
<item name="colorPrimary">@color/purple_500</item>
|
||||||
|
<item name="colorPrimaryVariant">@color/purple_700</item>
|
||||||
|
<item name="colorOnPrimary">@color/white</item>
|
||||||
|
<!-- Secondary brand color. -->
|
||||||
|
<item name="colorSecondary">@color/teal_200</item>
|
||||||
|
<item name="colorSecondaryVariant">@color/teal_700</item>
|
||||||
|
<item name="colorOnSecondary">@color/black</item>
|
||||||
|
<!-- Status bar color. -->
|
||||||
|
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
|
||||||
|
<!-- Customize your theme here. -->
|
||||||
|
</style>
|
||||||
|
</resources>
|
||||||
@@ -0,0 +1,122 @@
|
|||||||
|
package com.alibaba.testable.demo;
|
||||||
|
|
||||||
|
import com.alibaba.testable.demo.model.BlackBox;
|
||||||
|
import com.alibaba.testable.core.annotation.MockConstructor;
|
||||||
|
import com.alibaba.testable.core.annotation.MockMethod;
|
||||||
|
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
|
||||||
|
import static com.alibaba.testable.core.tool.TestableTool.MOCK_CONTEXT;
|
||||||
|
import static com.alibaba.testable.core.tool.TestableTool.SOURCE_METHOD;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 演示基本的Mock功能
|
||||||
|
* Demonstrate basic mock functionality
|
||||||
|
*/
|
||||||
|
public class DemoBasicTest {
|
||||||
|
|
||||||
|
private DemoBasic demoBasic = new DemoBasic();
|
||||||
|
|
||||||
|
public static class Mock {
|
||||||
|
@MockConstructor
|
||||||
|
private BlackBox createBlackBox(String text) {
|
||||||
|
return new BlackBox("mock_" + text);
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockMethod(targetClass = DemoBasic.class)
|
||||||
|
private String innerFunc(String text) {
|
||||||
|
return "mock_" + text;
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockMethod(targetClass = DemoBasic.class)
|
||||||
|
private String staticFunc() {
|
||||||
|
return "_MOCK_TAIL";
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockMethod(targetClass = String.class)
|
||||||
|
private String trim() {
|
||||||
|
return "trim_string";
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockMethod(targetClass = String.class, targetMethod = "substring")
|
||||||
|
private String sub(int i, int j) {
|
||||||
|
return "sub_string";
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockMethod(targetClass = String.class)
|
||||||
|
private boolean startsWith(String s) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockMethod(targetClass = BlackBox.class)
|
||||||
|
private BlackBox secretBox() {
|
||||||
|
return new BlackBox("not_secret_box");
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockMethod(targetClass = DemoBasic.class)
|
||||||
|
private String callFromDifferentMethod() {
|
||||||
|
if ("special_case".equals(MOCK_CONTEXT.get("case"))) {
|
||||||
|
return "mock_special";
|
||||||
|
}
|
||||||
|
switch (SOURCE_METHOD) {
|
||||||
|
case "callerOne":
|
||||||
|
return "mock_one";
|
||||||
|
default:
|
||||||
|
return "mock_others";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_mock_new_object() {
|
||||||
|
assertEquals("mock_something", demoBasic.newFunc());
|
||||||
|
verify("createBlackBox").with("something");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_mock_member_method() throws Exception {
|
||||||
|
assertEquals("{ \"res\": \"mock_hello_MOCK_TAIL\"}", demoBasic.outerFunc("hello"));
|
||||||
|
verify("innerFunc").with("hello");
|
||||||
|
verify("staticFunc").with();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_mock_common_method() {
|
||||||
|
assertEquals("trim_string__sub_string__false", demoBasic.commonFunc());
|
||||||
|
verify("trim").withTimes(1);
|
||||||
|
verify("sub").withTimes(1);
|
||||||
|
verify("startsWith").withTimes(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_mock_static_method() {
|
||||||
|
assertEquals("not_secret_box", demoBasic.getBox().get());
|
||||||
|
verify("secretBox").withTimes(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_get_source_method_name() throws Exception {
|
||||||
|
// synchronous
|
||||||
|
assertEquals("mock_one_mock_others", demoBasic.callerOne() + "_" + demoBasic.callerTwo());
|
||||||
|
// asynchronous
|
||||||
|
assertEquals("mock_one_mock_others",
|
||||||
|
Executors.newSingleThreadExecutor().submit(() -> demoBasic.callerOne() + "_" + demoBasic.callerTwo()).get());
|
||||||
|
verify("callFromDifferentMethod").withTimes(4);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_set_mock_context() throws Exception {
|
||||||
|
MOCK_CONTEXT.put("case", "special_case");
|
||||||
|
// synchronous
|
||||||
|
assertEquals("mock_special", demoBasic.callerOne());
|
||||||
|
// asynchronous
|
||||||
|
assertEquals("mock_special", Executors.newSingleThreadExecutor().submit(() -> demoBasic.callerOne()).get());
|
||||||
|
verify("callFromDifferentMethod").withTimes(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package com.alibaba.testable.demo;
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.alibaba.testable.core.annotation.MockMethod;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.robolectric.Robolectric;
|
||||||
|
import org.robolectric.RobolectricTestRunner;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
|
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
|
||||||
|
|
||||||
|
@RunWith(RobolectricTestRunner.class)
|
||||||
|
@Config(sdk = 30)
|
||||||
|
public class DemoServiceTest {
|
||||||
|
|
||||||
|
private DemoService demoService;
|
||||||
|
|
||||||
|
public static class Mock {
|
||||||
|
@MockMethod(targetClass = Log.class, targetMethod = "d")
|
||||||
|
public static int log(String tag, String msg) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() throws Exception {
|
||||||
|
demoService = Robolectric.setupService(DemoService.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOnStartCommand() {
|
||||||
|
Intent intent = new Intent();
|
||||||
|
|
||||||
|
intent.setAction("start_foreground");
|
||||||
|
demoService.onStartCommand(intent, 0, 1);
|
||||||
|
verify("log").with("DemoService", "start service.");
|
||||||
|
|
||||||
|
intent.setAction("stop_foreground");
|
||||||
|
demoService.onStartCommand(intent, 0, 1);
|
||||||
|
verify("log").with("DemoService", "stop service.");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
log.file = null
|
||||||
28
demo/android-demo/build.gradle
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||||
|
buildscript {
|
||||||
|
ext.kotlin_version = "1.4.32"
|
||||||
|
repositories {
|
||||||
|
mavenLocal()
|
||||||
|
google()
|
||||||
|
jcenter()
|
||||||
|
}
|
||||||
|
dependencies {
|
||||||
|
classpath "com.android.tools.build:gradle:7.0.0-alpha14"
|
||||||
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
|
|
||||||
|
// NOTE: Do not place your application dependencies here; they belong
|
||||||
|
// in the individual module build.gradle files
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
allprojects {
|
||||||
|
repositories {
|
||||||
|
mavenLocal()
|
||||||
|
google()
|
||||||
|
jcenter()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
task clean(type: Delete) {
|
||||||
|
delete rootProject.buildDir
|
||||||
|
}
|
||||||
21
demo/android-demo/gradle.properties
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
# Project-wide Gradle settings.
|
||||||
|
# IDE (e.g. Android Studio) users:
|
||||||
|
# Gradle settings configured through the IDE *will override*
|
||||||
|
# any settings specified in this file.
|
||||||
|
# For more details on how to configure your build environment visit
|
||||||
|
# http://www.gradle.org/docs/current/userguide/build_environment.html
|
||||||
|
# Specifies the JVM arguments used for the daemon process.
|
||||||
|
# The setting is particularly useful for tweaking memory settings.
|
||||||
|
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
|
||||||
|
# When configured, Gradle will run in incubating parallel mode.
|
||||||
|
# This option should only be used with decoupled projects. More details, visit
|
||||||
|
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
|
||||||
|
# org.gradle.parallel=true
|
||||||
|
# AndroidX package structure to make it clearer which packages are bundled with the
|
||||||
|
# Android operating system, and which are packaged with your app"s APK
|
||||||
|
# https://developer.android.com/topic/libraries/support-library/androidx-rn
|
||||||
|
android.useAndroidX=true
|
||||||
|
# Automatically convert third-party libraries to use AndroidX
|
||||||
|
android.enableJetifier=true
|
||||||
|
# Kotlin code style for this project: "official" or "obsolete":
|
||||||
|
kotlin.code.style=official
|
||||||
6
demo/android-demo/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#Fri Apr 30 14:35:40 CST 2021
|
||||||
|
distributionBase=GRADLE_USER_HOME
|
||||||
|
distributionPath=wrapper/dists
|
||||||
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
|
zipStorePath=wrapper/dists
|
||||||
|
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-bin.zip
|
||||||
172
demo/android-demo/gradlew
vendored
Executable file
@@ -0,0 +1,172 @@
|
|||||||
|
#!/usr/bin/env sh
|
||||||
|
|
||||||
|
##############################################################################
|
||||||
|
##
|
||||||
|
## Gradle start up script for UN*X
|
||||||
|
##
|
||||||
|
##############################################################################
|
||||||
|
|
||||||
|
# Attempt to set APP_HOME
|
||||||
|
# Resolve links: $0 may be a link
|
||||||
|
PRG="$0"
|
||||||
|
# Need this for relative symlinks.
|
||||||
|
while [ -h "$PRG" ] ; do
|
||||||
|
ls=`ls -ld "$PRG"`
|
||||||
|
link=`expr "$ls" : '.*-> \(.*\)$'`
|
||||||
|
if expr "$link" : '/.*' > /dev/null; then
|
||||||
|
PRG="$link"
|
||||||
|
else
|
||||||
|
PRG=`dirname "$PRG"`"/$link"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
SAVED="`pwd`"
|
||||||
|
cd "`dirname \"$PRG\"`/" >/dev/null
|
||||||
|
APP_HOME="`pwd -P`"
|
||||||
|
cd "$SAVED" >/dev/null
|
||||||
|
|
||||||
|
APP_NAME="Gradle"
|
||||||
|
APP_BASE_NAME=`basename "$0"`
|
||||||
|
|
||||||
|
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||||
|
DEFAULT_JVM_OPTS=""
|
||||||
|
|
||||||
|
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||||
|
MAX_FD="maximum"
|
||||||
|
|
||||||
|
warn () {
|
||||||
|
echo "$*"
|
||||||
|
}
|
||||||
|
|
||||||
|
die () {
|
||||||
|
echo
|
||||||
|
echo "$*"
|
||||||
|
echo
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# OS specific support (must be 'true' or 'false').
|
||||||
|
cygwin=false
|
||||||
|
msys=false
|
||||||
|
darwin=false
|
||||||
|
nonstop=false
|
||||||
|
case "`uname`" in
|
||||||
|
CYGWIN* )
|
||||||
|
cygwin=true
|
||||||
|
;;
|
||||||
|
Darwin* )
|
||||||
|
darwin=true
|
||||||
|
;;
|
||||||
|
MINGW* )
|
||||||
|
msys=true
|
||||||
|
;;
|
||||||
|
NONSTOP* )
|
||||||
|
nonstop=true
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||||
|
|
||||||
|
# Determine the Java command to use to start the JVM.
|
||||||
|
if [ -n "$JAVA_HOME" ] ; then
|
||||||
|
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||||
|
# IBM's JDK on AIX uses strange locations for the executables
|
||||||
|
JAVACMD="$JAVA_HOME/jre/sh/java"
|
||||||
|
else
|
||||||
|
JAVACMD="$JAVA_HOME/bin/java"
|
||||||
|
fi
|
||||||
|
if [ ! -x "$JAVACMD" ] ; then
|
||||||
|
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||||
|
|
||||||
|
Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
location of your Java installation."
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
JAVACMD="java"
|
||||||
|
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||||
|
|
||||||
|
Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
location of your Java installation."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Increase the maximum file descriptors if we can.
|
||||||
|
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
|
||||||
|
MAX_FD_LIMIT=`ulimit -H -n`
|
||||||
|
if [ $? -eq 0 ] ; then
|
||||||
|
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
|
||||||
|
MAX_FD="$MAX_FD_LIMIT"
|
||||||
|
fi
|
||||||
|
ulimit -n $MAX_FD
|
||||||
|
if [ $? -ne 0 ] ; then
|
||||||
|
warn "Could not set maximum file descriptor limit: $MAX_FD"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# For Darwin, add options to specify how the application appears in the dock
|
||||||
|
if $darwin; then
|
||||||
|
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
||||||
|
fi
|
||||||
|
|
||||||
|
# For Cygwin, switch paths to Windows format before running java
|
||||||
|
if $cygwin ; then
|
||||||
|
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
||||||
|
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
||||||
|
JAVACMD=`cygpath --unix "$JAVACMD"`
|
||||||
|
|
||||||
|
# We build the pattern for arguments to be converted via cygpath
|
||||||
|
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
|
||||||
|
SEP=""
|
||||||
|
for dir in $ROOTDIRSRAW ; do
|
||||||
|
ROOTDIRS="$ROOTDIRS$SEP$dir"
|
||||||
|
SEP="|"
|
||||||
|
done
|
||||||
|
OURCYGPATTERN="(^($ROOTDIRS))"
|
||||||
|
# Add a user-defined pattern to the cygpath arguments
|
||||||
|
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
|
||||||
|
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
|
||||||
|
fi
|
||||||
|
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||||
|
i=0
|
||||||
|
for arg in "$@" ; do
|
||||||
|
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
|
||||||
|
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
|
||||||
|
|
||||||
|
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
|
||||||
|
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
|
||||||
|
else
|
||||||
|
eval `echo args$i`="\"$arg\""
|
||||||
|
fi
|
||||||
|
i=$((i+1))
|
||||||
|
done
|
||||||
|
case $i in
|
||||||
|
(0) set -- ;;
|
||||||
|
(1) set -- "$args0" ;;
|
||||||
|
(2) set -- "$args0" "$args1" ;;
|
||||||
|
(3) set -- "$args0" "$args1" "$args2" ;;
|
||||||
|
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
||||||
|
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
||||||
|
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
||||||
|
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
||||||
|
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
||||||
|
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Escape application args
|
||||||
|
save () {
|
||||||
|
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
|
||||||
|
echo " "
|
||||||
|
}
|
||||||
|
APP_ARGS=$(save "$@")
|
||||||
|
|
||||||
|
# Collect all arguments for the java command, following the shell quoting and substitution rules
|
||||||
|
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
|
||||||
|
|
||||||
|
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
|
||||||
|
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
|
||||||
|
cd "$(dirname "$0")"
|
||||||
|
fi
|
||||||
|
|
||||||
|
exec "$JAVACMD" "$@"
|
||||||
84
demo/android-demo/gradlew.bat
vendored
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
@if "%DEBUG%" == "" @echo off
|
||||||
|
@rem ##########################################################################
|
||||||
|
@rem
|
||||||
|
@rem Gradle startup script for Windows
|
||||||
|
@rem
|
||||||
|
@rem ##########################################################################
|
||||||
|
|
||||||
|
@rem Set local scope for the variables with windows NT shell
|
||||||
|
if "%OS%"=="Windows_NT" setlocal
|
||||||
|
|
||||||
|
set DIRNAME=%~dp0
|
||||||
|
if "%DIRNAME%" == "" set DIRNAME=.
|
||||||
|
set APP_BASE_NAME=%~n0
|
||||||
|
set APP_HOME=%DIRNAME%
|
||||||
|
|
||||||
|
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||||
|
set DEFAULT_JVM_OPTS=
|
||||||
|
|
||||||
|
@rem Find java.exe
|
||||||
|
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||||
|
|
||||||
|
set JAVA_EXE=java.exe
|
||||||
|
%JAVA_EXE% -version >NUL 2>&1
|
||||||
|
if "%ERRORLEVEL%" == "0" goto init
|
||||||
|
|
||||||
|
echo.
|
||||||
|
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||||
|
echo.
|
||||||
|
echo Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
echo location of your Java installation.
|
||||||
|
|
||||||
|
goto fail
|
||||||
|
|
||||||
|
:findJavaFromJavaHome
|
||||||
|
set JAVA_HOME=%JAVA_HOME:"=%
|
||||||
|
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||||
|
|
||||||
|
if exist "%JAVA_EXE%" goto init
|
||||||
|
|
||||||
|
echo.
|
||||||
|
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||||
|
echo.
|
||||||
|
echo Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
echo location of your Java installation.
|
||||||
|
|
||||||
|
goto fail
|
||||||
|
|
||||||
|
:init
|
||||||
|
@rem Get command-line arguments, handling Windows variants
|
||||||
|
|
||||||
|
if not "%OS%" == "Windows_NT" goto win9xME_args
|
||||||
|
|
||||||
|
:win9xME_args
|
||||||
|
@rem Slurp the command line arguments.
|
||||||
|
set CMD_LINE_ARGS=
|
||||||
|
set _SKIP=2
|
||||||
|
|
||||||
|
:win9xME_args_slurp
|
||||||
|
if "x%~1" == "x" goto execute
|
||||||
|
|
||||||
|
set CMD_LINE_ARGS=%*
|
||||||
|
|
||||||
|
:execute
|
||||||
|
@rem Setup the command line
|
||||||
|
|
||||||
|
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||||
|
|
||||||
|
@rem Execute Gradle
|
||||||
|
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
|
||||||
|
|
||||||
|
:end
|
||||||
|
@rem End local scope for the variables with windows NT shell
|
||||||
|
if "%ERRORLEVEL%"=="0" goto mainEnd
|
||||||
|
|
||||||
|
:fail
|
||||||
|
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||||
|
rem the _cmd.exe /c_ return code!
|
||||||
|
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
||||||
|
exit /b 1
|
||||||
|
|
||||||
|
:mainEnd
|
||||||
|
if "%OS%"=="Windows_NT" endlocal
|
||||||
|
|
||||||
|
:omega
|
||||||
2
demo/android-demo/settings.gradle
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
include ':app'
|
||||||
|
rootProject.name = "Testable Android Demo"
|
||||||
@@ -13,8 +13,8 @@ repositories {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
|
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
|
||||||
testImplementation('com.alibaba.testable:testable-all:0.4.11')
|
testImplementation('com.alibaba.testable:testable-all:0.6.7')
|
||||||
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.4.11')
|
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.6.7')
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.withType(JavaCompile) {
|
tasks.withType(JavaCompile) {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
<junit.version>5.6.2</junit.version>
|
<junit.version>5.6.2</junit.version>
|
||||||
<testable.version>0.4.11</testable.version>
|
<testable.version>0.6.7</testable.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@@ -32,7 +32,6 @@
|
|||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package com.alibaba.demo.association;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 目标类,此类中的一些调用将会被Mock掉
|
||||||
|
* Target class, some invocations inside this class will be mocked
|
||||||
|
*/
|
||||||
|
public class CookerService {
|
||||||
|
|
||||||
|
private static String hireSandwichCooker() {
|
||||||
|
return "Real-Sandwich-Cooker";
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String hireHamburgerCooker() {
|
||||||
|
return "Real-Hamburger-Cooker";
|
||||||
|
}
|
||||||
|
|
||||||
|
private String cookSandwich() {
|
||||||
|
return "Real-Sandwich";
|
||||||
|
}
|
||||||
|
|
||||||
|
private String cookHamburger() {
|
||||||
|
return "Real-Hamburger";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String prepareSandwich() {
|
||||||
|
return hireSandwichCooker() + " & " + cookSandwich();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String prepareHamburger() {
|
||||||
|
return hireHamburgerCooker() + " & " + cookHamburger();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.alibaba.demo.association;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 被测类,会访问`CookerService`里的方法
|
||||||
|
* Class to be tested, which will access methods in TargetService class
|
||||||
|
*/
|
||||||
|
public class SellerService {
|
||||||
|
|
||||||
|
private CookerService cookerService = new CookerService();
|
||||||
|
|
||||||
|
public String sellSandwich() {
|
||||||
|
return cookerService.prepareSandwich();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String sellHamburger() {
|
||||||
|
return cookerService.prepareHamburger();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package com.alibaba.testable.demo;
|
package com.alibaba.demo.basic;
|
||||||
|
|
||||||
import com.alibaba.testable.demo.model.BlackBox;
|
import com.alibaba.demo.basic.model.mock.BlackBox;
|
||||||
import com.alibaba.testable.demo.model.Box;
|
import com.alibaba.demo.basic.model.mock.Box;
|
||||||
import com.alibaba.testable.demo.model.Color;
|
import com.alibaba.demo.basic.model.mock.Color;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 演示父类变量引用子类对象时的Mock场景
|
* 演示父类变量引用子类对象时的Mock场景
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package com.alibaba.demo.basic;
|
||||||
|
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 演示对内部类的Mock支持
|
||||||
|
* Demonstrate support for mocking invocation inside a inner class
|
||||||
|
*/
|
||||||
|
public class DemoInnerClass {
|
||||||
|
|
||||||
|
public static class StaticInner {
|
||||||
|
/**
|
||||||
|
* invocation inside a static inner class
|
||||||
|
*/
|
||||||
|
public String demo() {
|
||||||
|
return methodToBeMock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Inner {
|
||||||
|
/**
|
||||||
|
* invocation inside a non-static inner class
|
||||||
|
*/
|
||||||
|
public String demo() {
|
||||||
|
return methodToBeMock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ExecutorService executorService = Executors.newSingleThreadExecutor();
|
||||||
|
|
||||||
|
public String callAnonymousInner() throws ExecutionException, InterruptedException {
|
||||||
|
/**
|
||||||
|
* invocation inside a anonymous inner class
|
||||||
|
*/
|
||||||
|
Future<String> future = executorService.submit(new Callable<String>() {
|
||||||
|
@Override
|
||||||
|
public String call() throws Exception {
|
||||||
|
return methodToBeMock();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return future.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String callLambdaInner() throws ExecutionException, InterruptedException {
|
||||||
|
/**
|
||||||
|
* invocation inside a lambda inner class
|
||||||
|
*/
|
||||||
|
Future<String> future = executorService.submit(() -> methodToBeMock());
|
||||||
|
return future.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String callInnerDemo() {
|
||||||
|
return new Inner().demo();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String methodToBeMock() {
|
||||||
|
return "RealCall";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package com.alibaba.testable.demo;
|
package com.alibaba.demo.basic;
|
||||||
|
|
||||||
|
|
||||||
import com.alibaba.testable.demo.model.BlackBox;
|
import com.alibaba.demo.basic.model.mock.BlackBox;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package com.alibaba.testable.demo;
|
package com.alibaba.demo.basic;
|
||||||
|
|
||||||
import com.alibaba.testable.demo.model.BlackBox;
|
import com.alibaba.demo.basic.model.mock.BlackBox;
|
||||||
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.alibaba.testable.demo;
|
package com.alibaba.demo.basic;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.alibaba.testable.demo;
|
package com.alibaba.demo.basic;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.alibaba.demo.basic.model.mock;
|
||||||
|
|
||||||
|
public class BlackBox extends Box implements Color {
|
||||||
|
|
||||||
|
public BlackBox(String data) {
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BlackBox secretBox() {
|
||||||
|
return new BlackBox("secret");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void put(String something) {
|
||||||
|
data = something;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getColor() {
|
||||||
|
return "black";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.alibaba.demo.basic.model.mock;
|
||||||
|
|
||||||
|
abstract public class Box {
|
||||||
|
|
||||||
|
protected String data;
|
||||||
|
|
||||||
|
abstract public void put(String something);
|
||||||
|
|
||||||
|
public String get() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.alibaba.demo.basic.model.mock;
|
||||||
|
|
||||||
|
public interface Color {
|
||||||
|
|
||||||
|
String getColor();
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package com.alibaba.demo.basic.model.omni;
|
||||||
|
|
||||||
|
public class Child {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 我是一个私有的构造方法
|
||||||
|
* This class have only private constructor
|
||||||
|
*/
|
||||||
|
private Child() {
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- 内部成员字段 ----------
|
||||||
|
|
||||||
|
private GrandChild grandChild;
|
||||||
|
|
||||||
|
private InnerChild subChild;
|
||||||
|
|
||||||
|
/* ---------------------------------------
|
||||||
|
以下Getters/Setters方法仅为便于功能演示而添加
|
||||||
|
并非OmniConstructor或OmniAccessor功能所需
|
||||||
|
--------------------------------------- */
|
||||||
|
|
||||||
|
public GrandChild getGrandChild() {
|
||||||
|
return grandChild;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGrandChild(GrandChild grandChild) {
|
||||||
|
this.grandChild = grandChild;
|
||||||
|
}
|
||||||
|
|
||||||
|
public InnerChild getSubChild() {
|
||||||
|
return subChild;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSubChild(InnerChild subChild) {
|
||||||
|
this.subChild = subChild;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 这是一个私有内部类
|
||||||
|
* An private inner class
|
||||||
|
*/
|
||||||
|
private class InnerChild {
|
||||||
|
private String secret;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package com.alibaba.demo.basic.model.omni;
|
||||||
|
|
||||||
|
public class GrandChild {
|
||||||
|
|
||||||
|
// ---------- 内部成员字段 ----------
|
||||||
|
|
||||||
|
private int value;
|
||||||
|
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
/* ---------------------------------------
|
||||||
|
以下Getters/Setters方法仅为便于功能演示而添加
|
||||||
|
并非OmniConstructor或OmniAccessor功能所需
|
||||||
|
--------------------------------------- */
|
||||||
|
|
||||||
|
public int getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(int value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContent() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContent(String content) {
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package com.alibaba.demo.basic.model.omni;
|
||||||
|
|
||||||
|
public class Parent {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 我是一个虽然存在但无法正常使用的构造方法
|
||||||
|
* This class have constructor with exception throw
|
||||||
|
*/
|
||||||
|
public Parent() {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- 内部成员字段 ----------
|
||||||
|
|
||||||
|
private Child child;
|
||||||
|
|
||||||
|
private Child[] children;
|
||||||
|
|
||||||
|
/* ---------------------------------------
|
||||||
|
以下Getters/Setters方法仅为便于功能演示而添加
|
||||||
|
并非OmniConstructor或OmniAccessor功能所需
|
||||||
|
--------------------------------------- */
|
||||||
|
|
||||||
|
public Child getChild() {
|
||||||
|
return child;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setChild(Child child) {
|
||||||
|
this.child = child;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Child[] getChildren() {
|
||||||
|
return children;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setChildren(Child[] children) {
|
||||||
|
this.children = children;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.alibaba.demo.one2multi;
|
||||||
|
|
||||||
|
public class ASvc {
|
||||||
|
|
||||||
|
public String demo(String name) {
|
||||||
|
return String.format("a_%s", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.alibaba.demo.one2multi;
|
||||||
|
|
||||||
|
public class BSvc {
|
||||||
|
|
||||||
|
public String demo(String name) {
|
||||||
|
return String.format("b_%s", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.alibaba.demo.one2multi;
|
||||||
|
|
||||||
|
public class CSvc {
|
||||||
|
|
||||||
|
public String demo(String name) {
|
||||||
|
return String.format("c_%s", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.alibaba.demo.association;
|
||||||
|
|
||||||
|
import com.alibaba.testable.core.annotation.MockMethod;
|
||||||
|
import com.alibaba.testable.core.model.MockScope;
|
||||||
|
|
||||||
|
class CookerServiceMock {
|
||||||
|
|
||||||
|
@MockMethod(targetClass = CookerService.class)
|
||||||
|
public static String hireSandwichCooker() {
|
||||||
|
return "Fake-Sandwich-Cooker";
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockMethod(targetClass = CookerService.class, scope = MockScope.ASSOCIATED)
|
||||||
|
public static String hireHamburgerCooker() {
|
||||||
|
return "Fake-Hamburger-Cooker";
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockMethod(targetClass = CookerService.class)
|
||||||
|
private String cookSandwich() {
|
||||||
|
return "Faked-Sandwich";
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockMethod(targetClass = CookerService.class, scope = MockScope.ASSOCIATED)
|
||||||
|
private String cookHamburger() {
|
||||||
|
return "Faked-Hamburger";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.alibaba.demo.association;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class SellerServiceTest {
|
||||||
|
|
||||||
|
private SellerService sellerService = new SellerService();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_sell_sandwich() {
|
||||||
|
assertEquals("Fake-Sandwich-Cooker & Faked-Sandwich", sellerService.sellSandwich());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_sell_hamburger() {
|
||||||
|
assertEquals("Real-Hamburger-Cooker & Real-Hamburger", sellerService.sellHamburger());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
package com.alibaba.testable.demo;
|
package com.alibaba.demo.basic;
|
||||||
|
|
||||||
|
import com.alibaba.demo.basic.model.mock.BlackBox;
|
||||||
|
import com.alibaba.demo.basic.model.mock.Box;
|
||||||
|
import com.alibaba.demo.basic.model.mock.Color;
|
||||||
import com.alibaba.testable.core.annotation.MockMethod;
|
import com.alibaba.testable.core.annotation.MockMethod;
|
||||||
import com.alibaba.testable.demo.model.BlackBox;
|
|
||||||
import com.alibaba.testable.demo.model.Box;
|
|
||||||
import com.alibaba.testable.demo.model.Color;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
|
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
|
||||||
@@ -17,74 +17,75 @@ class DemoInheritTest {
|
|||||||
|
|
||||||
private DemoInherit demoInherit = new DemoInherit();
|
private DemoInherit demoInherit = new DemoInherit();
|
||||||
|
|
||||||
@MockMethod(targetMethod = "put")
|
public static class Mock {
|
||||||
private void put_into_box(Box self, String something) {
|
@MockMethod(targetMethod = "put")
|
||||||
self.put("put_" + something + "_into_box");
|
private void put_into_box(Box self, String something) {
|
||||||
}
|
self.put("put_" + something + "_into_box");
|
||||||
|
}
|
||||||
|
|
||||||
@MockMethod(targetMethod = "put")
|
@MockMethod(targetMethod = "put")
|
||||||
private void put_into_blackbox(BlackBox self, String something) {
|
private void put_into_blackbox(BlackBox self, String something) {
|
||||||
self.put("put_" + something + "_into_blackbox");
|
self.put("put_" + something + "_into_blackbox");
|
||||||
}
|
}
|
||||||
|
|
||||||
@MockMethod(targetMethod = "get")
|
@MockMethod(targetMethod = "get")
|
||||||
private String get_from_box(Box self) {
|
private String get_from_box(Box self) {
|
||||||
return "get_from_box";
|
return "get_from_box";
|
||||||
}
|
}
|
||||||
|
|
||||||
@MockMethod(targetMethod = "get")
|
@MockMethod(targetMethod = "get")
|
||||||
private String get_from_blackbox(BlackBox self) {
|
private String get_from_blackbox(BlackBox self) {
|
||||||
return "get_from_blackbox";
|
return "get_from_blackbox";
|
||||||
}
|
}
|
||||||
|
|
||||||
@MockMethod(targetMethod = "getColor")
|
@MockMethod(targetMethod = "getColor")
|
||||||
private String get_color_from_color(Color self) {
|
private String get_color_from_color(Color self) {
|
||||||
return "color_from_color";
|
return "color_from_color";
|
||||||
}
|
}
|
||||||
|
|
||||||
@MockMethod(targetMethod = "getColor")
|
@MockMethod(targetMethod = "getColor")
|
||||||
private String get_color_from_blackbox(BlackBox self) {
|
private String get_color_from_blackbox(BlackBox self) {
|
||||||
return "color_from_blackbox";
|
return "color_from_blackbox";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void should_able_to_mock_call_sub_object_method_by_parent_object() {
|
void should_mock_call_sub_object_method_by_parent_object() {
|
||||||
BlackBox box = (BlackBox)demoInherit.putIntoBox();
|
BlackBox box = (BlackBox)demoInherit.putIntoBox();
|
||||||
verify("put_into_box").withTimes(1);
|
verify("put_into_box").withTimes(1);
|
||||||
assertEquals("put_data_into_box", box.get());
|
assertEquals("put_data_into_box", box.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void should_able_to_mock_call_sub_object_method_by_sub_object() {
|
void should_mock_call_sub_object_method_by_sub_object() {
|
||||||
BlackBox box = demoInherit.putIntoBlackBox();
|
BlackBox box = demoInherit.putIntoBlackBox();
|
||||||
verify("put_into_blackbox").withTimes(1);
|
verify("put_into_blackbox").withTimes(1);
|
||||||
assertEquals("put_data_into_blackbox", box.get());
|
assertEquals("put_data_into_blackbox", box.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void should_able_to_mock_call_parent_object_method_by_parent_object() {
|
void should_mock_call_parent_object_method_by_parent_object() {
|
||||||
String content = demoInherit.getFromBox();
|
String content = demoInherit.getFromBox();
|
||||||
verify("get_from_box").withTimes(1);
|
verify("get_from_box").withTimes(1);
|
||||||
assertEquals("get_from_box", content);
|
assertEquals("get_from_box", content);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void should_able_to_mock_call_parent_object_method_by_sub_object() {
|
void should_mock_call_parent_object_method_by_sub_object() {
|
||||||
String content = demoInherit.getFromBlackBox();
|
String content = demoInherit.getFromBlackBox();
|
||||||
verify("get_from_blackbox").withTimes(1);
|
verify("get_from_blackbox").withTimes(1);
|
||||||
assertEquals("get_from_blackbox", content);
|
assertEquals("get_from_blackbox", content);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void should_able_to_mock_call_interface_method_by_interface_object() {
|
void should_mock_call_interface_method_by_interface_object() {
|
||||||
String color = demoInherit.getColorViaColor();
|
String color = demoInherit.getColorViaColor();
|
||||||
verify("get_color_from_color").withTimes(1);
|
verify("get_color_from_color").withTimes(1);
|
||||||
assertEquals("color_from_color", color);
|
assertEquals("color_from_color", color);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void should_able_to_mock_call_interface_method_by_sub_class_object() {
|
void should_mock_call_interface_method_by_sub_class_object() {
|
||||||
String color = demoInherit.getColorViaBox();
|
String color = demoInherit.getColorViaBox();
|
||||||
verify("get_color_from_blackbox").withTimes(1);
|
verify("get_color_from_blackbox").withTimes(1);
|
||||||
assertEquals("color_from_blackbox", color);
|
assertEquals("color_from_blackbox", color);
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.alibaba.demo.basic;
|
||||||
|
|
||||||
|
import com.alibaba.testable.core.annotation.MockMethod;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 演示对内部类的Mock支持
|
||||||
|
* Demonstrate support for mocking invocation inside a inner class
|
||||||
|
*/
|
||||||
|
class DemoInnerClassTest {
|
||||||
|
|
||||||
|
public static class Mock {
|
||||||
|
@MockMethod(targetClass = DemoInnerClass.class)
|
||||||
|
String methodToBeMock() {
|
||||||
|
return "MockedCall";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_mock_invoke_inside_inner_class() throws Exception {
|
||||||
|
DemoInnerClass demo = new DemoInnerClass();
|
||||||
|
assertEquals("MockedCall", demo.callInnerDemo());
|
||||||
|
assertEquals("MockedCall", demo.callAnonymousInner());
|
||||||
|
assertEquals("MockedCall", demo.callLambdaInner());
|
||||||
|
assertEquals("MockedCall", new DemoInnerClass.StaticInner().demo());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package com.alibaba.testable.demo;
|
package com.alibaba.demo.basic;
|
||||||
|
|
||||||
|
import com.alibaba.demo.basic.model.mock.BlackBox;
|
||||||
import com.alibaba.testable.core.annotation.MockMethod;
|
import com.alibaba.testable.core.annotation.MockMethod;
|
||||||
import com.alibaba.testable.core.error.VerifyFailedError;
|
import com.alibaba.testable.core.error.VerifyFailedError;
|
||||||
import com.alibaba.testable.demo.model.BlackBox;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static com.alibaba.testable.core.matcher.InvokeMatcher.*;
|
import static com.alibaba.testable.core.matcher.InvokeMatcher.*;
|
||||||
@@ -17,15 +17,16 @@ class DemoMatcherTest {
|
|||||||
|
|
||||||
private DemoMatcher demoMatcher = new DemoMatcher();
|
private DemoMatcher demoMatcher = new DemoMatcher();
|
||||||
|
|
||||||
@MockMethod(targetMethod = "methodToBeMocked")
|
public static class Mock {
|
||||||
private void methodWithoutArgument(DemoMatcher self) {}
|
@MockMethod(targetMethod = "methodToBeMocked")
|
||||||
|
private void methodWithoutArgument(DemoMatcher self) {}
|
||||||
|
|
||||||
@MockMethod(targetMethod = "methodToBeMocked")
|
@MockMethod(targetMethod = "methodToBeMocked")
|
||||||
private void methodWithArguments(DemoMatcher self, Object a1, Object a2) {}
|
private void methodWithArguments(DemoMatcher self, Object a1, Object a2) {}
|
||||||
|
|
||||||
@MockMethod(targetMethod = "methodToBeMocked")
|
|
||||||
private void methodWithArrayArgument(DemoMatcher self, Object[] a) {}
|
|
||||||
|
|
||||||
|
@MockMethod(targetMethod = "methodToBeMocked")
|
||||||
|
private void methodWithArrayArgument(DemoMatcher self, Object[] a) {}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void should_match_no_argument() {
|
void should_match_no_argument() {
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package com.alibaba.testable.demo;
|
package com.alibaba.demo.basic;
|
||||||
|
|
||||||
|
import com.alibaba.demo.basic.model.mock.BlackBox;
|
||||||
import com.alibaba.testable.core.annotation.MockConstructor;
|
import com.alibaba.testable.core.annotation.MockConstructor;
|
||||||
import com.alibaba.testable.core.annotation.MockMethod;
|
import com.alibaba.testable.core.annotation.MockMethod;
|
||||||
import com.alibaba.testable.demo.model.BlackBox;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
@@ -20,68 +20,71 @@ class DemoMockTest {
|
|||||||
|
|
||||||
private DemoMock demoMock = new DemoMock();
|
private DemoMock demoMock = new DemoMock();
|
||||||
|
|
||||||
@MockConstructor
|
public static class Mock {
|
||||||
private BlackBox createBlackBox(String text) {
|
@MockConstructor
|
||||||
return new BlackBox("mock_" + text);
|
private BlackBox createBlackBox(String text) {
|
||||||
}
|
return new BlackBox("mock_" + text);
|
||||||
|
|
||||||
@MockMethod(targetClass = DemoMock.class)
|
|
||||||
private String innerFunc(String text) {
|
|
||||||
return "mock_" + text;
|
|
||||||
}
|
|
||||||
|
|
||||||
@MockMethod(targetClass = DemoMock.class)
|
|
||||||
private String staticFunc() {
|
|
||||||
return "_MOCK_TAIL";
|
|
||||||
}
|
|
||||||
|
|
||||||
@MockMethod(targetClass = String.class)
|
|
||||||
private String trim() {
|
|
||||||
return "trim_string";
|
|
||||||
}
|
|
||||||
|
|
||||||
@MockMethod(targetClass = String.class, targetMethod = "substring")
|
|
||||||
private String sub(int i, int j) {
|
|
||||||
return "sub_string";
|
|
||||||
}
|
|
||||||
|
|
||||||
@MockMethod(targetClass = String.class)
|
|
||||||
private boolean startsWith(String s) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@MockMethod(targetClass = BlackBox.class)
|
|
||||||
private BlackBox secretBox() {
|
|
||||||
return new BlackBox("not_secret_box");
|
|
||||||
}
|
|
||||||
|
|
||||||
@MockMethod(targetClass = DemoMock.class)
|
|
||||||
private String callFromDifferentMethod() {
|
|
||||||
if ("special_case".equals(MOCK_CONTEXT.get("case"))) {
|
|
||||||
return "mock_special";
|
|
||||||
}
|
}
|
||||||
switch (SOURCE_METHOD) {
|
|
||||||
case "callerOne": return "mock_one";
|
@MockMethod(targetClass = DemoMock.class)
|
||||||
default: return "mock_others";
|
private String innerFunc(String text) {
|
||||||
|
return "mock_" + text;
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockMethod(targetClass = DemoMock.class)
|
||||||
|
private String staticFunc() {
|
||||||
|
return "_MOCK_TAIL";
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockMethod(targetClass = String.class)
|
||||||
|
private String trim() {
|
||||||
|
return "trim_string";
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockMethod(targetClass = String.class, targetMethod = "substring")
|
||||||
|
private String sub(int i, int j) {
|
||||||
|
return "sub_string";
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockMethod(targetClass = String.class)
|
||||||
|
private boolean startsWith(String s) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockMethod(targetClass = BlackBox.class)
|
||||||
|
private BlackBox secretBox() {
|
||||||
|
return new BlackBox("not_secret_box");
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockMethod(targetClass = DemoMock.class)
|
||||||
|
private String callFromDifferentMethod() {
|
||||||
|
if ("special_case".equals(MOCK_CONTEXT.get("case"))) {
|
||||||
|
return "mock_special";
|
||||||
|
}
|
||||||
|
switch (SOURCE_METHOD) {
|
||||||
|
case "callerOne":
|
||||||
|
return "mock_one";
|
||||||
|
default:
|
||||||
|
return "mock_others";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void should_able_to_mock_new_object() {
|
void should_mock_new_object() {
|
||||||
assertEquals("mock_something", demoMock.newFunc());
|
assertEquals("mock_something", demoMock.newFunc());
|
||||||
verify("createBlackBox").with("something");
|
verify("createBlackBox").with("something");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void should_able_to_mock_member_method() throws Exception {
|
void should_mock_member_method() throws Exception {
|
||||||
assertEquals("{ \"res\": \"mock_hello_MOCK_TAIL\"}", demoMock.outerFunc("hello"));
|
assertEquals("{ \"res\": \"mock_hello_MOCK_TAIL\"}", demoMock.outerFunc("hello"));
|
||||||
verify("innerFunc").with("hello");
|
verify("innerFunc").with("hello");
|
||||||
verify("staticFunc").with();
|
verify("staticFunc").with();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void should_able_to_mock_common_method() {
|
void should_mock_common_method() {
|
||||||
assertEquals("trim_string__sub_string__false", demoMock.commonFunc());
|
assertEquals("trim_string__sub_string__false", demoMock.commonFunc());
|
||||||
verify("trim").withTimes(1);
|
verify("trim").withTimes(1);
|
||||||
verify("sub").withTimes(1);
|
verify("sub").withTimes(1);
|
||||||
@@ -89,13 +92,13 @@ class DemoMockTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void should_able_to_mock_static_method() {
|
void should_mock_static_method() {
|
||||||
assertEquals("not_secret_box", demoMock.getBox().get());
|
assertEquals("not_secret_box", demoMock.getBox().get());
|
||||||
verify("secretBox").withTimes(1);
|
verify("secretBox").withTimes(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void should_able_to_get_source_method_name() throws Exception {
|
void should_get_source_method_name() throws Exception {
|
||||||
// synchronous
|
// synchronous
|
||||||
assertEquals("mock_one_mock_others", demoMock.callerOne() + "_" + demoMock.callerTwo());
|
assertEquals("mock_one_mock_others", demoMock.callerOne() + "_" + demoMock.callerTwo());
|
||||||
// asynchronous
|
// asynchronous
|
||||||
@@ -105,14 +108,13 @@ class DemoMockTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void should_able_to_get_test_case_name() throws Exception {
|
void should_set_mock_context() throws Exception {
|
||||||
MOCK_CONTEXT.put("case", "special_case");
|
MOCK_CONTEXT.put("case", "special_case");
|
||||||
// synchronous
|
// synchronous
|
||||||
assertEquals("mock_special", demoMock.callerOne());
|
assertEquals("mock_special", demoMock.callerOne());
|
||||||
// asynchronous
|
// asynchronous
|
||||||
assertEquals("mock_special", Executors.newSingleThreadExecutor().submit(() -> demoMock.callerOne()).get());
|
assertEquals("mock_special", Executors.newSingleThreadExecutor().submit(() -> demoMock.callerOne()).get());
|
||||||
verify("callFromDifferentMethod").withTimes(2);
|
verify("callFromDifferentMethod").withTimes(2);
|
||||||
MOCK_CONTEXT.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
package com.alibaba.demo.basic;
|
||||||
|
|
||||||
|
import com.alibaba.demo.basic.model.omni.Child;
|
||||||
|
import com.alibaba.demo.basic.model.omni.Parent;
|
||||||
|
import com.alibaba.testable.core.tool.OmniAccessor;
|
||||||
|
import com.alibaba.testable.core.tool.OmniConstructor;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 演示快速创建任意对象和使用路径访问成员
|
||||||
|
* Demonstrate quick object construction and access members by path
|
||||||
|
*/
|
||||||
|
class DemoOmniMethodsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_construct_any_class() {
|
||||||
|
Parent parent = OmniConstructor.newInstance(Parent.class);
|
||||||
|
|
||||||
|
// 任意深度的子孙成员对象都不为空
|
||||||
|
assertNotNull(parent.getChild().getGrandChild().getContent());
|
||||||
|
|
||||||
|
// 所有基础类型初始化为默认数值
|
||||||
|
assertEquals(0, parent.getChild().getGrandChild().getValue());
|
||||||
|
assertEquals("", parent.getChild().getGrandChild().getContent());
|
||||||
|
|
||||||
|
// 所有数组类型初始化为空数组
|
||||||
|
assertEquals(0, parent.getChildren().length);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_get_any_member() {
|
||||||
|
Parent parent = OmniConstructor.newInstance(Parent.class);
|
||||||
|
parent.setChildren(OmniConstructor.newArray(Child.class, 3));
|
||||||
|
parent.getChild().getGrandChild().setContent("from child");
|
||||||
|
parent.getChildren()[0].getGrandChild().setContent("from 1st children");
|
||||||
|
parent.getChildren()[1].getGrandChild().setContent("from 2nd children");
|
||||||
|
parent.getChildren()[2].getGrandChild().setContent("from 3rd children");
|
||||||
|
|
||||||
|
// 使用成员名称快速读取成员对象
|
||||||
|
List<String> contents = OmniAccessor.get(parent, "content");
|
||||||
|
assertEquals(4, contents.size());
|
||||||
|
assertEquals("from child", contents.get(0));
|
||||||
|
assertEquals("from 1st children", contents.get(1));
|
||||||
|
assertEquals("from 2nd children", contents.get(2));
|
||||||
|
assertEquals("from 3rd children", contents.get(3));
|
||||||
|
|
||||||
|
// 使用成员类型快速读取成员对象
|
||||||
|
contents = OmniAccessor.get(parent, "{Child}/{GrandChild}/content");
|
||||||
|
assertEquals(1, contents.size());
|
||||||
|
assertEquals("from child", contents.get(0));
|
||||||
|
|
||||||
|
// 使用带下标的路径读取成员对象
|
||||||
|
assertEquals("from 2nd children", OmniAccessor.getFirst(parent, "children[1]/{GrandChild}/content"));
|
||||||
|
assertEquals("from 3rd children", OmniAccessor.getFirst(parent, "{Child[]}[2]/{GrandChild}/content"));
|
||||||
|
|
||||||
|
// 使用模糊路径快速读取成员对象
|
||||||
|
assertEquals("from 1st children", OmniAccessor.getFirst(parent, "{C*[]}[0]/*/con*t"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_set_any_member() {
|
||||||
|
Parent parent = OmniConstructor.newInstance(Parent.class);
|
||||||
|
parent.setChildren(OmniConstructor.newArray(Child.class, 3));
|
||||||
|
|
||||||
|
// 使用指定路径快速给成员对象赋值
|
||||||
|
OmniAccessor.set(parent, "child/grandChild/content", "demo child");
|
||||||
|
assertEquals("demo child", parent.getChild().getGrandChild().getContent());
|
||||||
|
|
||||||
|
// 使用带下标的路径给成员对象赋值
|
||||||
|
OmniAccessor.set(parent, "children[1]/grandChild/content", "demo children[1]");
|
||||||
|
assertEquals("demo children[1]", parent.getChildren()[1].getGrandChild().getContent());
|
||||||
|
|
||||||
|
// 使用模糊路径批量给成员对象赋值
|
||||||
|
OmniAccessor.set(parent, "child*/*/content", "demo in batch");
|
||||||
|
assertEquals("demo in batch", parent.getChild().getGrandChild().getContent());
|
||||||
|
assertEquals("demo in batch", parent.getChildren()[0].getGrandChild().getContent());
|
||||||
|
assertEquals("demo in batch", parent.getChildren()[1].getGrandChild().getContent());
|
||||||
|
assertEquals("demo in batch", parent.getChildren()[2].getGrandChild().getContent());
|
||||||
|
|
||||||
|
// 读写私有内部类类型的成员(使用类型名引用内部类时,无需带外部类名)
|
||||||
|
assertEquals("", OmniAccessor.getFirst(parent, "subChild/secret"));
|
||||||
|
OmniAccessor.set(parent, "{InnerChild}/secret", "inner-class secret");
|
||||||
|
assertEquals("inner-class secret", OmniAccessor.getFirst(parent, "subChild/secret"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package com.alibaba.demo.basic;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static com.alibaba.testable.core.tool.PrivateAccessor.*;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 演示使用`PrivateAccessor`工具类访问私有成员
|
||||||
|
* Demonstrate access private member via `PrivateAccessor` class
|
||||||
|
*/
|
||||||
|
class DemoPrivateAccessorTest {
|
||||||
|
|
||||||
|
private DemoPrivateAccess demoPrivateAccess = new DemoPrivateAccess();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_access_private_method() {
|
||||||
|
List<String> list = new ArrayList<String>() {{ add("a"); add("b"); add("c"); }};
|
||||||
|
assertEquals("member", invoke(demoPrivateAccess, "privateFunc"));
|
||||||
|
assertEquals("abc + hello + 1", invoke(demoPrivateAccess, "privateFuncWithArgs", list, "hello", 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_access_private_field() {
|
||||||
|
set(demoPrivateAccess, "count", 3);
|
||||||
|
assertEquals(Integer.valueOf(3), get(demoPrivateAccess, "count"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_access_private_static_method() {
|
||||||
|
assertEquals("static", invokeStatic(DemoPrivateAccess.class, "privateStaticFunc"));
|
||||||
|
assertEquals("hello + 1", invokeStatic(DemoPrivateAccess.class, "privateStaticFuncWithArgs", "hello", 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_access_private_static_field() {
|
||||||
|
setStatic(DemoPrivateAccess.class, "staticCount", 3);
|
||||||
|
assertEquals(Integer.valueOf(3), getStatic(DemoPrivateAccess.class, "staticCount"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_update_final_field() {
|
||||||
|
set(demoPrivateAccess, "pi", 3.14);
|
||||||
|
assertEquals(Double.valueOf(3.14), get(demoPrivateAccess, "pi"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_use_null_parameter() {
|
||||||
|
set(demoPrivateAccess, "pi", null);
|
||||||
|
assertNull(get(demoPrivateAccess, "pi"));
|
||||||
|
assertEquals("null + 1", invokeStatic(DemoPrivateAccess.class, "privateStaticFuncWithArgs", null, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package com.alibaba.demo.basic;
|
||||||
|
|
||||||
|
import com.alibaba.testable.processor.annotation.EnablePrivateAccess;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 演示使用`@EnablePrivateAccess`注解访问私有成员
|
||||||
|
* Demonstrate access private member via `@EnablePrivateAccess` annotation
|
||||||
|
*/
|
||||||
|
@EnablePrivateAccess(srcClass = DemoPrivateAccess.class)
|
||||||
|
class DemoPrivateProcessorTest {
|
||||||
|
|
||||||
|
private DemoPrivateAccess demoPrivateAccess = new DemoPrivateAccess();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_access_private_method() {
|
||||||
|
List<String> list = new ArrayList<String>() {{ add("a"); add("b"); add("c"); }};
|
||||||
|
assertEquals("member", demoPrivateAccess.privateFunc());
|
||||||
|
assertEquals("abc + hello + 1", demoPrivateAccess.privateFuncWithArgs(list, "hello", 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_access_private_field() {
|
||||||
|
demoPrivateAccess.count = 2;
|
||||||
|
assertEquals(Integer.valueOf(2), demoPrivateAccess.count);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_access_private_static_method() {
|
||||||
|
assertEquals("static", DemoPrivateAccess.privateStaticFunc());
|
||||||
|
assertEquals("hello + 1", DemoPrivateAccess.privateStaticFuncWithArgs("hello", 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_access_private_static_field() {
|
||||||
|
DemoPrivateAccess.staticCount = 2;
|
||||||
|
assertEquals(Integer.valueOf(2), DemoPrivateAccess.staticCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_update_final_field() {
|
||||||
|
demoPrivateAccess.pi = 4.13;
|
||||||
|
assertEquals(Double.valueOf(4.13), demoPrivateAccess.pi);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_use_null_parameter() {
|
||||||
|
demoPrivateAccess.pi = null;
|
||||||
|
assertNull(demoPrivateAccess.pi);
|
||||||
|
assertEquals("null + 1", DemoPrivateAccess.privateStaticFuncWithArgs(null, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
package com.alibaba.demo.basic;
|
||||||
|
|
||||||
|
import com.alibaba.testable.core.annotation.MockConstructor;
|
||||||
|
import com.alibaba.testable.core.annotation.MockMethod;
|
||||||
|
import com.alibaba.demo.basic.DemoTemplate;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 演示模板方法的Mock场景
|
||||||
|
* Demonstrate scenario of mocking template method
|
||||||
|
*/
|
||||||
|
class DemoTemplateTest {
|
||||||
|
|
||||||
|
private DemoTemplate demoTemplate = new DemoTemplate();
|
||||||
|
|
||||||
|
public static class Mock {
|
||||||
|
/* 第一种写法:使用泛型定义 */
|
||||||
|
/* First solution: use generics type */
|
||||||
|
|
||||||
|
@MockMethod
|
||||||
|
private <T> List<T> getList(DemoTemplate self, T value) {
|
||||||
|
return new ArrayList<T>() {{ add((T)(value.toString() + "_mock_list")); }};
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockMethod
|
||||||
|
private <K, V> Map<K, V> getMap(DemoTemplate self, K key, V value) {
|
||||||
|
return new HashMap<K, V>() {{ put(key, (V)(value.toString() + "_mock_map")); }};
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockConstructor
|
||||||
|
private <T> HashSet<T> newHashSet() {
|
||||||
|
HashSet<T> set = new HashSet<>();
|
||||||
|
set.add((T)"insert_mock");
|
||||||
|
return set;
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockMethod
|
||||||
|
private <E> boolean add(Set s, E e) {
|
||||||
|
s.add(e.toString() + "_mocked");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 第二种写法:使用Object类型 */
|
||||||
|
/* Second solution: use object type */
|
||||||
|
|
||||||
|
//@MockMethod
|
||||||
|
//private List<Object> getList(DemoTemplate self, Object value) {
|
||||||
|
// return new ArrayList<Object>() {{ add(value.toString() + "_mock_list"); }};
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//@MockMethod
|
||||||
|
//private Map<Object, Object> getMap(DemoTemplate self, Object key, Object value) {
|
||||||
|
// return new HashMap<Object, Object>() {{ put(key, value.toString() + "_mock_map"); }};
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//@MockConstructor
|
||||||
|
//private HashSet newHashSet() {
|
||||||
|
// HashSet<Object> set = new HashSet<>();
|
||||||
|
// set.add("insert_mock");
|
||||||
|
// return set;
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//@MockMethod
|
||||||
|
//private boolean add(Set s, Object e) {
|
||||||
|
// s.add(e.toString() + "_mocked");
|
||||||
|
// return true;
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_mock_single_template_method() {
|
||||||
|
String res = demoTemplate.singleTemplateMethod();
|
||||||
|
assertEquals("demo_mock_list", res);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_mock_double_template_method() {
|
||||||
|
String res = demoTemplate.doubleTemplateMethod();
|
||||||
|
assertEquals("testable_mock_map", res);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_mock_new_template_method() {
|
||||||
|
Set<?> res = demoTemplate.newTemplateMethod();
|
||||||
|
assertEquals(2, res.size());
|
||||||
|
Iterator<?> iterator = res.stream().iterator();
|
||||||
|
assertEquals("insert_mock", iterator.next());
|
||||||
|
assertEquals("world_mocked", iterator.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.alibaba.demo.one2multi;
|
||||||
|
|
||||||
|
import com.alibaba.testable.core.annotation.MockMethod;
|
||||||
|
|
||||||
|
public class ASvcMock {
|
||||||
|
|
||||||
|
@MockMethod(targetClass = String.class, targetMethod = "format")
|
||||||
|
public String a_format(String format, Object... args) {
|
||||||
|
return "a_mock";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.alibaba.demo.one2multi;
|
||||||
|
|
||||||
|
import com.alibaba.testable.core.annotation.MockMethod;
|
||||||
|
|
||||||
|
public class BSvcMock {
|
||||||
|
|
||||||
|
@MockMethod(targetClass = String.class, targetMethod = "format")
|
||||||
|
public String b_format(String format, Object... args) {
|
||||||
|
return "b_mock";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.alibaba.demo.one2multi;
|
||||||
|
|
||||||
|
import com.alibaba.testable.core.annotation.MockMethod;
|
||||||
|
|
||||||
|
public class CSvcMock {
|
||||||
|
|
||||||
|
@MockMethod(targetClass = String.class, targetMethod = "format")
|
||||||
|
public String c_format(String format, Object... args) {
|
||||||
|
return "c_mock";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.alibaba.demo.one2multi;
|
||||||
|
|
||||||
|
import com.alibaba.testable.core.annotation.MockWith;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
||||||
|
import static com.alibaba.testable.core.matcher.InvokeVerifier.verify;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
@MockWith
|
||||||
|
public class OneToMultiSvcTest {
|
||||||
|
|
||||||
|
private ASvc aSvc = new ASvc();
|
||||||
|
private BSvc bSvc = new BSvc();
|
||||||
|
private CSvc cSvc = new CSvc();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_test_multi_class_together() {
|
||||||
|
assertEquals("a_mock", aSvc.demo("test"));
|
||||||
|
assertEquals("b_mock", bSvc.demo("test"));
|
||||||
|
assertEquals("c_mock", cSvc.demo("test"));
|
||||||
|
verify("a_format").withTimes(1);
|
||||||
|
verify("b_format").withTimes(1);
|
||||||
|
verify("c_format").withTimes(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,77 +0,0 @@
|
|||||||
package com.alibaba.testable.demo;
|
|
||||||
|
|
||||||
import com.alibaba.testable.core.accessor.PrivateAccessor;
|
|
||||||
import com.alibaba.testable.processor.annotation.EnablePrivateAccess;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 演示私有成员访问功能
|
|
||||||
* Demonstrate private member access functionality
|
|
||||||
*/
|
|
||||||
@EnablePrivateAccess
|
|
||||||
class DemoPrivateAccessTest {
|
|
||||||
|
|
||||||
private DemoPrivateAccess demoPrivateAccess = new DemoPrivateAccess();
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void should_able_to_access_private_method() {
|
|
||||||
List<String> list = new ArrayList<String>() {{ add("a"); add("b"); add("c"); }};
|
|
||||||
assertEquals("member", demoPrivateAccess.privateFunc());
|
|
||||||
assertEquals("member", PrivateAccessor.invoke(demoPrivateAccess, "privateFunc"));
|
|
||||||
assertEquals("abc + hello + 1", demoPrivateAccess.privateFuncWithArgs(list, "hello", 1));
|
|
||||||
assertEquals("abc + hello + 1", PrivateAccessor.invoke(demoPrivateAccess, "privateFuncWithArgs", list, "hello", 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void should_able_to_access_private_field() {
|
|
||||||
demoPrivateAccess.count = 2;
|
|
||||||
assertEquals(Integer.valueOf(2), demoPrivateAccess.count);
|
|
||||||
|
|
||||||
PrivateAccessor.set(demoPrivateAccess, "count", 3);
|
|
||||||
assertEquals(Integer.valueOf(3), PrivateAccessor.get(demoPrivateAccess, "count"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void should_able_to_access_private_static_method() {
|
|
||||||
assertEquals("static", DemoPrivateAccess.privateStaticFunc());
|
|
||||||
assertEquals("static", PrivateAccessor.invokeStatic(DemoPrivateAccess.class, "privateStaticFunc"));
|
|
||||||
assertEquals("hello + 1", DemoPrivateAccess.privateStaticFuncWithArgs("hello", 1));
|
|
||||||
assertEquals("hello + 1", PrivateAccessor.invokeStatic(DemoPrivateAccess.class, "privateStaticFuncWithArgs", "hello", 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void should_able_to_access_private_static_field() {
|
|
||||||
DemoPrivateAccess.staticCount = 2;
|
|
||||||
assertEquals(Integer.valueOf(2), DemoPrivateAccess.staticCount);
|
|
||||||
|
|
||||||
PrivateAccessor.setStatic(DemoPrivateAccess.class, "staticCount", 3);
|
|
||||||
assertEquals(Integer.valueOf(3), PrivateAccessor.getStatic(DemoPrivateAccess.class, "staticCount"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void should_able_to_update_final_field() {
|
|
||||||
demoPrivateAccess.pi = 4.13;
|
|
||||||
assertEquals(Double.valueOf(4.13), demoPrivateAccess.pi);
|
|
||||||
|
|
||||||
PrivateAccessor.set(demoPrivateAccess, "pi", 3.14);
|
|
||||||
assertEquals(Double.valueOf(3.14), PrivateAccessor.get(demoPrivateAccess, "pi"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void should_able_to_use_null_parameter() {
|
|
||||||
demoPrivateAccess.pi = null;
|
|
||||||
assertNull(demoPrivateAccess.pi);
|
|
||||||
assertEquals("null + 1", DemoPrivateAccess.privateStaticFuncWithArgs(null, 1));
|
|
||||||
|
|
||||||
PrivateAccessor.set(demoPrivateAccess, "pi", null);
|
|
||||||
assertNull(PrivateAccessor.get(demoPrivateAccess, "pi"));
|
|
||||||
assertEquals("null + 1", PrivateAccessor.invokeStatic(DemoPrivateAccess.class, "privateStaticFuncWithArgs", null, 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,93 +0,0 @@
|
|||||||
package com.alibaba.testable.demo;
|
|
||||||
|
|
||||||
import com.alibaba.testable.core.annotation.MockConstructor;
|
|
||||||
import com.alibaba.testable.core.annotation.MockMethod;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 演示模板方法的Mock场景
|
|
||||||
* Demonstrate scenario of mocking template method
|
|
||||||
*/
|
|
||||||
class DemoTemplateTest {
|
|
||||||
|
|
||||||
private DemoTemplate demoTemplate = new DemoTemplate();
|
|
||||||
|
|
||||||
/* 第一种写法:使用泛型定义 */
|
|
||||||
/* First solution: use generics type */
|
|
||||||
|
|
||||||
@MockMethod
|
|
||||||
private static <T> List<T> getList(DemoTemplate self, T value) {
|
|
||||||
return new ArrayList<T>() {{ add((T)(value.toString() + "_mock_list")); }};
|
|
||||||
}
|
|
||||||
|
|
||||||
@MockMethod
|
|
||||||
private static <K, V> Map<K, V> getMap(DemoTemplate self, K key, V value) {
|
|
||||||
return new HashMap<K, V>() {{ put(key, (V)(value.toString() + "_mock_map")); }};
|
|
||||||
}
|
|
||||||
|
|
||||||
@MockConstructor
|
|
||||||
private <T> HashSet<T> newHashSet() {
|
|
||||||
HashSet<T> set = new HashSet<>();
|
|
||||||
set.add((T)"insert_mock");
|
|
||||||
return set;
|
|
||||||
}
|
|
||||||
|
|
||||||
@MockMethod
|
|
||||||
private <E> boolean add(Set s, E e) {
|
|
||||||
s.add(e.toString() + "_mocked");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 第二种写法:使用Object类型 */
|
|
||||||
/* Second solution: use object type */
|
|
||||||
|
|
||||||
//@MockMethod
|
|
||||||
//private static List<Object> getList(DemoTemplate self, Object value) {
|
|
||||||
// return new ArrayList<Object>() {{ add(value.toString() + "_mock_list"); }};
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//@MockMethod
|
|
||||||
//private static Map<Object, Object> getMap(DemoTemplate self, Object key, Object value) {
|
|
||||||
// return new HashMap<Object, Object>() {{ put(key, value.toString() + "_mock_map"); }};
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//@MockConstructor
|
|
||||||
//private HashSet newHashSet() {
|
|
||||||
// HashSet<Object> set = new HashSet<>();
|
|
||||||
// set.add("insert_mock");
|
|
||||||
// return set;
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//@MockMethod
|
|
||||||
//private boolean add(Set s, Object e) {
|
|
||||||
// s.add(e.toString() + "_mocked");
|
|
||||||
// return true;
|
|
||||||
//}
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void should_able_to_mock_single_template_method() {
|
|
||||||
String res = demoTemplate.singleTemplateMethod();
|
|
||||||
assertEquals("demo_mock_list", res);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void should_able_to_mock_double_template_method() {
|
|
||||||
String res = demoTemplate.doubleTemplateMethod();
|
|
||||||
assertEquals("testable_mock_map", res);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void should_able_to_mock_new_template_method() {
|
|
||||||
Set<?> res = demoTemplate.newTemplateMethod();
|
|
||||||
assertEquals(2, res.size());
|
|
||||||
Iterator<?> iterator = res.stream().iterator();
|
|
||||||
assertEquals("insert_mock", iterator.next());
|
|
||||||
assertEquals("world_mocked", iterator.next());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
2
demo/java-demo/src/test/resources/testable.properties
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
omni.constructor.enhance.enable = true
|
||||||
|
mock.target.checking.enable = true
|
||||||
@@ -9,6 +9,7 @@ version = "1.0.0-SNAPSHOT"
|
|||||||
java.sourceCompatibility = JavaVersion.VERSION_1_8
|
java.sourceCompatibility = JavaVersion.VERSION_1_8
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
|
mavenLocal()
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -16,8 +17,8 @@ dependencies {
|
|||||||
implementation("org.jetbrains.kotlin:kotlin-reflect")
|
implementation("org.jetbrains.kotlin:kotlin-reflect")
|
||||||
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
|
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
|
||||||
testImplementation("org.junit.jupiter:junit-jupiter:5.6.2")
|
testImplementation("org.junit.jupiter:junit-jupiter:5.6.2")
|
||||||
testImplementation("com.alibaba.testable:testable-all:0.4.11")
|
testImplementation("com.alibaba.testable:testable-all:0.6.7")
|
||||||
testAnnotationProcessor("com.alibaba.testable:testable-processor:0.4.11")
|
testAnnotationProcessor("com.alibaba.testable:testable-processor:0.6.7")
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.withType<KotlinCompile> {
|
tasks.withType<KotlinCompile> {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
<junit.version>5.6.2</junit.version>
|
<junit.version>5.6.2</junit.version>
|
||||||
<testable.version>0.4.11</testable.version>
|
<testable.version>0.6.7</testable.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
rootProject.name = "demo"
|
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package com.alibaba.demo.association
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 目标类,此类中的一些调用将会被Mock掉
|
||||||
|
* Target class, some invocations inside this class will be mocked
|
||||||
|
*/
|
||||||
|
class CookerService {
|
||||||
|
|
||||||
|
private fun cookSandwich(): String {
|
||||||
|
return "Real-Sandwich"
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun cookHamburger(): String {
|
||||||
|
return "Real-Hamburger"
|
||||||
|
}
|
||||||
|
|
||||||
|
fun prepareSandwich(): String {
|
||||||
|
return hireSandwichCooker() + " & " + cookSandwich()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun prepareHamburger(): String {
|
||||||
|
return hireHamburgerCooker() + " & " + cookHamburger()
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private fun hireSandwichCooker(): String {
|
||||||
|
return "Real-Sandwich-Cooker"
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun hireHamburgerCooker(): String {
|
||||||
|
return "Real-Hamburger-Cooker"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.alibaba.demo.association
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 被测类,会访问`CookerService`里的方法
|
||||||
|
* Class to be tested, which will access methods in TargetService class
|
||||||
|
*/
|
||||||
|
class SellerService {
|
||||||
|
|
||||||
|
private val cookerService = CookerService()
|
||||||
|
|
||||||
|
fun sellSandwich(): String {
|
||||||
|
return cookerService.prepareSandwich()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun sellHamburger(): String {
|
||||||
|
return cookerService.prepareHamburger()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package com.alibaba.testable.demo
|
package com.alibaba.demo.basic
|
||||||
|
|
||||||
import com.alibaba.testable.demo.model.BlackBox
|
import com.alibaba.demo.basic.model.mock.BlackBox
|
||||||
import com.alibaba.testable.demo.model.Box
|
import com.alibaba.demo.basic.model.mock.Box
|
||||||
import com.alibaba.testable.demo.model.Color
|
import com.alibaba.demo.basic.model.mock.Color
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 演示父类变量引用子类对象时的Mock场景
|
* 演示父类变量引用子类对象时的Mock场景
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package com.alibaba.demo.basic
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutionException
|
||||||
|
import java.util.concurrent.Executors
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 演示对内部类的Mock支持
|
||||||
|
* Demonstrate support for mocking invocation inside a inner class
|
||||||
|
*/
|
||||||
|
class DemoInnerClass {
|
||||||
|
|
||||||
|
class StaticInner {
|
||||||
|
/**
|
||||||
|
* invocation inside a static inner class
|
||||||
|
*/
|
||||||
|
fun demo(): String {
|
||||||
|
return methodToBeMock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inner class Inner {
|
||||||
|
/**
|
||||||
|
* invocation inside a non-static inner class
|
||||||
|
*/
|
||||||
|
fun demo(): String {
|
||||||
|
return methodToBeMock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val executorService = Executors.newSingleThreadExecutor()
|
||||||
|
@Throws(ExecutionException::class, InterruptedException::class)
|
||||||
|
fun callAnonymousInner(): String {
|
||||||
|
/**
|
||||||
|
* invocation inside a anonymous inner class
|
||||||
|
*/
|
||||||
|
val future = executorService.submit<String> { methodToBeMock() }
|
||||||
|
return future.get()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun callInnerDemo(): String {
|
||||||
|
return Inner().demo()
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun methodToBeMock(): String {
|
||||||
|
return "RealCall"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package com.alibaba.testable.demo
|
package com.alibaba.demo.basic
|
||||||
|
|
||||||
import com.alibaba.testable.demo.model.BlackBox
|
import com.alibaba.demo.basic.model.mock.BlackBox
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package com.alibaba.testable.demo
|
package com.alibaba.demo.basic
|
||||||
|
|
||||||
import com.alibaba.testable.demo.model.BlackBox
|
import com.alibaba.demo.basic.model.mock.BlackBox
|
||||||
import com.alibaba.testable.demo.model.ColorBox
|
import com.alibaba.demo.basic.model.mock.ColorBox
|
||||||
import java.nio.file.Files
|
import java.nio.file.Files
|
||||||
import java.nio.file.Paths
|
import java.nio.file.Paths
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.alibaba.testable.demo
|
package com.alibaba.demo.basic
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 演示私有成员访问功能
|
* 演示私有成员访问功能
|
||||||
@@ -9,7 +9,7 @@ class DemoPrivateAccess {
|
|||||||
/**
|
/**
|
||||||
* a private member field
|
* a private member field
|
||||||
*/
|
*/
|
||||||
private var count = 0
|
var count = 0
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* a constant field
|
* a constant field
|
||||||
@@ -19,7 +19,7 @@ class DemoPrivateAccess {
|
|||||||
/**
|
/**
|
||||||
* private member method
|
* private member method
|
||||||
*/
|
*/
|
||||||
private fun privateFunc(list: List<String>, str: String, i: Int): String {
|
fun privateFunc(list: List<String>, str: String, i: Int): String {
|
||||||
return list.reduce { a: String, s: String -> a + s } + " + " + "$str + $i"
|
return list.reduce { a: String, s: String -> a + s } + " + " + "$str + $i"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,12 +28,12 @@ class DemoPrivateAccess {
|
|||||||
/**
|
/**
|
||||||
* a private static field
|
* a private static field
|
||||||
*/
|
*/
|
||||||
private var staticCount = 0
|
var staticCount = 0
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* private static method
|
* private static method
|
||||||
*/
|
*/
|
||||||
private fun privateStaticFunc(str: String, i: Int): String {
|
fun privateStaticFunc(str: String, i: Int): String {
|
||||||
return "$str + $i"
|
return "$str + $i"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.alibaba.testable.demo
|
package com.alibaba.demo.basic
|
||||||
|
|
||||||
import java.util.ArrayList
|
import java.util.ArrayList
|
||||||
import java.util.HashMap
|
import java.util.HashMap
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.alibaba.testable.demo.model
|
package com.alibaba.demo.basic.model.mock
|
||||||
|
|
||||||
|
|
||||||
class BlackBox(var input: String) : Box(), Color {
|
class BlackBox(var input: String) : Box(), Color {
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.alibaba.testable.demo.model
|
package com.alibaba.demo.basic.model.mock
|
||||||
|
|
||||||
abstract class Box {
|
abstract class Box {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.alibaba.testable.demo.model
|
package com.alibaba.demo.basic.model.mock
|
||||||
|
|
||||||
interface Color {
|
interface Color {
|
||||||
|
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.alibaba.demo.basic.model.omni
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 我是一个只有私有构造方法的类
|
||||||
|
* This class have only private constructor
|
||||||
|
*/
|
||||||
|
class Child private constructor() {
|
||||||
|
|
||||||
|
// ---------- 内部成员字段 ----------
|
||||||
|
var grandChild: GrandChild? = null
|
||||||
|
var subChild: InnerChild? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 这是一个私有内部类
|
||||||
|
* An private inner class
|
||||||
|
*/
|
||||||
|
inner class InnerChild {
|
||||||
|
private val secret: String? = null
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.alibaba.demo.basic.model.omni
|
||||||
|
|
||||||
|
class GrandChild {
|
||||||
|
|
||||||
|
// ---------- 内部成员字段 ----------
|
||||||
|
var value = 0
|
||||||
|
var content: String? = null
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.alibaba.demo.basic.model.omni
|
||||||
|
|
||||||
|
import java.lang.IllegalArgumentException
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 我是一个虽有构造方法,但无法正常构造的类
|
||||||
|
* This class have constructor with exception throw
|
||||||
|
*/
|
||||||
|
class Parent {
|
||||||
|
|
||||||
|
init {
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- 内部成员字段 ----------
|
||||||
|
var child: Child? = null
|
||||||
|
var children: Array<Child>? = null
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
package com.alibaba.testable.demo.util
|
package com.alibaba.demo.java2kotlin
|
||||||
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
object PathUtil {
|
object PathDemo {
|
||||||
|
|
||||||
fun deleteRecursively(file: File) {
|
fun deleteRecursively(file: File) {
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.alibaba.demo.one2multi
|
||||||
|
|
||||||
|
class ASvc {
|
||||||
|
|
||||||
|
fun demo(name: String): String {
|
||||||
|
return String.format("a_%s", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.alibaba.demo.one2multi
|
||||||
|
|
||||||
|
class BSvc {
|
||||||
|
|
||||||
|
fun demo(name: String): String {
|
||||||
|
return String.format("b_%s", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.alibaba.demo.one2multi
|
||||||
|
|
||||||
|
class CSvc {
|
||||||
|
|
||||||
|
fun demo(name: String): String {
|
||||||
|
return String.format("c_%s", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.alibaba.demo.association
|
||||||
|
|
||||||
|
import com.alibaba.testable.core.annotation.MockMethod
|
||||||
|
import com.alibaba.testable.core.model.MockScope
|
||||||
|
|
||||||
|
internal class CookerServiceMock {
|
||||||
|
|
||||||
|
@MockMethod(targetClass = CookerService::class)
|
||||||
|
private fun cookSandwich(): String {
|
||||||
|
return "Faked-Sandwich"
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockMethod(targetClass = CookerService::class, scope = MockScope.ASSOCIATED)
|
||||||
|
private fun cookHamburger(): String {
|
||||||
|
return "Faked-Hamburger"
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockMethod(targetClass = CookerService::class)
|
||||||
|
fun hireSandwichCooker(): String {
|
||||||
|
return "Fake-Sandwich-Cooker"
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockMethod(targetClass = CookerService::class, scope = MockScope.ASSOCIATED)
|
||||||
|
fun hireHamburgerCooker(): String {
|
||||||
|
return "Fake-Hamburger-Cooker"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.alibaba.demo.association
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
|
internal class SellerServiceTest {
|
||||||
|
|
||||||
|
private val sellerService = SellerService()
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun should_sell_sandwich() {
|
||||||
|
assertEquals("Fake-Sandwich-Cooker & Faked-Sandwich", sellerService.sellSandwich())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun should_sell_hamburger() {
|
||||||
|
assertEquals("Real-Hamburger-Cooker & Real-Hamburger", sellerService.sellHamburger())
|
||||||
|
}
|
||||||
|
}
|
||||||