KV
10/21/2020, 12:43 PMbuildRequestUrl
which build the URL including headers and query parameters.
Now I am working on tests for api call's response.
and I am debugging the code where I build the URL which is this
val uriBuilder = Uri.parse(baseUrl).buildUpon()
But the issue is it throws null pointer exception and result is null (see attached screenshot)
How do I solve this issue?
I am using below library
testImplementation 'junit:junit:4.13'
testImplementation "org.mockito:mockito-core:3.4.6"
testImplementation 'androidx.arch.core:core-testing:2.1.0'
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.9'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
I am using below class
@ExperimentalCoroutinesApi
class TestCoroutineRule : TestRule {
private val testCoroutineDispatcher = TestCoroutineDispatcher()
private val testCoroutineScope = TestCoroutineScope(testCoroutineDispatcher)
override fun apply(base: Statement, description: Description?) = object : Statement() {
@Throws(Throwable::class)
override fun evaluate() {
Dispatchers.setMain(testCoroutineDispatcher)
base.evaluate()
Dispatchers.resetMain()
testCoroutineScope.cleanupTestCoroutines()
}
}
fun runBlockingTest(block: suspend TestCoroutineScope.() -> Unit) =
testCoroutineScope.runBlockingTest { block() }
}
I am using the below rule in the test class:
@get:Rule
val testInstantTaskExecutorRule: TestRule = InstantTaskExecutorRule()
@get:Rule
val testCoroutineRule = TestCoroutineRule()
Marc Knaup
10/21/2020, 12:56 PMbaseUrl
? It may not be a valid URL and thus cannot be parsed.KV
10/21/2020, 1:22 PMMarc Knaup
10/21/2020, 1:24 PMUri.parse(baseUrl)
null when it’s valid?
What’s the value of baseUrl
?KV
10/21/2020, 1:27 PMUri
is an Android class and as such cannot be used in local unit tests ...is this the issue ?Marc Knaup
10/21/2020, 1:28 PMMarc Knaup
10/21/2020, 1:28 PMUri
is quite strict about URLs being correct.KV
10/21/2020, 1:29 PMMarc Knaup
10/21/2020, 1:32 PMUri.parse()
being broken at that location for some reason. But that’s quite unlikely.
Can you share the URL with the hostname changed or something like that?
Just don’t change it too much or we may miss a detail that could explain the issue.KV
10/21/2020, 1:34 PM<https://staging.XYZ.com/api/v3>
KV
10/21/2020, 1:34 PMMarc Knaup
10/21/2020, 1:34 PMMarc Knaup
10/21/2020, 1:35 PMbaseUrl
has the same value when you debug it at the same location?
Maybe it’s not properly initialized 🤔KV
10/21/2020, 1:37 PMMarc Knaup
10/21/2020, 1:38 PMUri.parse
. It never returns null
😮Marc Knaup
10/21/2020, 1:38 PM<http://android.net|android.net>.Uri
?Marc Knaup
10/21/2020, 1:39 PMMarc Knaup
10/21/2020, 1:39 PMKV
10/21/2020, 1:39 PM