Created
April 28, 2023 11:33
-
-
Save nisaefendioglu/c71f3efe70203dcf1dde23ec9ea64555 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(AndroidJUnit4ClassRunner::class) | |
class UserDaoTest { | |
private lateinit var db: TestUserDatabase | |
private lateinit var userDao: UserDao | |
@Before | |
fun createDb() { | |
val context = InstrumentationRegistry.getInstrumentation().targetContext | |
db = Room.inMemoryDatabaseBuilder(context, TestUserDatabase::class.java).build() | |
userDao = db.userDao() | |
} | |
@After | |
fun closeDb() { | |
db.close() | |
} | |
@Test | |
@Throws(Exception::class) | |
fun insertAndGetUser() { | |
val user = User("John", "Doe") | |
userDao.insert(user) | |
val savedUser = userDao.getUserById(1) | |
assertEquals(user.firstName, savedUser.firstName) | |
assertEquals(user.lastName, savedUser.lastName) | |
} | |
@Test | |
@Throws(Exception::class) | |
fun deleteUser() { | |
val user = User("John", "Doe") | |
userDao.insert(user) | |
userDao.delete(user) | |
val savedUser = userDao.getUserById(1) | |
assertNull(savedUser) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment