2016-03-07 171 views
2

我已經繼承了舊版Android項目並開始添加Robolectric 3和JUnit 4測試用例。可悲的是,目前主要的Application類不能被初始化,因爲意大利麪代碼和框架在Robolectric中不起作用。我想替換測試的應用程序類。但Robolectric崩潰之前,我甚至可以更新RuntimeEnviroment.application變量。 有沒有辦法在Robolectric中禁用清單應用程序的創建?如何覆蓋Robolectric應用程序?

這是我一直在努力做的事情:

@RunWith(RobolectricGradleTestRunner.class) 
@Config(constants = BuildConfig.class, sdk = 21, manifest = Config.NONE) 
public class TestClass { 
    @Before 
    public void setUp() { 
     RuntimeEnvironment.application = new Application(); 
    } 

    @Test 
    public void testFunc() { 
     // some testing goes here 
    } 
} 

但由於失敗解析框架:

java.lang.RuntimeException 
    at org.robolectric.util.SimpleFuture.run(SimpleFuture.java:61) 
    at org.robolectric.shadows.ShadowAsyncTask$2.run(ShadowAsyncTask.java:96) 
    at org.robolectric.util.Scheduler.runOrQueueRunnable(Scheduler.java:230) 
    at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:85) 
    at org.robolectric.util.Scheduler.post(Scheduler.java:72) 
    at org.robolectric.shadows.ShadowAsyncTask.execute(ShadowAsyncTask.java:93) 
    at android.os.AsyncTask.execute(AsyncTask.java) 
    at com.facebook.internal.Utility.loadAppSettingsAsync(Utility.java:771) 
    at com.facebook.FacebookSdk.sdkInitialize(FacebookSdk.java:167) 
    at com.facebook.FacebookSdk.sdkInitialize(FacebookSdk.java:146) 
    at com.parse.FacebookController$FacebookSdkDelegateImpl.initialize(FacebookController.java:187) 
    at com.parse.FacebookController.initialize(FacebookController.java:70) 
    at com.parse.ParseFacebookUtils.initialize(ParseFacebookUtils.java:108) 
    at com.parse.ParseFacebookUtils.initialize(ParseFacebookUtils.java:92) 
    at MyApplication.initParse(StrongliftsApplication.java:174) 
    at MyApplication.onCreate(StrongliftsApplication.java:112) 
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:140) 
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433) 
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240) 
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188) 
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
    at org.junit.runners.Suite.runChild(Suite.java:128) 
    at org.junit.runners.Suite.runChild(Suite.java:27) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) 

回答

7

所以它發生Robolectric開發商forsaw這種情況,並@Config指令允許應用程序覆蓋。另外顯示覆蓋並沒有幫助我,所以它可以被刪除。

@RunWith(RobolectricGradleTestRunner.class) 
@Config(constants = BuildConfig.class, sdk = 21, application = MyTestApplication.class) 
public class TestClass { 
    @Test 
    public void testFunc() { 
     // some testing goes here 
    } 
} 

由於我們的應用程序使用單例模式,我還必須初始化單例實例。我忽略了所有其他關於purpouse的初始化,因爲它大多是在啓動框架。以下是我的MyTestApplication的樣子:

public class MyTestApplication extends MyApplication { 
    @Override 
    public void onCreate() { 
     MyApplication.instance = this; 
    } 
} 
+1

你能分享MyTestApplication課程嗎?至少是MyTestApplication的基本版本。 – LostPuppy

+1

@LostPuppy確定,剛剛添加了我們的確切代碼 – Ritave

1

有一個更簡單的解決方案。

只需在測試源中放置一個類Test<YouApplicationName> ** ** Robolectric將會拿起它。

所以,如果您的應用程序的名稱是org.my.package.SuperApplication然後在文件夾中創建test\java\org\my\package\TestSuperapplication,你並不需要提供在@Config部分東西。

+0

很高興知道,沒有在文檔中找到它,謝謝!儘管我更願意明確地陳述我的模擬應用程序,但這樣下一個開發人員將知道發生了什麼。這也允許不同的應用程序進行不同的測試。 – Ritave

+0

當然。這是你的代碼。我認爲你永遠不會使用第二個應用程序 –

相關問題