roboguice

31
RoboGuice DI & IoC framework for Android

Upload: peerapat-asoktummarungsri

Post on 10-May-2015

717 views

Category:

Technology


2 download

DESCRIPTION

Dependency Injection & Inversion of Control for Android with RoboGuice. How to unit test Android project.

TRANSCRIPT

Page 1: Roboguice

RoboGuiceDI & IoC framework for Android

Page 2: Roboguice

Me

Software Engineer @ FICO Corp.Programmer@nuboathttps://github.com/nuboathttp://slideshare.net/nuboat/

@ COPYRIGHT 2013 NUBOAT IN WONDERLAND

Page 3: Roboguice

RoboGuice

RoboGuice เป็น framework สาํหรับทาํ Dependency Injection บน Android, พัฒนาจาก Google Guice library. ลักษณะคลา้ยกับ Spring & EJB ของ JavaEE

@ COPYRIGHT 2013 NUBOAT IN WONDERLAND

Page 4: Roboguice

Normal Style

@ COPYRIGHT 2013 NUBOAT IN WONDERLAND

Page 5: Roboguice

RoboGuice Style

@ COPYRIGHT 2013 NUBOAT IN WONDERLAND

Page 6: Roboguice

Requirements

Java 6 or 7MavenAndroid SDK (platform 18)Coffee & Beer (up to you)

@ COPYRIGHT 2013 NUBOAT IN WONDERLAND

Page 7: Roboguice

Librarys

RoboGuice v3.0b-experimentalGoogle Guice v3.0Roboelectric v1.0Mockito v1.9.5JUnit v4.8.2

@ COPYRIGHT 2013 NUBOAT IN WONDERLAND

Page 8: Roboguice

Advantage of RoboGuice

Clean codeInherit Guice feature (Singleton, …)Automate TestingLog Framework

@ COPYRIGHT 2013 NUBOAT IN WONDERLAND

Page 9: Roboguice

Clean Code

RoboActivity not Activity

@ COPYRIGHT 2013 NUBOAT IN WONDERLAND

Page 10: Roboguice

Inheriting from the

RoboGuice

Inheriting from the

RoboGuice

Inheriting from the

RoboGuice

Inheriting from the

RoboGuice

Inheriting from the RoboGuice

RoboActivity

RoboListActivity

RoboExpandableListActivity

RoboMapActivity

RoboPreferenceActivity

RoboAccountAuthenticatorActivity

RoboActivityGroup

RoboTabActivity

RoboFragmentActivity

RoboLauncherActivity

RoboService

RoboIntentService

RoboFragment

RoboListFragment

RoboDialogFragment

etc.

@ COPYRIGHT 2013 NUBOAT IN WONDERLAND

Page 11: Roboguice

Inject ?

roboguice.inject.InjectViewroboguice.inject.InjectExtraroboguice.inject.InjectFragmentroboguice.inject.InjectPreferenceroboguice.inject.InjectResourcejavax.inject.Inject (POJO)

@ COPYRIGHT 2013 NUBOAT IN WONDERLAND

Page 12: Roboguice

@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)];

}

}

Singleton - Threadsafe

@ COPYRIGHT 2013 NUBOAT IN WONDERLAND

Page 13: Roboguice

Test Random value

@Singletonpublic 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

Page 14: Roboguice

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

Page 15: Roboguice

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

Page 16: Roboguice

Test Code 2public 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

Page 17: Roboguice

Test Code 2public 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

Page 18: Roboguice

Test Code 2public class Astroboy2Test {

. . .

public class MyTestModule extends AbstractModule {

@Override

protected void configure() {

bind(Vibrator.class).toInstance(vibratorMock);

}

}

}

@ COPYRIGHT 2013 NUBOAT IN WONDERLAND

Page 19: Roboguice

Test Code 2public 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

Page 20: Roboguice

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

Page 21: Roboguice

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

Page 22: Roboguice

Short Workshop

ConfigBuildTestDeployRunDebug

Page 23: Roboguice

Maven - pom.xml #1

<properties>

<android.sdk.path>

C:\Program Files\adt-bundle-windows\sdk

</android.sdk.path>

</properties>

@ COPYRIGHT 2013 NUBOAT IN WONDERLAND

Page 24: Roboguice

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

Page 25: Roboguice

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

Page 26: Roboguice

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

Page 27: Roboguice

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

Page 28: Roboguice

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

Page 29: Roboguice

NETBEANS

@ COPYRIGHT 2013 NUBOAT IN WONDERLAND

Page 30: Roboguice

Who uses RoboGuice?

@ COPYRIGHT 2013 NUBOAT IN WONDERLAND

Page 31: Roboguice

THANK YOU

QUESTION

@ COPYRIGHT 2013 NUBOAT IN WONDERLAND