Skip to content

Commit c35b560

Browse files
committed
Add FakeDependencyGraphsService to replace reflection in tests
Replace hacky reflection code in DefaultAndroidUnitTestDataExtractorTest with a proper FakeDependencyGraphsService class that extends DefaultDependencyGraphsService and accepts DependencyGraphs in constructor.
1 parent 616e0e2 commit c35b560

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2022 Grabtaxi Holdings PTE LTD (GRAB)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.grab.grazel.fake
18+
19+
import com.grab.grazel.gradle.dependencies.DefaultDependencyGraphsService
20+
import com.grab.grazel.gradle.dependencies.DependencyGraphs
21+
import com.grab.grazel.gradle.dependencies.DependencyGraphsService
22+
23+
/**
24+
* A test fake for [DependencyGraphsService] that returns pre-configured [DependencyGraphs].
25+
*
26+
* Usage:
27+
* ```kotlin
28+
* val fakeService = FakeDependencyGraphsService(FakeDependencyGraphs())
29+
* val provider = project.provider { fakeService }
30+
* ```
31+
*/
32+
internal class FakeDependencyGraphsService(
33+
private val dependencyGraphs: DependencyGraphs = FakeDependencyGraphs()
34+
) : DefaultDependencyGraphsService() {
35+
36+
override fun get(): DependencyGraphs = dependencyGraphs
37+
38+
override fun getParameters(): DependencyGraphsService.Params {
39+
throw UnsupportedOperationException("Not needed for tests")
40+
}
41+
}

grazel-gradle-plugin/src/test/kotlin/com/grab/grazel/migrate/android/DefaultAndroidUnitTestDataExtractorTest.kt

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.grab.grazel.GrazelExtension
2222
import com.grab.grazel.GrazelPluginTest
2323
import com.grab.grazel.buildProject
2424
import com.grab.grazel.fake.FakeDependencyGraphs
25+
import com.grab.grazel.fake.FakeDependencyGraphsService
2526
import com.grab.grazel.gradle.ANDROID_LIBRARY_PLUGIN
2627
import com.grab.grazel.gradle.DefaultConfigurationDataSource
2728
import com.grab.grazel.gradle.KOTLIN_ANDROID_PLUGIN
@@ -113,20 +114,7 @@ class DefaultAndroidUnitTestDataExtractorTest : GrazelPluginTest() {
113114
val dependencyGraphs = FakeDependencyGraphs()
114115
val androidManifestParser: AndroidManifestParser = DefaultAndroidManifestParser()
115116

116-
// Create a test implementation of DependencyGraphsService
117-
val testDependencyGraphsService = object : DefaultDependencyGraphsService() {
118-
init {
119-
// Pre-initialize with fake dependency graphs for testing
120-
val field =
121-
DefaultDependencyGraphsService::class.java.getDeclaredField("dependencyGraphs")
122-
field.isAccessible = true
123-
field.set(this, dependencyGraphs)
124-
}
125-
126-
override fun getParameters(): com.grab.grazel.gradle.dependencies.DependencyGraphsService.Params {
127-
throw UnsupportedOperationException("Not needed for tests")
128-
}
129-
}
117+
val testDependencyGraphsService = FakeDependencyGraphsService(dependencyGraphs)
130118

131119
val mockDependencyGraphsService: GradleProvider<DefaultDependencyGraphsService> =
132120
rootProject.provider { testDependencyGraphsService }

0 commit comments

Comments
 (0)