代码之家  ›  专栏  ›  技术社区  ›  Cyrus the Great

Dagger2的AndroidUnit测试室

  •  0
  • Cyrus the Great  · 技术社区  · 6 年前

    我已经为房间数据库创建了dagger2模块,如下所示:

    @Module
    public class RoomModule {
    
        @AppScope
        @Provides
        StateDataBase dbEngineerProvider(Context context){
            return Room.databaseBuilder(context, StateDataBase.class, "State.db").build();
        }
    
        @AppScope
        @Provides
        UserDao getUserDao(StateDataBase db) {
            return db.getUserDao();
        }
    }
    

    我在应用程序类中初始化了匕首:

        @Override
        public void onCreate() {
            super.onCreate();
            Timber.plant(new Timber.DebugTree());
    
            component = DaggerAppComponent.builder()
                    .networkModule(new NetworkModule(this))
                    .build();
        }
    
        public AppComponent getAppComponent() {
            return component;
        }
    }
    

    我想知道怎样才能从我房间的数据库中进行真正的测试而不是模拟测试?

    我想在我的数据库中插入一个数据,在我的应用程序中我使用这个数据!!!! 例如,在测试中,我插入我的用户的用户名和密码,然后在运行应用程序时,我使用这些数据登录到应用程序!!有可能吗?

    0 回复  |  直到 6 年前
        1
  •  0
  •   Pallavi Tapkir    6 年前
    @Test
        fun testRoomDatabase() {
            val dao = Room.databaseBuilder(context!!, Database::class.java, "Database.db").build().dao()
    
            val alertList = ArrayList<Alert>()
    
            val alert = Alert()
            alert.alertId = 22603
            alert.alertType = 3
            alert.geoFence = ""
    
            val alert1 = Alert()
            alert1.alertId = 12603
            alert1.alertType = 10
            alert1.geoFence = ""
    
            alertList.add(alert)
            alertList.add(alert1)
    
            Assert.assertNotNull(alertList)
            val ids = dao.insertSelectedAlertsToDatabase(alertList)
            Assert.assertEquals(2, ids.size)
            Assert.assertFalse(1 == ids.size)
    
            // fetch all alerts from database
            val alerts = dao.fetchAllAlerts()/*.test().onComplete()*/
            Assert.assertNotNull(alerts)
        }
    

    我希望你有合适的数据库。