我正在嘗試Robolectric測試,並在我們當前的項目中工作,並沒有很多運氣。我的偏好是讓這些在Android Studio 1.1.0+中運行。這是我的項目結構:清單和設置問題Robolectric使用Android Studio 1.1.0
,這裏是我的測試:
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'
}
不能完全確定,如果我們有同樣的問題。我想使用Robolectric 2.4,但是這對於Android Studio 1.0來說並不合適。最後,我仍然使用robolectric 2.3。 – 2015-02-24 16:04:56