JUnit是一個Java語言的單元測試框架。它由Kent Beck和Erich Gamma建立,逐漸成為源於Kent Beck的sUnit的xUnit家族中最為成功的一個。 JUnit有它自己的JUnit擴展生態圈。多數Java的開發環境都已經集成了JUnit作為單元測試的工具。
JUnit是由 Erich Gamma 和 Kent Beck 編寫的一個回歸測試框架(regression testing framework)。Junit測試是程序員測試,即所謂白盒測試,因為程序員知道被測試的軟件如何(How)完成功能和完成什么樣(What)的功能。
1.首先在module的build.gradle中添加以下依賴
testImplementation 'junit:junit:4.13-beta-3'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

2.選中我的項目中要測試的方法OnCreate(Bundle savedInstanceState),右鍵選擇GO TO -> Test

3.然后選擇Create New Test

4.依次填寫Class Name、Destination package,勾選setUp,勾選要測試的方法

5.選擇路徑

6.在setUp()方法中完成實例化對象

7.在LoginMainActivityTest上右鍵,選擇Run LoginMainActivityTest

8.選擇模擬器

9.運行結果如圖所示

從運行結果"Tests ran to completion"看出本次測試運行成功,沒有拋出異常。在Event Log中顯示了"Tests passed:1"。