6

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?

Aleksei Potapkin
  • 1,021
  • 12
  • 23

2 Answers2

4

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.

ice1000
  • 6,406
  • 4
  • 39
  • 85
  • Sorry, but I've not managed to do it. Looks like it isn't intended to work with Android gradle plugin, but it is not clearly stated anywhere in the docs. Or at least I haven't managed to find it. – Aleksei Potapkin Jun 09 '17 at 11:57
  • It's OK, and currently the newest version is `1.1.3-2`. Maybe update again? – ice1000 Jul 28 '17 at 14:56
  • 1
    BTW the newest now is `1.2.0` :D –  Nov 30 '17 at 04:18
  • 1
    I am getting the same issues. Does not appear to be working. I am using Kotlin version 1.3.41 and the same version of the plugin. – user3430360 Aug 05 '19 at 13:25
3

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

MrVasilev
  • 1,503
  • 2
  • 17
  • 34
  • @MrVasiliev, can you tell me why my plugin usage is not working please? https://stackoverflow.com/questions/60025359/how-to-enable-allopen-plugin-for-android – Lena Bru Feb 02 '20 at 10:51
  • @LenaBru did you check the path to your OpenForTesting file in the build.gradle file -> "annotation('com.example.myproject.OpenForTesting')" ? – MrVasilev Jun 09 '20 at 15:08