Created
August 21, 2017 11:41
-
-
Save ppamorim/4b2a360d9ce38e969126679c35773ea1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import android.content.Context | |
import io.realm.* | |
import io.realm.internal.RealmCore | |
import io.realm.log.RealmLog | |
import org.junit.Before | |
import org.junit.Rule | |
import org.junit.Test | |
import org.junit.runner.RunWith | |
import org.mockito.Mockito.`when` | |
import org.mockito.Matchers.`any` | |
import org.powermock.core.classloader.annotations.PowerMockIgnore | |
import org.powermock.core.classloader.annotations.PrepareForTest | |
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor | |
import org.powermock.modules.junit4.PowerMockRunner | |
import org.powermock.modules.junit4.rule.PowerMockRule | |
import org.powermock.api.mockito.PowerMockito.mockStatic | |
import org.robolectric.RuntimeEnvironment | |
import org.robolectric.annotation.Config | |
import io.realm.RealmConfiguration | |
import org.powermock.api.mockito.PowerMockito.doNothing | |
import org.powermock.api.mockito.PowerMockito.mock | |
import org.powermock.api.mockito.PowerMockito.whenNew | |
/** | |
* Check https://github.com/realm/realm-java/blob/master/examples/unitTestExample/src/test/java/io/realm/examples/unittesting/ExampleActivityTest.java | |
*/ | |
@RunWith(PowerMockRunner::class) | |
@Config(constants = BuildConfig::class, sdk = intArrayOf(21)) | |
@PowerMockIgnore("org.mockito.*", "org.robolectric.*", "android.*") | |
@SuppressStaticInitializationFor("io.realm.internal.Util") | |
@PrepareForTest(Realm::class, RealmConfiguration::class, RealmQuery::class, RealmResults::class, RealmCore::class, RealmLog::class) | |
class TestRepositoryTest { | |
@JvmField @Rule public var rule = PowerMockRule() | |
lateinit private var mockRealm: Realm | |
@Before fun setup() { | |
mockStatic(RealmCore::class.java) | |
mockStatic(RealmLog::class.java) | |
mockStatic(Realm::class.java) | |
mockStatic(RealmConfiguration::class.java) | |
Realm.init(RuntimeEnvironment.application) | |
// Create the mock | |
val mockRealm = mock(Realm::class.java) | |
val mockRealmConfig = mock(RealmConfiguration::class.java) | |
// TODO: Better solution would be just mock the RealmConfiguration.Builder class. But it seems there is some | |
// problems for powermock to mock it (static inner class). We just mock the RealmCore.loadLibrary(Context) which | |
// will be called by RealmConfiguration.Builder's constructor. | |
doNothing().`when`(RealmCore::class.java) | |
RealmCore.loadLibrary(`any`(Context::class.java)) | |
// TODO: Mock the RealmConfiguration's constructor. If the RealmConfiguration.Builder.build can be mocked, this | |
// is not necessary anymore. | |
whenNew(RealmConfiguration::class.java).withAnyArguments().thenReturn(mockRealmConfig) | |
// Anytime getInstance is called with any configuration, then return the mockRealm | |
`when`(Realm.getDefaultInstance()).thenReturn(mockRealm) | |
this.mockRealm = mockRealm | |
} | |
//Stubbing | |
@Test fun test() {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment