Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin-allopen for android

Is it possible to use kotlin-allopen gradle plugin for android testing with mockito?

I've tried to add kotlin-allopen plugin to my build.gradle and define the annotation.

buildscript {
   ext.kotlin_version = '1.0.6'

   dependencies {
       classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
       classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
   }
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-allopen'
apply plugin: 'kotlin-android'

allOpen {
    annotation("com.mycompany.OpenForTest")
}

And these for annotation itself

annotation class OpenForTest

It's not working for me. Maybe I miss something?

like image 631
Aleksei Potapkin Avatar asked Feb 01 '17 10:02

Aleksei Potapkin


2 Answers

Yes you can. Because it's a compiler plugin, you'll get all-open code after compilation. So it should work with tests. Don't worry.

Edit: according to the comment area, updating the kotlin plugin version seems work. Currently the newest version is 1.2.41.

like image 69
ice1000 Avatar answered Sep 30 '22 04:09

ice1000


First add the dependency in your build.gradle (project) file:

dependencies {
        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
    }

After that apply the plugin in your build.gradle (app mobdule) file:

apply plugin: 'kotlin-allopen'

Then specify the list of annotations that will make classes open:

allOpen {
    annotation('com.example.myproject.OpenForTesting')
}

And use this annotation for every class which you want to be open

@OpenForTesting

Here is the Kotlin official documentation about All-open: https://kotlinlang.org/docs/reference/compiler-plugins.html

Hope this help

like image 37
MrVasilev Avatar answered Sep 30 '22 06:09

MrVasilev