Kotlin have a compiler plugin called all open. It forces that all the classes with some annotations are open.
I want to use this feature for my tests but I don't want it in my production code (I want my classes closed). How can I do this?
I tried something like:
test {
allOpen {
annotation('com.my.Annotation')
}
}
But the code is execute always.
single system property can be used to specify a single test. You can do gradle -Dtest. single=ClassUnderTestTest test if you want to test single class or use regexp like gradle -Dtest. single=ClassName*Test test you can find more examples of filtering classes for tests under this link.
Use the command ./gradlew test to run all tests.
I think, that you can do so:
android.applicationVariants.all { ApplicationVariant variant ->
boolean hasTest = gradle.startParameter.taskNames.find {it.contains("test") || it.contains("Test")} != null
if (hasTest) {
apply plugin: 'kotlin-allopen'
allOpen {
annotation('com.my.Annotation')
}
}
}
Then you will not need to pass a property when running the test.
It happens because the plugin is applied in the root of the build.gradle
file.
A solution that will 100% work is to not apply the plugin unless some project property is set.
if (project.hasProperty("allopen")) {
apply plugin: "kotlin-allopen"
allOpen {
annotation('com.my.Annotation')
}
}
and run test with the property: gradle -Pallopen test
.
Maybe some Gradle gurus can chime in with a better solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With