Android에서 JUnit4를 사용하는 방법
                                            
                                                
                                                
                                                
                                                
                                                
                                                 8713 단어  안드로이드JUnitAndroidStudio
                    
Android SDK 지원에 대해서는 여기를 참조하십시오.
계속 · Android에서 JUnit4를 사용하는 방법 (Android SDK에서 공식 지원되었습니다!)
쓰기 시점(2014/04/07)의 환경, 버전 등
이클립스의 경우
build.gradle 편집 (JUnit4, Android-JUnit4 jar 추가, TestRunner 지정)
build.gradle 에 다음을 추가한다.build.gradle
android {
    defaultConfig {
        testInstrumentationRunner "com.uphyca.testing.JUnit4InstrumentationTestRunner"
    }
    dependencies {
        // 行末のバージョン指定(4.+ 等)は適宜変更
        androidTestCompile ("junit:junit:4.+")
        androidTestCompile ("com.uphyca:android-junit4:+")
    }
    // LICENS.txtがどうのこうのというエラーが出たら、他のjarと競合しているので下記の記述を追加。
    // パスはAndroidStudioの「Project」ペインでjarの中身を見て適宜変更。
    // (AndroidStudioのバグらしい)
    packagingOptions {
        // for AndroidStudio's bug. duplication error of file below.
        pickFirst 'META-INF/LICENSE.txt'
        pickFirst 'LICENSE.txt'
    }
}
추가 후,
Tools > Android > Sync Project with Gradle Files (또는 툴바의 Sync Project with Gradle Files 아이콘  )를 실행한다. 이렇게하면 Maven Central에서 jar가 다운로드됩니다.
 )를 실행한다. 이렇게하면 Maven Central에서 jar가 다운로드됩니다.AndroidManifest.xml 편집
앱의 AndroidManifest.xml이 아닌 테스트 코드의 AndroidManifest.xml이므로주의하십시오.

AndroidManifest.xml
<manifest>
    <!-- 下記以外は省略しています -->
    <instrumentation
        android:name="com.uphyca.testing.JUnit4InstrumentationTestRunner"
        android:targetPackage="your.app.package" />
</manifest>
build.gradle 의 defaultConfig로 덮어쓰기 때문에 android:name="com.uphyca.testing.JUnit4InstrumentationTestRunner" 는 쓰지 않아도 움직이는 모양.Test 클래스 만들기
예 : AndroidUtil 클래스의 Test 클래스
AndroidUtilTest.java
import com.uphyca.testing.AndroidTestCase;  
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
// テスト内容に応じて com.uphyca.testing 内の
// AndroiTestCase, ActivityTestCase 等を extends する
public class AndroidUtilTest extends AndroidTestCase {
    @Test   // @Testアノテーションでテストメソッドを指定。
    public void ほげメソッドがXXXを返すテスト() {
        // メソッド名をtestで始める必要なし。
    }
}
com.uphyca.testing내의 TestCase 클래스를 사용하는 것 이외는, 통상의 JUnit4의 사용 방법과 같다.
JUnit3 계열의 Test 클래스를 JUnit4 용으로 재기록하는 예
JUnit3.8용(발췌)
AndroidUtilTest.java
import android.test.AndroidTestCase;
// android.test.AndroidTestCase を extends
public class AndroidUtilTest extends AndroidTestCase {
    // メソッド名をtestで始める。
    public void test_checkメソッドがIllegalArgumentExceptionを送出する() {
        try {
            AndroidUtil.check(null);
        } catch (IllegalArgumentException e) {
            assertTrue(true);
        }
    }
}
JUnit4용(발췌)
AndroidUtilTest.java
import com.uphyca.testing.AndroidTestCase;  
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
// com.uphyca.testing.AndroidTestCase を extends
public class AndroidUtilTest extends AndroidTestCase {
    @Test(expected = IllegalArgumentException.class)
    public void checkの引数がnullの時IllegalArgumentExceptionが送出される() {
        AndroidUtil.check(null);
    }
}
AndroidStudio에서 실행할 설정
Run > Edit Configurations... (또는 툴바)의 specific instrumentation runner(optional): 에 com.uphyca.testing.JUnit4InstrumentationTestRunner 를 지정한다.

AndroidStudio에서 테스트 실행
툴바의
Run 아이콘등으로부터 실행.
                Reference
이 문제에 관하여(Android에서 JUnit4를 사용하는 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/AbeHaruhiko/items/8b3b11421b431d219617텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)