无法解析符号InstantTaskExecutorRule

时间:2018-01-01 11:06:09

标签: android unit-testing junit4 rx-java2

我打开示例代码BasicRxJavaSample(来自本文Room+RxJava) 最重要的是:

@Rule
public InstantTaskExecutorRule instantTaskExecutorRule = 
    new InstantTaskExecutorRule();

BasicRxJavaSample都可以。 但我不能在我的测试中应用这个。 这就是发生的事情:

无法解析符号InstantTaskExecutorRule

enter image description here

手动导入不起作用:

enter image description here

我的自动完成功能就像这样 enter image description here

但应该如此

enter image description here

我的应用build.gradle(full gradle here):

// tests
testImplementation 'junit:junit:4.12'
androidTestCompile "com.android.support:support-annotations:$supportVersion"
testImplementation "android.arch.core:core-testing:$archVersion"
// Test helpers for Room
testImplementation "android.arch.persistence.room:testing:1.0.0"
// https://github.com/mockito/mockito
testImplementation 'org.mockito:mockito-core:2.13.0'
androidTestImplementation 'org.mockito:mockito-android:2.13.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'com.android.support.test:rules:1.0.1'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
// https://developer.android.com/topic/libraries/testing-support-library/packages.html
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-idling-resource:3.0.1'

7 个答案:

答案 0 :(得分:28)

通过androidTestImplementation替换testImplementation。因此,对文件夹androidTest的测试可以访问该库。

dependencies {
    androidTestImplementation "android.arch.core:core-testing:1.0.0"
}

答案 1 :(得分:8)

对于androidX迁移,添加

androidTestImplementation "androidx.arch.core:core-testing:2.0.0"

答案 2 :(得分:2)

请将这两个依赖项放在您的gradle文件中,

dependencies {

    // Test helpers for LiveData
    testImplementation "android.arch.core:core-testing:1.0.0"

    // Test helpers for Room
    testImplementation "android.arch.persistence.room:testing:1.0.0"
}

更多信息请通过此链接, Android Architecture components integration guide

答案 3 :(得分:2)

我知道已经晚了,但是我想为这个已接受的答案添加一件事, 如果要使用,

To

在您的 JUnit 测试用例中,即在 test文件夹中,然后使用以下依赖项,即通过 testImplementation

From

如果要在用户界面或集成测试用例中使用#include <iostream> #include <string> #include <vector> template <typename To, typename From, decltype( To(std::declval<From>()), bool{} ) = true> To convert (From const & f) { return f; } template <typename To, typename From> struct IsConvertible { template <typename T, typename F, typename = decltype(convert<T>(std::declval<F>()))> static std::true_type test(int); template <typename...> static std::false_type test(...); static bool const value = decltype(test<To, From>(0))::value; }; int main () { std::cout << "IsConvertible=" << IsConvertible<float, int>::value << std::endl; std::cout << "IsConvertible=" << IsConvertible<int, std::string>::value << std::endl; } ,请使用 androidTestImplementation 。即:

created() { let uri = 'http://192.168.10.75:8000/api/combine_data'; // sample this.axios.get(uri).then(response => { this.finish_goods = response.data.data.goods; this.items = response.data.data.foods; }); }

如果要同时添加两者,请使用实现,即:

@Rule public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();

答案 4 :(得分:0)

我认为某些链接库存在冲突。 我解决了这个问题,我使用 blockingGet() blockingFirst()

最后,我用了 https://developer.android.com/training/testing/junit-runner.html#using-android-test-orchestrator

<div id="scena">
  <!-- EDIT #1 -->
  <div id="kostka" class="animation-1">
    <!-- EDIT #1 END -->
    <div class="sciana front">
      1
    </div>
    <div class="sciana back">
      2
    </div>
    <div class="sciana left">
      3
    </div>
    <div class="sciana right">
      4
    </div>
    <div class="sciana top">
      5
    </div>
    <div class="sciana bottom">
      6
    </div>
  </div>

这就是你需要的!

答案 5 :(得分:0)

有时候,取决于您的Gradle配置,测试依赖项可能是选择适当的构建版本的问题。就我而言,测试仅配置为调试构建版本。

答案 6 :(得分:0)

dependencies {
    testImplementation "androidx.arch.core:core-testing:2.1.0"
}