У меня есть очень старый проект, который начался как проект eclipse и теперь является gradle / android studio
В настоящее время у меня есть только один тест, который хранится в пути
src/test/java/
У меня есть следующий файл Gradle
buildscript {
ext.kotlin_version = '1.2.41'
ext.latest_google_version = '27.1.1'
repositories {
jcenter()
maven { url 'https://maven.fabric.io/public' }
maven { url 'https://dl.bintray.com/jetbrains/anko' }
mavenCentral()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'io.fabric.tools:gradle:1.24.1'
}
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'
kotlin {
experimental {
coroutines 'enable'
}
}
repositories {
maven { url "https://jitpack.io" }
maven { url 'https://maven.fabric.io/public' }
maven { url 'https://dl.bintray.com/jetbrains/anko' }
mavenCentral()
google()
jcenter()
}
dependencies {
implementation fileTree(dir: 'libs', include: '*.jar')
implementation('com.crashlytics.sdk.android:crashlytics:2.8.0@aar') {
transitive = true
}
implementation('com.crashlytics.sdk.android:answers:1.4.1@aar') {
transitive = true
}
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation 'io.reactivex.rxjava2:rxjava:2.1.9'
implementation 'com.jakewharton.rxbinding:rxbinding:0.4.0'
implementation 'com.jakewharton.rxbinding:rxbinding-support-v4:0.4.0'
implementation 'io.reactivex.rxjava2:rxkotlin:2.2.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.anko:anko:0.10.3"
implementation "com.android.support:support-v13:$latest_google_version"
implementation "com.android.support:appcompat-v7:$latest_google_version"
implementation "com.android.support:support-v4:$latest_google_version"
implementation "com.android.support:support-core-utils:$latest_google_version"
implementation "com.android.support:design:$latest_google_version"
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
// UnitTest frameworks
testImplementation 'junit:junit:4.12'
testImplementation "org.mockito:mockito-core:2.18.0"
testImplementation "org.robolectric:robolectric:3.8"
// androidTest frameworks
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
}
android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
multiDexEnabled true
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/ASL2.0'
exclude 'META-INF/rxjava.properties'
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
// resources.srcDirs = ['src']
// aidl.srcDirs = ['src']
// renderscript.srcDirs = ['src']
res.srcDirs = ['res']
// assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
androidTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
Если я запускаю свои модульные тесты с файлом Gradle, как указано выше, я получаю ошибки
Unresolved reference: junit
Unresolved reference: junit
Unresolved reference: mockito
Unresolved reference: mockito
Unresolved reference: mockito
Unresolved reference: mockito
Unresolved reference: Mock
Unresolved reference: Before
Unresolved reference: MockitoAnnotations
Unresolved reference: Test
Unresolved reference: verify
Unresolved reference: verifyNoMoreInteractions
Unresolved reference: Test
Unresolved reference: Test
Thisэффективно везде, где в тесте упоминается junit.
Если я изменю
// UnitTest frameworks
testImplementation 'junit:junit:4.12'
testImplementation "org.mockito:mockito-core:2.18.0"
testImplementation "org.robolectric:robolectric:3.8"
на
// UnitTest frameworks
implementation 'junit:junit:4.12'
implementation "org.mockito:mockito-core:2.18.0"
implementation "org.robolectric:robolectric:3.8"
Тесты скомпилируются, но бегущий будет жаловаться, что набор тестов пуст.
Что я делаю не так?