2017-11-13 59 views
7

Tôi có một vấn đề với thử nghiệm một cơ sở dữ liệu phòng: khi tôi chạy thử nghiệm, tôi nhận được ngoại lệ này:Phòng không thể tìm thấy thi

java.lang.RuntimeException: cannot find implementation for database.TicketDatabase. TicketDatabase_Impl does not exist 
at android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:92) 
at android.arch.persistence.room.RoomDatabase$Builder.build(RoomDatabase.java:454) 
at com.sw.ing.gestionescontrini.DatabaseTest.setDatabase(DatabaseTest.java:36) 
at java.lang.reflect.Method.invoke(Native Method) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) 
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runners.Suite.runChild(Suite.java:128) 
at org.junit.runners.Suite.runChild(Suite.java:27) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:115) 
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59) 
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262) 
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1886) 

Lớp DatabaseTest:

public class DatabaseTest { 

    TicketDatabase database; 
    DAO ticketDAO; 


    @Before 
    public void setDatabase() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { 
     Context context = InstrumentationRegistry.getTargetContext(); 
     database = Room.inMemoryDatabaseBuilder(context, TicketDatabase.class).build(); 

     Method method = TicketDatabase.class.getDeclaredMethod("ticketDao()"); 
     method.setAccessible(true); 
     ticketDAO = (DAO) method.invoke(database, null); 
    } 
} 

tập tin gradle:

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 26 
    buildToolsVersion '26.0.2' 
    defaultConfig { 
     applicationId "com.sw.ing.gestionescontrini" 
     minSdkVersion 15 
     targetSdkVersion 26 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 
    defaultConfig { 
     multiDexEnabled true 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 
    compile 'com.android.support:appcompat-v7:26.+' 
    compile 'com.android.support.constraint:constraint-layout:1.0.2' 
    testCompile 'junit:junit:4.12' 

    annotationProcessor 'android.arch.lifecycle:compiler:1.0.0' 
    compile 'android.arch.persistence.room:rxjava2:1.0.0-rc1' 
    testCompile'android.arch.persistence.room:testing:1.0.0-rc1' 

} 

Tôi thực sự không biết triển khai này nên là gì, tôi đã tìm kiếm một giải pháp n nhưng tất cả những gì tôi thấy không hiệu quả với tôi. Ví dụ, nhiều người tìm thấy một giải pháp thêm kapt "android.arch.persistence.room ..." nhưng gradle không nhận ra "kapt".

Trả lời

8

kapt dành cho Kotlin.

Đầu tiên, thêm:

annotationProcessor "android.arch.persistence.room:compiler:1.0.0" 

để dependencies đóng cửa của bạn.

Sau đó, nâng cấp android.arch.persistence.room:rxjava2android.arch.persistence.room:testing tới 1.0.0 thay vì 1.0.0-rc1.

Các vấn đề liên quan