英文:
com.android.tools.r8.internal.Jc: Space characters in SimpleName exception are not allowed prior to DEX version 040
问题
我试图在我的仪器化测试的定义中使用反引号`,但我不明白为什么编译器会抱怨一个奇怪的错误:
Caused by: com.android.tools.r8.internal.Jj: com.android.tools.r8.internal.Jc: SimpleName 'given a closebottomsheetevent eventlistener onCloseBottomSheet should be called' 中不允许在DEX版本040之前有空格字符
我的测试是一个相当标准的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 `给定一个closebottomsheetevent事件监听器,在关闭底部表应该被调用`() {
val closeBottomSheetState = mutableStateOf(
HomeFeedState(
emptyList(),
emptyList(),
event = HomeFeedEvent.CloseBottomSheet
)
)
// 启动应用
startScenario(closeBottomSheetState)
assert(triggeredEventId == ON_CLOSE_BOTTOM_SHEET_EVENT_ID)
}
}
如果我删除反引号和空格,测试将编译而无任何问题。
英文:
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
My test is a pretty standard instrumented test for compose
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-kotlin -->
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)
}
}
<!-- end snippet -->
If I remove backticks and spaces test compiles without any problem
答案1
得分: 5
该语法非常酷,但目前不受Android运行时支持。
https://kotlinlang.org/docs/coding-conventions.html#names-for-test-methods
我认为文档中缺少关于它在Android上的单元测试中可用(在JVM上运行)而不在仪器化测试中(Android运行时)的说明。
英文:
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)
答案2
得分: 1
用反引号括起来的具有空格的名称是Kotlin的一个很棒的功能,可以在您的单元测试中使用。但是,这在Android实现测试中不起作用。
要进一步说明@cmunaro的回答:KotlinLang文档测试方法的命名中指出:
在测试中(仅在测试中),您可以使用用反引号括起来的具有空格的方法名称。请注意,Android运行时当前不支持此类方法名称。方法名称中也允许使用下划线。
class MyTestCase {
@Test fun `ensure everything works`() { /*...*/ }
@Test fun ensureEverythingWorks_onAndroid() { /*...*/ }
}
Android的构建仪器测试指南显示了具有snake_case名称的测试示例:
@RunWith(AndroidJUnit4::class)
class LogHistoryAndroidUnitTest {
@Test
fun logHistory_ParcelableWriteRead() { ... }
}
英文:
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() { ... }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论