forked from robolectric/robolectric
-
Notifications
You must be signed in to change notification settings - Fork 0
Write simple service test on robolectric
tyanhly edited this page Apr 18, 2013
·
1 revision
See http://marakana.com/forums/android/examples/60.html for this example Code
package com.example.a7; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; import org.robolectric.shadows.ShadowActivity; import org.robolectric.shadows.ShadowIntent;import android.content.Intent; import android.widget.Button;
@RunWith(RobolectricTestRunner.class) public class MainActivityTest { MainActivity mainActivity; Button btnStart; Button btnStop; @Before public void setup(){ mainActivity = new MainActivity(); mainActivity.onCreate(null); btnStart = (Button) mainActivity.findViewById(R.id.buttonStart); btnStop = (Button) mainActivity.findViewById(R.id.buttonStop); }
@Test public void testStart(){ btnStart.performClick(); ShadowActivity sa = Robolectric.shadowOf(mainActivity); Intent intent = sa.getNextStartedService(); ShadowIntent si = Robolectric.shadowOf(intent); Assert.assertSame(si.getComponent().getClassName(), MyService.class.getName()); } @Test public void testStop(){ btnStart.performClick(); btnStop.performClick(); ShadowActivity sa = Robolectric.shadowOf(mainActivity); Intent intent = sa.getNextStoppedService(); ShadowIntent si = Robolectric.shadowOf(intent); Assert.assertEquals(si.getComponent().getClassName(), MyService.class.getName()); }
}