0

I'm writing new features in Kotlin but some stuff are already wrote in Java.

I'm writing tests in Mockito(Kotlin) but Mockito has problem with final class so I created:

annotation class Mockable {}

And added to Gradle-build:

apply plugin: 'kotlin-allopen'

allOpen {
    annotation('com.Mockable')
}

So using this annotation class I can mock Kotlin class.

How I can use this annotation in Java class ?

1
  • have you tried just using @Mockable? Commented Jul 6, 2018 at 12:25

2 Answers 2

1

You don't need it in Java. But regardless of this, there's an alternative method to use Mockito with Kotlin:

Create a file src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker with contents:

mock-maker-inline

This changes the mocking behavior to also support final classes like the Kotlin ones. For some Java classes I had to use spy() instead of mock() afterward though.

Sign up to request clarification or add additional context in comments.

1 Comment

I saw this option but this is almost 3 times slower in performance
1

How I can use this annotation in Java class ?

You can't use this annotation in this way, because kotlin-allopen only works on Kotlin classes. In Java, just don't mark classes as final...

For that matter, in Kotlin it doesn't seem useful either if I understood you correctly: kotlin-allopen is used when the annotation is going to be processed in some way which requires creating subclasses of the annotated type, so you'd need to write open as well as annotation. Here, you can write open instead of @Mockable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.