2015-02-24 39 views
6

我正在嘗試Robolectric測試,並在我們當前的項目中工作,並沒有很多運氣。我的偏好是讓這些在Android Studio 1.1.0+中運行。這是我的項目結構:清單和設置問題Robolectric使用Android Studio 1.1.0

enter image description here

,這裏是我的測試:

import android.widget.Button; 

import com.mycompany.android.app.R; 

import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.robolectric.Robolectric; 
import org.robolectric.RobolectricTestRunner; 
import org.robolectric.annotation.Config; 

import static org.junit.Assert.assertNotNull; 

@Config(manifest = "AndroidManifest.xml") 
@RunWith(RobolectricTestRunner.class) 
public class SplashActivityTest { 

    private SplashActivity splashActivity; 

    @Before 
    public void setup() { 
     splashActivity = Robolectric.buildActivity(SplashActivity.class).create().start().resume().get(); 
    } 

    @Test 
    public void shouldNotBeNull() { 
     Button signUpButton = (Button) splashActivity.findViewById(R.id.sign_up_button); 
     assertNotNull(signUpButton); 

     Button loginButton = (Button) splashActivity.findViewById(R.id.login_button); 
     assertNotNull(loginButton); 
    } 

} 

不管我做什麼,試圖讓該架構通過改變到清單的路徑找到測試,它找不到它 - 我收到WARNING: No manifest file found at ./AndroidManifest.xml.Falling back to the Android OS resources only.消息或API Level XX is not supported - Sorry!消息。最後,我想這是我收到以下錯誤,當測試運行的原因:

android.content.res.Resources$NotFoundException: unknown resource 2130903074 

我有實驗選項打開,配置了合適的搖籃插件(單元測試工作好),但我不確定我缺少什麼來使儀器測試正常運行。

應用級構建文件:

apply plugin: 'org.robolectric' 

testCompile 'junit:junit:4.12' 
testCompile 'org.mockito:mockito-core:1.9.5' 
testCompile 'org.robolectric:robolectric:2.4' 
testCompile 'org.hamcrest:hamcrest-core:1.1' 
testCompile 'org.hamcrest:hamcrest-library:1.1' 
testCompile 'org.hamcrest:hamcrest-integration:1.1' 

頂級構建文件:

dependencies { 
    classpath 'com.android.tools.build:gradle:1.1.0' 
    classpath 'org.robolectric:robolectric-gradle-plugin:1.0.0' 
} 
+0

不能完全確定,如果我們有同樣的問題。我想使用Robolectric 2.4,但是這對於Android Studio 1.0來說並不合適。最後,我仍然使用robolectric 2.3。 – 2015-02-24 16:04:56

回答

2

在你的情況下一個變化可能會有所幫助:

@Config(manifest = "src/main/AndroidManifest.xml", emulateSdk = 18, reportSdk = 18) 

而且閱讀@nenick指導

+0

這使我更加接近 - 註釋中的路徑加上其他屬性肯定有幫助(現在我在資源中獲得NPE)。我將很快閱讀指南。 – TheIcemanCometh 2015-02-24 19:58:26

+0

我假設你正在使用App compat。檢查陰影菜單項 – 2015-02-24 20:26:32

+0

的點我們目前沒有使用任何AppCompat庫,但我遇到的其中一個問題是在清單中定義了Google Play服務版本。來自Google的文檔中有一些例子,其值應該是一個整數,但這會使得RE中的AndroidManifest類拋出NPE。如果我將版本的定義更改爲String,它似乎就是這樣。奇怪。 – TheIcemanCometh 2015-02-25 15:29:00

0

我只是改爲:

@RunWith(RobolectricTestRunner.class) 

和它的作品。所有其他配置的東西似乎是沒有必要的。

2

經過幾天努力,我終於想通了你的問題的根本原因:

WARNING: No manifest file found at ./AndroidManifest.xml.Falling back to the Android OS resources only. messages, or API Level XX is not supported - Sorry! 

我StackOverflow上發現很多的答案告訴我,清單文件的路徑添加到@Config這樣:

@Config(manifest = "src/main/AndroidManifest.xml") 

它做的工作爲我的項目之一,但是當我打開一個新的項目,並建立測試環境,同樣的錯誤信息彈出。

背後的原因將是你的項目的不同工作目錄。您應該首先找到您的工作目錄,然後在您的@Config中指定相對路徑(maniest =「...」)。

我在哪裏可以找到Android Studio中的工作目錄?

Run -> Edit Configurations -> Junit -> Your Test 
-> Configuration Tab -> **Working directory:** 

就拿我的工作目錄作爲一個例子,我的工作目錄是

"/Users/Bible/AndroidstudioProjects/MVP" 

這樣的路徑,我AndroidManifest.xml中應

@Config(manifest = "app/src/main/AndroidManifest.xml") 

在這裏你去!希望這個答案能讓你的生活更輕鬆。

+0

終於解決了我的問題。 「它確實對我的一個項目有用,但是當我打開一個新項目並設置測試環境時,同樣的錯誤消息彈出」=>同樣發生在我身上 – 2017-08-10 12:40:16

+0

我之前沒有找到您的答案(可能是因爲底部的答案)。它確實爲我的項目之一工作,我在這裏回答:https://stackoverflow.com/a/4557188​​4/4802664然後創建一個新的項目&得到了同樣的錯誤(花了我3天修復,在第三天得到你的答案)。現在我會在答案中鏈接你的答案。 – 2017-08-10 12:41:56

0

如果您使用的是最新的Android Studio(1.2.x),那麼它最好使用RobolectricGradleTestRunner.class 它專爲不同的構建風格和測試風格而設計。您也不需要指定源代碼清單。它會根據您正在運行的構建風格來獲取清單。當你有不同的構建環境(分段,生產)並且你想在特定環境中運行測試用例時,這是特別有用的

2

我遇到了同樣的錯誤。我們使用多種口味和buildtypes 所以,有步驟,使其工作:

1)的Android工作室的測試運行配置

Build Variants menu

你必須在Windows中設置工作目錄設置爲$ MODULE_DIR $太。 robolectric.org/getting-started/應該這樣說。

Run Configuration Menu

2)單元測試應註明是這樣的:

@RunWith(RobolectricTestRunner.class) 
@Config(constants = BuildConfig.class, sdk = 21, manifest = "src/main/AndroidManifest.xml", packageName = "com.example.yourproject") 
public class SomeFragmentTest { 

下面我們就簡單的鏈接清單文件和的packageName