https://kotlinlang.org logo
#coroutines
Title
# coroutines
k

KV

10/21/2020, 12:43 PM
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

Marc Knaup

10/21/2020, 12:56 PM
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

KV

10/21/2020, 1:22 PM
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

Marc Knaup

10/21/2020, 1:24 PM
You can’t delete entire threads 🙂 So why is
Uri.parse(baseUrl)
null when it’s valid? What’s the value of
baseUrl
?
k

KV

10/21/2020, 1:27 PM
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

Marc Knaup

10/21/2020, 1:28 PM
It should work just fine in Android unit tests 🤔
I remember that
Uri
is quite strict about URLs being correct.
k

KV

10/21/2020, 1:29 PM
Yes URL is 100% correct 🙂
m

Marc Knaup

10/21/2020, 1:32 PM
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

KV

10/21/2020, 1:34 PM
<https://staging.XYZ.com/api/v3>
this is the url
m

Marc Knaup

10/21/2020, 1:34 PM
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

KV

10/21/2020, 1:37 PM
Yes I got the same value while debugging the info. In the test do we need to initialise?
m

Marc Knaup

10/21/2020, 1:38 PM
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

KV

10/21/2020, 1:39 PM
Okay I will try this out thank you 🙂
5 Views