Androidã®ãã¹ããã¼ã«ãªãããï¼Robolectricå°å ¥ã®åã
ãã¤ã¦ã¯Androidä¾åã®ã¢ã¸ã¥ã¼ã«ã®ãã¹ãã¯ãé ãããPOJOã¨ãã¾ãåé¢ãã¦ãªãã¹ãPOJOãé常ã®Unitãã¹ãã§ã»ã»ã»ã¨ãæã£ã¦ããææããã£ããã ãã©ããã®åé¢ã£ã¦ãã¤ãç¸å½ãããã¦ãã³ã«ã¹ããã°ã©ãã ããã
ã¤ã¾ããã¹ãæ¸ãã¦ãªãã£ããã ãã©ãããã¯ã¾ã Androidéçºã®ããªãããã£ã¦ãªãã£ããã¨ããã£ãããéçºå¯¾è±¡ãç»é¢ã®é·ç§»å¨ããã¡ã¤ã³ã§ãã¾ãå°é£ããå¦çããªãã£ããã§ããã¹ãã«ããã¡ãªãããããã¡ãªãããä¸åãå¯è½æ§ããã£ãããã§ã
ä»ã¯ããªãããã£ã¦ããããç´°ããå®è£ ããã£ã¦ããã«ããããªãã§ããä¸åº¦ãã¹ãã«ã¤ãã¦ãã調ã¹ã¦ã¿ãã®ãã
ããããAndroidã¢ã¸ã¥ã¼ã«ã絡ããã¹ããé ãã®ã¯
- Googleãandroid.jarã«ããå ¨ã¡ã½ããã"throw new RuntimeException("Stub!");"ã«å¤ãã¦ãã
- ãã®ãããã¨ãã¥ã¬ã¼ã¿ãªãå®æ©ãªãã§åãããªãã¨ãããªãï¼ã¢ãã¯ã«å·®ãæ¿ããåã«ä¾å¤ãé£ã¶ã®ã§Mockingããã¼ãã¼ï¼
- ã¨ãããã¨ã¯Dexï¼ç¢ºãDalvikVMã§ããããã¤ãã³ã¼ãã«ããå¦çï¼ããã¤ã³ã¹ãã¼ã«ãããå¿ è¦ãªã®ã§ãé ãã
ã¨ãããã¨ãªãã ãã©ããªãã¨ï¼é»éè¡ã«ã¦ãã®"throw new RuntimeException("Stub!");"ãå·®ãæ¿ãã¦ãããªã«ãã£ã¦ããããã¬ã¼ã ã¯ã¼ã¯ãããã¾ããï¼
Robolectric
ãã®ä¼ç¤¾ã®äººãã¡ã¯IntelliJã使ã£ã¦ã¾ãï¼ã¤ã§ã¼ï¼
ã§ãIntelliJã«éä¸ããå°å ¥ããæé ãå ¬å¼ã«æ¸ãã¦ãããã ãã©ããªããããããããªãæé ã ã£ãã®ã§ç¡è¦ãã¦åæã«ãã£ã¦ãã¾ããã£ãæ¹æ³ã以ä¸ã«ã
åæ
pomã«ã¡ããã£ã¨è¶³ã
<!-- TEST DEPENDENCIES --> <!-- Robolectric --> <dependency> <groupId>com.pivotallabs</groupId> <artifactId>robolectric</artifactId> <version>1.1</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency>
ããããªãã¹ãä¸ã«ãã£ã¦ãã£ã¦ã»ããã»ããããã§ããçç±ã¯å¾è¿°ã
ä¾åé¢ä¿ã®è¨å®ããã
Project Setting > Modules > ã¢ã㪠> Dependenciesã¿ã
ã§ããã¹ãé¢ä¿ã®ã¢ã¸ã¥ã¼ã«ãAndroidSDK(é»è²ãçã®ãã¤ï¼ããä¸ã«æã£ã¦ããããããããªãã¨"java.lang.RuntimeException: Stub!"ãã§ããã§ããã£ãpomã§ãã¹ãã¢ã¸ã¥ã¼ã«ã®è¨è¿°ããªãã¹ãä¸ã«ãã¨ãã話ãããã®ã¯ããã®ä¸ã«æã£ã¦ããä½æ¥ãã¡ãã£ã¨ã ã楽ã ãããã ãããã£ã±ãpomä¸ã§ã¯ä¸ã«æã£ã¦ãããªãã¦ãããã§ãã
Sourceã®è¨å®ããã
Project Setting > Modules > ã¢ã㪠> Sourcesã¿ã
ã§ãSource Foldersãsrc/main/javaãTest Source Foldersãsrc/test/javaã«ãªã£ã¦ããããã«ãããMavenã®ã¹ã¿ã³ãã¼ãã¹ãã©ã¯ãã£ã¼ã£ã¦ãã¤ããä»ã®ããã£ããããããããéªéã«ãªããããããªããããè¦ããªãã¨æã£ããæ¶ãã¦ããã
ãã¹ããä½ã
ã¨ããããé©å½ã«ã
import com.xtremelabs.robolectric.RobolectricTestRunner; import org.junit.Test; import org.junit.runner.RunWith; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.assertThat; /** * User: ria10 * Date: 2012/11/27 * Time: 16:35 */ @RunWith(RobolectricTestRunner.class) public class MyActivityTest { @Test public void shouldNotBeNull() throws Exception { MyActivity activity = new MyActivity(); assertThat(activity, is(notNullValue())); } }
ã¨ãããã¨ã§ã@RunWith(RobolectricTestRunner.class)ãæ¸ããã¨ãRobolectricã使ããã¨ãããã¨ã«ãªãã¾ãããã®è¶
ç°¡åãªãã¹ãã§ã¯ãã以å¤ã«Robolectricä¾åã®è¨è¿°ã¯ããã¾ããã
å®è¡
è¶ ããã¼ã«å®è¡ããã ãã
ãã ã
java.lang.RuntimeException: java.lang.ClassCastException: com.xtremelabs.robolectric.RobolectricTestRunner cannot be cast to com.xtremelabs.robolectric.internal.RobolectricTestRunnerInterface
ãåºããããã¶ãããã¯ã使ãã®JUnitã®ãã¼ã¸ã§ã³ãRobolectricã«ãã£ã¦ãªãããã4.11ã¯ãã¡ã4.10ãªã大ä¸å¤«ã
https://groups.google.com/forum/#!msg/robolectric/dtvMJSr5HkQ/VA3F3ZcCmekJ
pomæ¸ãæããã¨ãDependenciesã®ã¨ããã§ã¾ãTestã¢ã¸ã¥ã¼ã«ãã¡ãAndroidSDKããä¸ã«ãã¡ããããåè¨å®ãå¿ è¦ãªã®ãå¾®å¦ã ãã©ä»æ¹ãªãã
ã¤ã§ã¼ï¼ï¼ãããã¼ãã¹ãã£ã³ã°ï¼