12. Singleton - Threadsafe
@Singleton
public class Astroboy {
@Inject Application application;
@Inject Vibrator vibrator;
@Inject Random random;
public void say(final String something) {
// Make a Toast, using the current context as returned by the Context Provider
Toast.makeText(application, "Astroboy says, "" + something + """, Toast.LENGTH_LONG).show();
}
public void brushTeeth() {
vibrator.vibrate(new long[]{0, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, }, -1);
}
public String punch() {
final String expletives[] = new String[]{"POW!", "BANG!", "KERPOW!", "OOF!"};
return expletives[random.nextInt(expletives.length)];
}
}
@ COPYRIGHT 2013 NUBOAT IN WONDERLAND
13. Test Random value
@Singleton
public class Astroboy {
public String punch() {
final String expletives[] =
new String[]{"POW!", "BANG!", "KERPOW!", "OOF!"};
return expletives[random.nextInt(expletives.length)];
}
...
}
@ COPYRIGHT 2013 NUBOAT IN WONDERLAND
14. Test Code 1
@RunWith(RobolectricTestRunner.class)
public class Astroboy1Test {
protected Context context = new RoboActivity();
protected Astroboy astroboy = RoboGuice.getInjector(context).getInstance(Astroboy.class);
@Test
public void stringShouldEndInExclamationMark() {
assertTrue(astroboy.punch().endsWith("!"));
}
}
@ COPYRIGHT 2013 NUBOAT IN WONDERLAND
15. Test Vibrator
@Singleton
public class Astroboy {
public void brushTeeth() {
vibrator.vibrate(
new long[]{0, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200,
50, 200, 50, 200, 50, 200, 50, }, -1);
}
...
}
@ COPYRIGHT 2013 NUBOAT IN WONDERLAND
16. Test Code 2
public class Astroboy2Test {
...
@Test
public void brushingTeethShouldCausePhoneToVibrate() {
// get the astroboy instance
final Astroboy astroboy = RoboGuice.getInjector(context).getInstance(Astroboy.class);
// do the thing
astroboy.brushTeeth();
// verify that by doing the thing, vibratorMock.vibrate was called
verify(vibratorMock).vibrate(new long[]{0, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50},-1);
}
}
@ COPYRIGHT 2013 NUBOAT IN WONDERLAND
17. Test Code 2
public class Astroboy2Test {
protected Application application = mock(Application.class, RETURNS_DEEP_STUBS);
protected Context context = mock(RoboActivity.class, RETURNS_DEEP_STUBS);
protected Vibrator vibratorMock = mock(Vibrator.class);
@Before
public void setup() {
// Override the default RoboGuice module
RoboGuice.setBaseApplicationInjector(application, RoboGuice.DEFAULT_STAGE,
Modules.override(RoboGuice.newDefaultRoboModule(application)).with(new MyTestModule()));
when(context.getApplicationContext()).thenReturn(application);
when(application.getApplicationContext()).thenReturn(application);
}
...
}
@ COPYRIGHT 2013 NUBOAT IN WONDERLAND
18. Test Code 2
public class Astroboy2Test {
...
public class MyTestModule extends AbstractModule {
@Override
protected void configure() {
bind(Vibrator.class).toInstance(vibratorMock);
}
}
}
@ COPYRIGHT 2013 NUBOAT IN WONDERLAND
19. Test Code 2
public class Astroboy2Test {
...
@After
public void teardown() {
// Don't forget to tear down our custom injector to avoid polluting other test classes
RoboGuice.util.reset();
}
}
@ COPYRIGHT 2013 NUBOAT IN WONDERLAND
20. Log Framework
Log.d("TAG", "Sent say(" + something + ") command to Astroboy");
VS
Ln.d("Sent say(%s) command to Astroboy", something);
@ COPYRIGHT 2013 NUBOAT IN WONDERLAND
21. Log Framework
public static int d(Object s1, Object[] args) {
...
}
Ln.d("Sent say(%s) command to Astroboy %s", something, “1”);
**it will automatically not log on a signed APK
@ COPYRIGHT 2013 NUBOAT IN WONDERLAND