Skip to content

Commit 106c11f

Browse files
committed
[CodeSnippet][1/1]{接入mockk单元测试框架}
适用范围:{>=Q} 准入id:{NA} 分析:{xxx} 方案:{xxx} 风险及影响[快/稳/省/功能/安全隐私]:{NA} 测试建议:{} 跨组依赖:{NA}
1 parent 55e3fca commit 106c11f

File tree

4 files changed

+67
-10
lines changed

4 files changed

+67
-10
lines changed

Diff for: app/build.gradle

+11-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ android {
1818
targetSdkVersion 30
1919
versionCode versions.version_code
2020
versionName versions.version_name
21+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
2122
}
2223
buildTypes {
2324
debug {
@@ -50,21 +51,23 @@ android {
5051

5152
dependencies {
5253
implementation fileTree(dir: 'libs', include: ['*.jar'])
53-
implementation 'androidx.appcompat:appcompat:1.2.0'
54-
implementation 'androidx.recyclerview:recyclerview:1.1.0'
54+
implementation 'androidx.appcompat:appcompat:1.3.0'
55+
implementation 'androidx.recyclerview:recyclerview:1.2.1'
5556
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
5657
implementation 'com.google.android:flexbox:0.2.5'
57-
implementation 'androidx.work:work-runtime:2.4.0'
58-
implementation "androidx.core:core-ktx:1.3.2"
58+
implementation 'androidx.work:work-runtime:2.5.0'
59+
implementation "androidx.core:core-ktx:1.6.0"
5960
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
6061
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2'
6162
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2"
62-
implementation "com.google.dagger:hilt-android:2.28-alpha"
63-
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02'
63+
implementation "com.google.dagger:hilt-android:2.37"
64+
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
6465
implementation 'com.zhouyou:signseekbar:1.0.6'
6566
implementation 'com.airbnb.android:lottie:3.7.0'
66-
kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"
67-
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha02'
67+
kapt 'androidx.hilt:hilt-compiler:1.0.0'
68+
kapt "com.google.dagger:hilt-android-compiler:2.37"
69+
testImplementation "io.mockk:mockk:1.12.3"
70+
testImplementation 'junit:junit:4.13.2'
6871
}
6972
repositories {
7073
mavenCentral()
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package x.chestnut.code.snippet.test
2+
3+
class Car {
4+
var carName = "BenChi"
5+
private val userAge = 16
6+
fun recordTelemetry(speed: Double) {
7+
8+
}
9+
fun sumUnit(a: Int, b: Int): Unit {
10+
println(a+b)
11+
}
12+
fun sumVoid(a: Int, b: Int) {
13+
println(a+b)
14+
}
15+
fun hello() {
16+
print("hi, my age is $userAge")
17+
}
18+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package x.chestnut.code.snippet.test
2+
3+
import io.mockk.*
4+
import org.junit.Test
5+
6+
class CarTest {
7+
8+
@Test
9+
fun `should print when recordTelemetry`() {
10+
//GIVEN
11+
val car = mockk<Car>()
12+
val slot = slot<Double>()
13+
every {
14+
car.recordTelemetry(speed = capture(slot))
15+
} answers {
16+
println(slot.captured)
17+
}
18+
//WHEN
19+
car.recordTelemetry(15.0)
20+
//THEN
21+
verify {
22+
car.recordTelemetry(speed = 15.0)
23+
}
24+
}
25+
26+
@Test
27+
fun `should return no when sumVoid`() {
28+
val car = spyk<Car>()
29+
every { car.carName = any() } propertyType String::class answers {
30+
println("第一个参数是:$fieldValue")
31+
fieldValue = "abc"
32+
}
33+
car.carName = "cde"
34+
println("name: ${car.carName}")
35+
}
36+
}

Diff for: build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4-
ext.kotlin_version = '1.4.20'
4+
ext.kotlin_version = '1.6.0'
55

66
repositories {
77
maven{url 'http://maven.aliyun.com/nexus/content/groups/public/'}
@@ -13,7 +13,7 @@ buildscript {
1313
// NOTE: Do not place your application dependencies here; they belong
1414
// in the individual module build.gradle files
1515
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
16-
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'
16+
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.37'
1717
}
1818
}
1919

0 commit comments

Comments
 (0)