mockito / mockito-kotlin

Using Mockito with Kotlin
MIT License
3.1k stars 200 forks source link

: Android SDK 30 requires Java 9 (have Java 8). Tests won't be run on SDK 30 unless explicitly requested.? #422

Closed kyodgorbek closed 3 years ago

kyodgorbek commented 3 years ago

I am developing new todo app but when I run my test I am getting following warning and errors

com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.example.todo.TodoRoomRepositoryTest [Robolectric] WARN: Android SDK 29 requires Java 9 (have Java 8). Tests won't be run on SDK 29 unless explicitly requested. [Robolectric] WARN: Android SDK 30 requires Java 9 (have Java 8). Tests won't be run on SDK 30 unless explicitly requested. WARNING: No manifest file found at .\AndroidManifest.xml. Falling back to the Android OS resources only. To remove this warning, annotate your test class with @Config(manifest=Config.NONE). No such manifest file: .\AndroidManifest.xml

java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)

I have followed following stackoverflow it did not solve my issue and I have done all suggested answers

below my Testing class

import android.content.Context import android.os.Build import androidx.arch.core.executor.testing.InstantTaskExecutorRule import androidx.room.Room import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import com.example.todo.data.TodoRepository import com.example.todo.data.TodoRoomDatabase import com.example.todo.data.TodoRoomRepository import com.jraska.livedata.test import org.junit.After import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.mockito.kotlin.any import org.mockito.kotlin.spy import org.mockito.kotlin.verify import org.robolectric.annotation.Config

@RunWith(AndroidJUnit4::class) @Config(sdk = [Build.VERSION_CODES.P]) class TodoRoomRepositoryTest {

@get:Rule
val testRule = InstantTaskExecutorRule()
val now = System.currentTimeMillis()
val day = 1000 * 60 * 60 * 24

private lateinit var db:TodoRoomDatabase

@Before
fun setUp(){
    val context = ApplicationProvider.getApplicationContext<Context>()
    db  = Room.inMemoryDatabaseBuilder(context, TodoRoomDatabase::class.java)
        .allowMainThreadQueries()
        .build()
}

@Test
fun test_getUpcomingTodoCountEmpty(){
    val dao = spy(db.todoDao())
    val repo = TodoRoomRepository(dao)
    val expected = 0

    val actual = repo.getUpcomingTodosCount().test().value()
    assertEquals(expected, actual)
    verify(dao).getDateCount(any())

}

@After
fun tearDown(){
    db.close()
}

}

below my app.gradle

plugins { id 'com.android.application' id 'kotlin-android' id 'kotlin-kapt' id 'kotlin-android-extensions' }

android { compileSdkVersion 30 buildToolsVersion "30.0.2"

defaultConfig {
    applicationId "com.example.todo"
    minSdkVersion 21
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = '1.8'
}

}

dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.21.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
def room_version = "2.2.6"

implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
// Room components
implementation "androidx.room:room-ktx:$room_version"
kapt "androidx.room:room-compiler:$room_version"
androidTestImplementation "androidx.room:room-testing:$room_version"

def archLifecycleVersion = '2.2.0'

// Lifecycle components implementation "androidx.lifecycle:lifecycle-extensions:$archLifecycleVersion" //noinspection LifecycleAnnotationProcessorWithJava8 kapt "androidx.lifecycle:lifecycle-compiler:$archLifecycleVersion" androidTestImplementation "androidx.arch.core:core-testing:$archLifecycleVersion"

// ViewModel Kotlin support implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$archLifecycleVersion"

// Coroutines implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3' implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.3"

testImplementation 'junit:junit:4.+'
testImplementation 'org.mockito:mockito-inline:2.23.0'
testImplementation "org.mockito.kotlin:mockito-kotlin:2.2.11"
testImplementation 'android.arch.core:core-testing:2.1.0'
testImplementation 'androidx.test:core-ktx:1.3.0'
testImplementation 'androidx.test.ext:junit-ktx:1.1.2'
testImplementation "org.robolectric:robolectric:4.5.1"
testImplementation 'com.jraska.livedata:testing-ktx:1.1.2'

androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

}

bohsen commented 3 years ago

You can't use inline mocks in Android instrumentation-tests.