3

DBAdapterで何らかのテストを行うためにAndroid Studioでテストクラスを実装しようとしています。データベースを使用するために携帯電話でテストを実行する必要があるため、私はInstrumentation Unit Testを作成しました(ユニットテストだけで実行しようとしましたが、データベースを使用する必要があります。それらはローカルでのみ実行されます)。Android Studio Junitテストパッケージorg.junitが存在しません

問題は、私はデバイスを実行しているように私の携帯電話を使用して、テストクラスを実行しようとすると、コンパイラは次のエラーをスローしていることである:私はsolutioneを探してきた

error: package org.junit does not exist 

が、私見つかりませんでした。

これは私のテストクラス(単なるスケルトン)です:

import org.junit.Test; 
import android.support.test.runner.AndroidJUnit4; 

import static org.junit.Assert.*; 


public class DbAdapterTest { 

    @Test 
    public void testCreateSeries() throws Exception { 

    } 
} 

そして、この1つはbuild.gradleスクリプトです:

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.2" 

    defaultConfig { 
     applicationId "com.w2w.whattowatch" 
     minSdkVersion 15 
     targetSdkVersion 23 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'),  'proguard-rules.pro' 
     } 
    } 
    testOptions { 
     unitTests.returnDefaultValues = true 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:appcompat-v7:23.1.1' 
    compile 'com.android.support:design:23.1.1' 
    testCompile 'junit:junit:4.12' 
    testCompile "org.mockito:mockito-core:1.9.5" 
} 

私はまた別の問題があります。ご覧のとおり、私はこれもインポートしました:

import android.support.test.runner.AndroidJUnit4; 

しかし、実行する前に、 "ランナー"は "シンボルを解決できません"と言います。私はbuild.gradleにTestInstrumentationRunnerを追加しましたが、まだ動作していません。

+0

あなたはユニットテストを書いている( '/ SRC/test')、またはUIテスト(' SRC/androidTest')? –

+0

はい、計装ユニットテストなので、実行するにはsrc/androidTestにある必要があります。 http://developer.android.com/intl/es/training/testing/start/ index.html#run-instrumented-tests – gascon95

+0

こちらをご覧ください:https://github.com/googlesamples/android-testing/tree/master/ui/espresso/BasicSampleユニットテストのために、Junitユニットを使用します。 –

答えて

4

いいえ、私はそれを解決します、これは私のために働いた解決策です。

私はこの依存関係を持っていなかったので、私はbuild.gradleスクリプトに追加します:?

androidTestCompile 'com.android.support:support-annotations:23.1.1' 
androidTestCompile 'com.android.support.test:runner:0.4.1' 
androidTestCompile 'com.android.support.test:rules:0.4.1' 
+0

はい、これは私の仕事です、非常に感謝 – wangzhengyi

+0

私はまた、この回答を提案したことをしなければならなかったhttps://stackoverflow.com/a/42678218/1274764 – Hardy

関連する問題