Room은 Android 프레임워크 클래스의 인터페이스와 일치하는 인터페이스를 제공하는 SQLite 지원 라이브러리를 사용합니다. 이 지원을 통해 지원 라이브러리의 맞춤 구현을 전달하여 데이터베이스 쿼리를 테스트할 수 있습니다.
이전 테스트
Room에서는 앱 업데이트로 데이터베이스 스키마가 변경되는 상황에서 기존 앱 데이터를 유지할 수 있도록 증분 데이터베이스 이전을 지원합니다.
그러나 잘못 정의된 이전으로 인해 앱이 비정상 종료될 수 있습니다. Room 데이터베이스 이전을 테스트해야 합니다.
데이터베이스 디버그
데이터베이스를 디버그하는 데 사용할 수 있는 여러 도구와 프로세스가 있습니다.
Database Inspector 사용
Android 스튜디오 4.1 이상에서는 Database Inspector를 사용하여 앱 실행 중에 앱의 데이터베이스를 검사하고 쿼리하거나 수정할 수 있습니다. Database Inspector는 Android와 함께 번들로 제공되는 SQLite 버전과 호환되며, Room에서 사용할 수 있는 다음과 같은 특수 기능이 포함되어 있습니다.
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-07-27(UTC)"],[],[],null,["# Test and debug your database\n\nIt's important to verify the stability of your app's database and your users'\ndata when creating databases using the\n[Room persistence library](/training/data-storage/room). This page\ndiscusses how to test your database and perform debugging steps to help your\ntests pass.\n\nTest your database\n------------------\n\nThere are 2 ways to test your database:\n\n- On an Android device.\n- On your host development machine (not recommended).\n\nFor information about testing that's specific to database migrations, see\n[Testing Migrations](/training/data-storage/room/migrating-db-versions#test).\n| **Note:** When running tests for your app, Room allows you to create mock instances of your [DAO](/training/data-storage/room/accessing-data) classes. That way, you don't need to create a full database if you aren't testing the database itself. This functionality is possible because your DAOs don't leak any details of your database.\n\n### Test on an Android device\n\nThe recommended approach for testing your database implementation is writing a\nJUnit test that runs on an Android device. Because these tests don't require\ncreating an activity, they should be faster to execute than your UI tests.\n\nWhen setting up your tests, you should create an in-memory version of your\ndatabase to make your tests more hermetic, as shown in the following example: \n\n### Kotlin\n\n```kotlin\n@RunWith(AndroidJUnit4::class)\nclass SimpleEntityReadWriteTest {\n private lateinit var userDao: UserDao\n private lateinit var db: TestDatabase\n\n @Before\n fun createDb() {\n val context = ApplicationProvider.getApplicationContext\u003cContext\u003e()\n db = Room.inMemoryDatabaseBuilder(\n context, TestDatabase::class.java).build()\n userDao = db.getUserDao()\n }\n\n @After\n @Throws(IOException::class)\n fun closeDb() {\n db.close()\n }\n\n @Test\n @Throws(Exception::class)\n fun writeUserAndReadInList() {\n val user: User = TestUtil.createUser(3).apply {\n setName(\"george\")\n }\n userDao.insert(user)\n val byName = userDao.findUsersByName(\"george\")\n assertThat(byName.get(0), equalTo(user))\n }\n}\n```\n\n### Java\n\n```java\n@RunWith(AndroidJUnit4.class)\npublic class SimpleEntityReadWriteTest {\n private UserDao userDao;\n private TestDatabase db;\n\n @Before\n public void createDb() {\n Context context = ApplicationProvider.getApplicationContext();\n db = Room.inMemoryDatabaseBuilder(context, TestDatabase.class).build();\n userDao = db.getUserDao();\n }\n\n @After\n public void closeDb() throws IOException {\n db.close();\n }\n\n @Test\n public void writeUserAndReadInList() throws Exception {\n User user = TestUtil.createUser(3);\n user.setName(\"george\");\n userDao.insert(user);\n List\u003cUser\u003e byName = userDao.findUsersByName(\"george\");\n assertThat(byName.get(0), equalTo(user));\n }\n}\n```\n\n### Test on your host machine\n\nRoom uses the SQLite Support Library, which provides interfaces that match those\nin the Android Framework classes. This support allows you to pass custom\nimplementations of the support library to test your database queries.\n| **Note:** Even though this setup allows your tests to run very quickly, it isn't recommended because the version of SQLite running on your device---and your users' devices---might not match the version on your host machine.\n\n### Test your migrations\n\nRoom supports [incremental database\nmigrations](/training/data-storage/room/migrating-db-versions) to retain\nexisting app data in situations where an app update changes the database schema.\nHowever, an incorrectly defined migration could cause your app to crash. Make\nsure that you [test your Room database\nmigrations](/training/data-storage/room/migrating-db-versions#test).\n\nDebug your database\n-------------------\n\nThere are several tools and processes that you can use to debug your database.\n\n### Use the Database Inspector\n\nIn Android Studio 4.1 and higher, the Database Inspector allows you to inspect,\nquery, and modify your app's databases while your app is running. The Database\nInspector is compatible with the version of SQLite that is bundled with Android\nand includes special features for use with Room:\n\n- Use gutter actions to quickly run queries from your [DAO\n classes](/training/data-storage/room/accessing-data).\n- Immediately see live updates in the Database Inspector when your running app makes changes to the data.\n\nTo learn more about the Database Inspector, see [Debug your database with the\nDatabase Inspector](/studio/inspect/database).\n\n### Dump data from the command line\n\nThe Android SDK includes a `sqlite3` database tool for examining your app's\ndatabases. It includes commands such as `.dump` to print the contents of a\ntable, and `.schema` to print the `SQL CREATE` statement for an existing table.\n\nYou can also execute SQLite commands from the command line, as shown in the\nfollowing snippet: \n\n```bash\nadb -s emulator-5554 shell\nsqlite3 /data/data/your-app-package/databases/rssitems.db\n```\n\nFor more information, see the [`sqlite3` command line\ndocumentation](http://www.sqlite.org/cli.html), available on the\nSQLite website.\n\nAdditional resources\n--------------------\n\nTo learn more about testing and debugging your Room database, see the following\nadditional resources:\n\n### Blog posts\n\n- [Database Inspector: A live database tool we've been waiting\n for!](https://medium.com/androiddevelopers/database-inspector-9e91aa265316)\n\n### Videos\n\n- [Database Inspector](https://www.youtube.com/watch?v=UMc7Tu0nKYQ) (11 Weeks of Android)"]]