2017-01-22 57 views
5

我正在使用org.assertj:assertj-core:3.6.2來測試我的android項目。 根據offical ducoment,我應該使用java 8與assertj 3.x.未找到java.nio.file.Path的類文件

這是我的測試類,我試圖驗證點擊執行的代碼是否可以啓動預期的活動。

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 

而且在項目結構,我ALSE設置的JDK jdk1.8.0_112 enter image description here

如果它是由Java版本造成的,我該怎麼辦? 如果不是,那我該怎麼辦?

+7

在Android上使用這個靜態導入:'import static org.assertj.core.api.Java6Assertions。*;'http://joel-costigliola.github.io/assertj/assertj-core-quick-start。 html – ejohansson

+0

這是一個編譯錯誤。當你進行測試時並沒有發生。 – EJP

回答

4

ejohansson是對的,它可能不是非常明顯,但org.assertj.core.api.Java6Assertionshttp://joel-costigliola.github.io/assertj/assertj-core.html中提到,我會盡量使它在未來更清晰(https://github.com/joel-costigliola/assertj/issues/63)。

文檔已更新,希望這將是更容易爲Android開發者找到正確的斷言切入點: http://joel-costigliola.github.io/assertj/assertj-core.html#android

+0

哦,這是我的錯,我沒有讀過[一分鐘出發指南](http://joel-costigliola.github.io/assertj/assertj-core-quick-start.html),謝謝! –

0

利用Android Studio中,它可能是一個好主意,exclude org.assertj.core.api.Assertions from auto-import,所以它不會發生未來,忘了之後:

  1. 暫時取消靜態導入。
  2. 點擊F2導航到有問題的斷言。
  3. 命中Alt鍵 + 輸入以顯示可用 意圖操作的列表。
  4. 突出顯示Assertions (org.assertj.core.api)的條目,並點擊
  5. 選擇從剛剛彈出的列表中自動導入中排除'org.assertj.core.api.Assertions'。