Last active
April 21, 2021 06:06
-
-
Save jturolla/25489e1b676957197201 to your computer and use it in GitHub Desktop.
This gist provides a simple example of mocking a Realm database for android in java using Robolectric.
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
package com.example; | |
import android.content.Intent; | |
import android.widget.Button; | |
import com.example.MockSupport; | |
import org.junit.Before; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.powermock.core.classloader.annotations.PowerMockIgnore; | |
import org.powermock.core.classloader.annotations.PrepareForTest; | |
import org.powermock.modules.junit4.rule.PowerMockRule; | |
import org.robolectric.Robolectric; | |
import org.robolectric.RobolectricGradleTestRunner; | |
import org.robolectric.Shadows; | |
import org.robolectric.annotation.Config; | |
import org.robolectric.shadows.ShadowActivity; | |
import org.robolectric.util.ActivityController; | |
import io.realm.Realm; | |
import static org.junit.Assert.*; | |
@RunWith(RobolectricGradleTestRunner.class) | |
@Config(constants = BuildConfig.class, sdk = 21) | |
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"}) | |
@PrepareForTest({Realm.class}) | |
public class ActivityTest { | |
Realm mockRealm; | |
@Rule | |
public PowerMockRule rule = new PowerMockRule(); | |
@Before | |
public void setup() { | |
mockRealm = MockSupport.mockRealm(); | |
} | |
@Test | |
public void realmShouldBeMocked() { | |
assertThat(Realm.getDefaultInstance(), is(mockRealm)); | |
} | |
} |
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
// Other stuff... | |
dependencies { | |
testCompile 'junit:junit:4.12' | |
testCompile "org.robolectric:robolectric:3.0" | |
testCompile "org.mockito:mockito-core:1.10.19" | |
testCompile 'org.robolectric:shadows-support-v4:3.0' | |
testCompile "org.powermock:powermock-module-junit4:1.6.2" | |
testCompile "org.powermock:powermock-module-junit4-rule:1.6.2" | |
testCompile "org.powermock:powermock-api-mockito:1.6.2" | |
testCompile "org.powermock:powermock-classloading-xstream:1.6.2" | |
} |
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
package com.example; | |
import com.example.model.Address; | |
import com.example.model.Job; | |
import com.example.model.Offer; | |
import com.example.model.Tasker; | |
import org.powermock.api.mockito.PowerMockito; | |
import io.realm.Realm; | |
import static org.powermock.api.mockito.PowerMockito.mockStatic; | |
import static org.powermock.api.mockito.PowerMockito.when; | |
public class MockSupport { | |
public static Realm mockRealm() { | |
mockStatic(Realm.class); | |
Realm mockRealm = PowerMockito.mock(Realm.class); | |
when(mockRealm.createObject(Address.class)).thenReturn(new Address()); | |
when(mockRealm.createObject(Offer.class)).thenReturn(new Offer()); | |
when(mockRealm.createObject(Tasker.class)).thenReturn(new Tasker()); | |
when(mockRealm.createObject(Job.class)).thenReturn(new Job()); | |
when(Realm.getDefaultInstance()).thenReturn(mockRealm); | |
return mockRealm; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As stated in powermock/powermock#819, it's time to replace
powermock-api-mockito
withpowermock-api-mockito2