• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Roboguice
 

Roboguice

on

  • 669 views

Dependency Injection & Inversion of Control for Android with RoboGuice.

Dependency Injection & Inversion of Control for Android with RoboGuice.

How to unit test Android project.

Statistics

Views

Total Views
669
Views on SlideShare
669
Embed Views
0

Actions

Likes
2
Downloads
5
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Roboguice Roboguice Presentation Transcript

    • RoboGuice DI & IoC framework for Android
    • Me Software Engineer @ FICO Corp. Programmer @nuboat https://github.com/nuboat http://slideshare.net/nuboat/ @ COPYRIGHT 2013 NUBOAT IN WONDERLAND
    • RoboGuice RoboGuice เป็ น framework สําหรั บทํา Dependency Injection บน Android, พั ฒนาจาก Google Guice library. ลั กษณะคล้ายกั บ Spring & EJB ของ JavaEE @ COPYRIGHT 2013 NUBOAT IN WONDERLAND
    • Normal Style @ COPYRIGHT 2013 NUBOAT IN WONDERLAND
    • RoboGuice Style @ COPYRIGHT 2013 NUBOAT IN WONDERLAND
    • Requirements Java 6 or 7 Maven Android SDK (platform 18) Coffee & Beer (up to you) @ COPYRIGHT 2013 NUBOAT IN WONDERLAND
    • Librarys RoboGuice v3.0b-experimental Google Guice v3.0 Roboelectric v1.0 Mockito v1.9.5 JUnit v4.8.2 @ COPYRIGHT 2013 NUBOAT IN WONDERLAND
    • Advantage of RoboGuice Clean code Inherit Guice feature (Singleton, …) Automate Testing Log Framework @ COPYRIGHT 2013 NUBOAT IN WONDERLAND
    • Clean Code RoboActivity not Activity @ COPYRIGHT 2013 NUBOAT IN WONDERLAND
    • Inheriting from the RoboGuice RoboActivity RoboFragmentActivity RoboListActivity RoboLauncherActivity RoboExpandableListActivity RoboService RoboMapActivity RoboIntentService RoboPreferenceActivity RoboFragment RoboAccountAuthenticatorActivity RoboListFragment RoboActivityGroup RoboDialogFragment RoboTabActivity etc. @ COPYRIGHT 2013 NUBOAT IN WONDERLAND
    • Inject ? roboguice.inject.InjectView roboguice.inject.InjectExtra roboguice.inject.InjectFragment roboguice.inject.InjectPreference roboguice.inject.InjectResource javax.inject.Inject (POJO) @ COPYRIGHT 2013 NUBOAT IN WONDERLAND
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • Short Workshop Config Build Test Deploy Run Debug
    • Maven - pom.xml #1 <properties> <android.sdk.path> C:Program Filesadt-bundle-windowssdk </android.sdk.path> </properties> @ COPYRIGHT 2013 NUBOAT IN WONDERLAND
    • Maven - pom.xml #1 <!-- REGULAR DEPENDENCIES --> <dependency> <groupId>com.google.inject</groupId> <artifactId>guice</artifactId> <version>3.0</version> <classifier>no_aop</classifier> </dependency> <dependency> <groupId>org.roboguice</groupId> <artifactId>roboguice</artifactId> <version>3.0b-experimental</version> </dependency> @ COPYRIGHT 2013 NUBOAT IN WONDERLAND
    • Maven - pom.xml #2 <!-- TEST DEPENDENCIES --> <dependency> <groupId>com.pivotallabs</groupId> <artifactId>robolectric</artifactId> <version>1.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>1.9.5</version> <scope>test</scope> </dependency> @ COPYRIGHT 2013 NUBOAT IN WONDERLAND
    • Maven - pom.xml #3 <!-- PROVIDED DEPENDENCIES --> <dependency> <groupId>com.google.android</groupId> <artifactId>android</artifactId> <version>4.2.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>provided</scope> </dependency> <dependency> <!-- needed to prevent warnings in robolectric tests --> <groupId>com.google.android.maps</groupId> <artifactId>maps</artifactId> <version>7_r1</version> <scope>provided</scope> <optional>true</optional> </dependency> @ COPYRIGHT 2013 NUBOAT IN WONDERLAND
    • Maven - pom.xml #4 <plugin> <groupId>com.jayway.maven.plugins.android.generation2</groupId> <artifactId>android-maven-plugin</artifactId> <version>3.7.0</version> <configuration> <androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile> <assetsDirectory>${project.basedir}/assets</assetsDirectory> <resourceDirectory>${project.basedir}/res</resourceDirectory> <nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory> <sdk> <platform>18</platform> </sdk> <undeployBeforeDeploy>true</undeployBeforeDeploy> </configuration> <extensions>true</extensions> </plugin> @ COPYRIGHT 2013 NUBOAT IN WONDERLAND
    • Maven - Command mvn -DskipTests=true install mvn test mvn -DskipTests=true android:deploy mvn -DskipTests=true android:run -Dandroid.run.debug=true @ COPYRIGHT 2013 NUBOAT IN WONDERLAND
    • NETBEANS @ COPYRIGHT 2013 NUBOAT IN WONDERLAND
    • Who uses RoboGuice? @ COPYRIGHT 2013 NUBOAT IN WONDERLAND
    • QUESTION THANK YOU @ COPYRIGHT 2013 NUBOAT IN WONDERLAND