I am trying to mock MongoCollection class in kotlin
class MyTest {
val mockCollection: MongoCollection<Document> = mock(MongoCollection<Document>::class.java)
}
it given an error only classes are allowed on the left side of a class literal
on researching a bit I found this only classes are allowed on the left hand side of a class literal I tried create a type for MongoCollection and then passing it to the mock but it gives an error as the mock is of Type.
I also tried casting the mock to Document as below
val mockCollection: MongoCollection<Document> = mock(MongoCollection::class.java) as MongoCollection<Document>
but that gives an NullpointerException exception during access of MongoCollection inside implementation of code.
I have tried both the
- Mockito kotlin library -https://mvnrepository.com/artifact/com.nhaarman.mockitokotlin2/mockito-kotlin/2.2.0
- Java mockito core - https://mvnrepository.com/artifact/org.mockito/mockito-core/3.5.13
and both of them have the same error?
I tired writing the same test in java and casting of generics work in it.
MongoCollection<Document> mockCollection = (MongoCollection<Document>) mock(MongoCollection.class);
Does anyone have any experience of mocking generic classes in Kotlin?