6

I'm trying to using backticks ` in the definition of my instrumented tests. I don't understand why compiler complains about a strange error:

Caused by: com.android.tools.r8.internal.Jj: com.android.tools.r8.internal.Jc: Space characters in SimpleName 'given a closebottomsheetevent eventlistener onCloseBottomSheet should be called' are not allowed prior to DEX version 040

enter image description here

My test is a pretty standard instrumented test for compose

package com.dooitu.mobile.android.ui.homefeed

import android.content.Context
import androidx.activity.ComponentActivity
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import com.dooitu.mobile.android.ui.homefeed.components.HomeFeedEvent
import com.dooitu.mobile.android.ui.homefeed.models.HomeFeedScreenEventListener
import com.dooitu.mobile.android.ui.theme.DooituTheme
import org.junit.Rule
import org.junit.Test
import org.koin.androidx.compose.get

class HomeFeedScreenTest {
    @get:Rule
    // Use a dummy activity instead of real MainActivity
    val composeTestRule = createAndroidComposeRule<ComponentActivity>()
    private var triggeredEventId: Int? = null
    private val eventListener = object : HomeFeedScreenEventListener {
        override fun onCloseBottomSheet() {
            triggeredEventId = ON_CLOSE_BOTTOM_SHEET_EVENT_ID
        }

        override fun onOpenComments(postId: String, numberOfComments: Int) {
            triggeredEventId = ON_OPEN_COMMENTS_EVENT_ID
        }

        override fun onOpenShare(shareLink: String, context: Context) {
            TODO("Not yet implemented")
        }

        override fun onOpenPostSingleView(postId: String) {
            TODO("Not yet implemented")
        }

        override fun onOpenPostActions(postId: String, userId: String) {
            TODO("Not yet implemented")
        }

        override fun onOpenPostDetails(postId: String) {
            TODO("Not yet implemented")
        }

        override fun onProfileImageClicked(userId: String, isCurrentUser: Boolean) {
            TODO("Not yet implemented")
        }
    }
    private val updateViewModel = HomeFeedUpdateViewModel()

    private fun startScenario(state: State<HomeFeedState>) {
        composeTestRule.setContent {
            DooituTheme.SurfaceContainer {
                HomeFeedScreen(
                    state = state,
                    viewModel = get(),
                    cacheDataSourceFactory = get(),
                    getReloadedPostFlow = updateViewModel::reloadedPost,
                    eventsListener = eventListener,
                    logScreenView = {}
                )
            }
        }
    }

    @Test
    fun `given a closebottomsheetevent eventlistener on close bottom sheet should be called` () {
        val closeBottomSheetState = mutableStateOf(
            HomeFeedState(
                emptyList(),
                emptyList(),
                event = HomeFeedEvent.CloseBottomSheet
            )
        )
        // Start the app
        startScenario(closeBottomSheetState)
        assert(triggeredEventId == ON_CLOSE_BOTTOM_SHEET_EVENT_ID)
    }
}

If I remove backticks and spaces test compiles without any problem :(

Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64

2 Answers2

5

That syntax is super-cool but it's not currently supported by Android runtime https://kotlinlang.org/docs/coding-conventions.html#names-for-test-methods

I think in the doc is missing the note about it's available in the unit tests on Android (that works on the JVM) and not in the instrumented tests (Android runtime)

cmunaro
  • 66
  • 1
  • 3
1

Names that have spaces enclosed in backticks is a great feature of Kotlin and can be used in your Unit Tests. However, this will not work with Android Implementation tests.

To expand on @cmunaro's answer: The KotlinLang Docs Names for test methods states the following:

In tests (and only in tests), you can use method names with spaces enclosed in backticks. Note that such method names are currently not supported by the Android runtime. Underscores in method names are also allowed in test code.

class MyTestCase {
     @Test fun `ensure everything works`() { /*...*/ }

     @Test fun ensureEverythingWorks_onAndroid() { /*...*/ }
 }

Android's Build Instrumentation Tests Guide shows examples of tests with snake_case names:

@RunWith(AndroidJUnit4::class)
class LogHistoryAndroidUnitTest {
     @Test
     fun logHistory_ParcelableWriteRead() { ... }
}
DroidClicketyClacker
  • 2,057
  • 1
  • 10
  • 10