Created
March 13, 2016 20:05
-
-
Save hkurokawa/55f9b98fb34ff0bf055b to your computer and use it in GitHub Desktop.
PowerMock + 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.hkurokawa.powermocktest; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.os.Bundle; | |
| import android.util.Log; | |
| import rx.android.schedulers.AndroidSchedulers; | |
| import rx.functions.Action1; | |
| import rx.schedulers.Schedulers; | |
| public class MainActivity extends AppCompatActivity { | |
| private static final String TAG = "MainActivity"; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| MyStaticClass.getObservable() | |
| .subscribeOn(Schedulers.io()) | |
| .observeOn(AndroidSchedulers.mainThread()) | |
| .subscribe(new Action1<Void>() { | |
| @Override | |
| public void call(Void aVoid) { | |
| startActivity(AnotherActivity.newInstance(MainActivity.this)); | |
| } | |
| }, new Action1<Throwable>() { | |
| @Override | |
| public void call(Throwable throwable) { | |
| Log.e(TAG, "Failed: " + throwable.getMessage(), throwable); | |
| } | |
| }); | |
| } | |
| } |
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.hkurokawa.powermocktest; | |
| import android.content.Intent; | |
| import org.junit.Rule; | |
| import org.junit.Test; | |
| import org.junit.runner.RunWith; | |
| import org.powermock.api.mockito.PowerMockito; | |
| 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 rx.subjects.PublishSubject; | |
| import static org.junit.Assert.assertEquals; | |
| import static org.junit.Assert.assertNotNull; | |
| @RunWith(RobolectricGradleTestRunner.class) | |
| @Config(constants = BuildConfig.class, sdk = 21) | |
| @PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"}) | |
| @PrepareForTest({MyStaticClass.class}) | |
| public class MainActivityTest { | |
| // Robolectric, Using Power Mock https://github.com/robolectric/robolectric/wiki/Using-PowerMock | |
| @Rule | |
| public PowerMockRule rule = new PowerMockRule(); | |
| @Test | |
| public void test() { | |
| PowerMockito.mockStatic(MyStaticClass.class); | |
| final PublishSubject<Void> dummyObservable = PublishSubject.create(); | |
| PowerMockito.when(MyStaticClass.getObservable()).thenReturn(dummyObservable.asObservable()); | |
| final MainActivity activity = Robolectric.setupActivity(MainActivity.class); | |
| final ShadowActivity shadowOfActivity = Shadows.shadowOf(activity); | |
| dummyObservable.onNext(null); | |
| final Intent intent = shadowOfActivity.getNextStartedActivity(); | |
| assertNotNull(intent); | |
| assertEquals(intent.getComponent().getClassName(), AnotherActivity.class.getName()); | |
| } | |
| } |
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.hkurokawa.powermocktest; | |
| import rx.Observable; | |
| class MyStaticClass { | |
| static Observable<Void> getObservable() { | |
| return null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment