Hello guys, I have a function `buildRequestUrl` wh...
# coroutines
k
Hello guys, I have a function
buildRequestUrl
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
Copy code
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
Copy code
@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:
Copy code
@get:Rule
    val testInstantTaskExecutorRule: TestRule = InstantTaskExecutorRule()

    @get:Rule
    val testCoroutineRule = TestCoroutineRule()
m
This is not related to coroutines. What is the value of
baseUrl
? It may not be a valid URL and thus cannot be parsed.
k
Sorry but I posted this here because I am using a coroutines for api calls so I thought it is related to that. Sorry I will delete my post from here. And yes the url is valid and when I debug the test and use the evaluate expression and pass baseUrl than I get valid the url data but when I evaluate the whole expression, I got the null
m
You can’t delete entire threads 🙂 So why is
Uri.parse(baseUrl)
null when it’s valid? What’s the value of
baseUrl
?
k
Yes okay 🙂 I can share the private data of the company but I can say that it is in working condition because for the website we are using the same URL and the website is in working condition One thing I noticed that
Uri
 is an Android class and as such cannot be used in local unit tests ...is this the issue ?
m
It should work just fine in Android unit tests 🤔
I remember that
Uri
is quite strict about URLs being correct.
k
Yes URL is 100% correct 🙂
m
Hmm, there’s not much else I could think of apart from
Uri.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.
k
<https://staging.XYZ.com/api/v3>
this is the url
m
That’s definitely legit 😄
😀 1
And you’re sure that
baseUrl
has the same value when you debug it at the same location? Maybe it’s not properly initialized 🤔
k
Yes I got the same value while debugging the info. In the test do we need to initialise?
m
I’ve looked at the code of
Uri.parse
. It never returns
null
😮
Is that really
<http://android.net|android.net>.Uri
?
Looks like it’s indeed not available
k
Okay I will try this out thank you 🙂