Tôi gặp sự cố khi chạy androidTest của mình.Không tìm thấy lớp học, Bộ thử nghiệm trống trong androidTest sử dụng Android Studio 3.0.1, Phòng, Kotlin
Đây là thiết lập của tôi trong gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.blabla.shoppinglistapp"
minSdkVersion 17
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
ext.daggerVersion = '2.11'
ext.roomVersion = '1.0.0'
ext.mockitoVersion = '2.11.0'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-android:2.8.47'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:1.0.0"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0"
// RxJava
implementation 'io.reactivex.rxjava2:rxjava:2.1.5'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'com.jakewharton.rxbinding2:rxbinding:2.0.0'
// Room
implementation "android.arch.persistence.room:runtime:$roomVersion"
implementation "android.arch.persistence.room:rxjava2:$roomVersion"
kapt "android.arch.persistence.room:compiler:$roomVersion"
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.2.10"
androidTestImplementation "android.arch.persistence.room:testing:$roomVersion"
// Gson
implementation 'com.google.code.gson:gson:2.8.2'
}
Và đây là thử nghiệm (hầu như không có gì ở đây):
@RunWith(AndroidJUnit4::class)
class ShoppingListDaoTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getTargetContext()
assertEquals("com.blabla.shoppinglistapp", appContext.packageName)
}
private lateinit var database: ShoppingListDatabase
@Before
fun initDb() {
// using an in-memory database because the information stored here disappears after test
database = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(),
ShoppingListDatabase::class.java)
// allowing main thread queries, just for testing
.allowMainThreadQueries()
.build()
}
@After
fun closeDb() {
database.close()
}
@Test fun testGetActiveShoppingListWhenNoActiveShoppingListInserted() {
database.shoppingListDao().getActiveShoppingLists()
.test()
.assertNoValues()
}
}
Khi tôi cố gắng để chạy thử nghiệm tôi có được điều này:
Process finished with exit code 1 Class not found: "com.blabla.shoppinglistapp.ShoppingListDaoTest"Empty test suite.
Và nó không phải là bắt đầu
CẬP NHẬT
Tôi nhận thấy một số điều thực sự tồi tệ. Khi tôi bắt đầu dự án mới trong Android Studio và cố gắng chạy mặc định androidTest nó mang lại cho tôi lỗi tiếng:
Error:Gradle: java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
Các workround của việc này là để thêm dòng này vào gradle.properties:
android.enableAapt2=false
này là các bước mà tôi đã thực hiện trong dự án ban đầu của tôi trước đó (tôi cũng chạy vào lỗi đó). Thật không may, sau khi nó cho tôi lỗi từ câu hỏi của tôi. Vì vậy, nó thực sự là vấn đề làm thế nào để giải quyết các isse với aapt2 để thực hiện công việc này. Xem tại đây cũng: AAPT2 error: check logs for details, after workround the androidTest not working
Bất kỳ ý tưởng nào?
này là loại vô lý mà làm việc này (cảm ơn cho câu trả lời) ... bạn có thể nhấp chuột không đúng trên một thử nghiệm và chạy nó, nhưng bạn có thể nhấp chuột phải vào thư mục mẹ và chạy nó .... Tôi mới đến android nhưng thử nghiệm có vẻ như bị hỏng .... – jacoballenwood
@jacoballenwood Ya nó bị hỏng trong kotlin. Nếu bạn đang viết bài kiểm tra của bạn trong Java, nó hoạt động hoàn hảo. Nhưng bây giờ, công cụ này không hỗ trợ đầy đủ Kotlin. Hy vọng họ sẽ sửa nó sớm. – Henry