-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracted the duplicate test Realm setup to a test rule.
- Loading branch information
Showing
4 changed files
with
54 additions
and
36 deletions.
There are no files selected for viewing
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
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
44 changes: 44 additions & 0 deletions
44
app/src/androidTest/java/com/codemate/koffeemate/data/local/RealmTestRule.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.codemate.koffeemate.data.local | ||
|
||
import io.realm.Realm | ||
import io.realm.RealmConfiguration | ||
import org.junit.rules.ExternalResource | ||
import org.junit.runner.Description | ||
import org.junit.runners.model.Statement | ||
|
||
/** | ||
* A JUnit Rule that sets up a test Realm database before each test, | ||
* clears all data from it, and deletes the whole database file alltogether | ||
* after the test completes. | ||
* | ||
* Usage: | ||
* | ||
* @Rule @JvmField | ||
* val realmRule: RealmTestRule = RealmTestRule() | ||
* | ||
* The above assumes that your application logic that uses Realm always uses | ||
* the default instance. Meaning that your code calls Realm.getDefaultInstance() | ||
* and uses that for all database logic. | ||
*/ | ||
class RealmTestRule : ExternalResource() { | ||
val testConfig: RealmConfiguration = RealmConfiguration.Builder() | ||
.name("test.realm") | ||
.build() | ||
|
||
override fun before() { | ||
Realm.setDefaultConfiguration(testConfig) | ||
|
||
with (Realm.getDefaultInstance()) { | ||
executeTransaction(Realm::deleteAll) | ||
close() | ||
} | ||
} | ||
|
||
override fun apply(base: Statement?, description: Description?): Statement { | ||
return super.apply(base, description) | ||
} | ||
|
||
override fun after() { | ||
Realm.deleteRealm(testConfig) | ||
} | ||
} |
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