8

org.assertj:assertj-core:3.6.2用来测试我的android项目。根据官方规定,我应该将 java 8 与 assertj 3.x 一起使用。

这是我的测试类,我正在尝试验证单击执行的代码何时可以开始预期的活动。

import android.content.Intent;

import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowActivity;

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class LoginActivityTest {

    @Test
    public void testBtnLogin(){
        LoginActivity loginActivity = Robolectric.setupActivity(LoginActivity.class);

        loginActivity.findViewById(R.id.btnLogin)
                .performClick();

        Intent expectedIntent = new Intent(loginActivity,MainActivity.class);
        ShadowActivity shadowActivity = Shadows.shadowOf(loginActivity);
        Intent actualIntent = shadowActivity.getNextStartedActivity();
        Assertions.assertThat(actualIntent).isEqualTo(expectedIntent);
    }
}

但是当我运行测试时,我得到了这个错误:

:app:compileDebugUnitTestJavaWithJavac (Thread[Daemon worker,5,main]) started.
:app:compileDebugUnitTestJavaWithJavac
file or directory '/home/workspace/android/AndroidLib/app/src/testDebug/java', not found
Executing task ':app:compileDebugUnitTestJavaWithJavac' (up-to-date check took 0.006 secs) due to:
  Output file /home/workspace/android/AndroidLib/app/build/intermediates/classes/test/debug/com/cavalry/androidlib/sample/ui/activity/LoginActivityTest.class has been removed.
All input files are considered out-of-date for incremental task ':app:compileDebugUnitTestJavaWithJavac'.
Compiling with source level 1.7 and target level 1.7.
file or directory '/home/workspace/android/AndroidLib/app/src/testDebug/java', not found
Compiling with JDK Java compiler API.

/home/workspace/android/AndroidLib/app/src/test/java/com/cavalry/androidlib/sample/ui/activity/LoginActivityTest.java:43: error: cannot access Path
        Assertions.assertThat(actualIntent).isEqualTo(expectedIntent);
                  ^
  class file for java.nio.file.Path not found
1 error

:app:compileDebugUnitTestJavaWithJavac FAILED
:app:compileDebugUnitTestJavaWithJavac (Thread[Daemon worker,5,main]) completed. Took 0.556 secs.

我在谷歌上搜索,发现它可能是由错误版本的 java 引起的(我真的不确定)。

以下是有关我的 java 配置的一些信息:

$ java -version
java version "1.8.0_112"
Java(TM) SE Runtime Environment (build 1.8.0_112-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.112-b15, mixed mode)

$ javac -version
javac 1.8.0_112


$ update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      auto mode
  1            /home/java/jdk1.8.0_112/bin/java                 300       manual mode
  2            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      manual mode

并且在项目结构中,我还将JDK设置为jdk1.8.0_112 在此处输入图像描述

如果是java版本引起的,应该怎么办?如果没有,那我该怎么办?

4

2 回答 2

4

ejohansson 是对的,它可能不是超级可见,但在http://joel-costigliola.github.io/assertj/assertj-core.htmlorg.assertj.core.api.Java6Assertions中提到了,我会在以后尝试使其更清晰(https:// github.com/joel-costigliola/assertj/issues/63)。

文档已更新,希望 android 开发人员更容易找到正确的断言入口点:http: //joel-costigliola.github.io/assertj/assertj-core.html#android

于 2017-01-22T20:30:56.687 回答
0

使用 Android Studio,从 auto-import中排除org.assertj.core.api.Assertions可能是一个好主意,因此在您忘记它之后就不会发生这种情况:

  1. 暂时删除静态导入。
  2. 点击F2导航到有问题的断言。
  3. 点击Alt+Enter以显示可用意图操作的列表。
  4. 突出显示条目Assertions (org.assertj.core.api)并点击→</kbd>.
  5. 在刚刚弹出的列表中选择Exclude 'org.assertj.core.api.Assertions' from auto-import 。
于 2017-07-21T17:27:25.213 回答