You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
JpaRepository<Entity, Id> {
<S extends Entity> S save(S entity);
}
Here is my try:
val mockRepository = mock<JpaRepository> {
on { save(any()) } doThrow RuntimeException("Some exception")
}
But then I got Caused by: java.lang.NullPointerException: save(any()) must not be null
In order to fix this, I must do ugly thing, specyfing generic type like:
val mockRepository = mock<JpaRepository> {
on { save(any<Entity>()) } doThrow RuntimeException("Some exception")
}
And it works, but IDE (Intellij) is suggesting generic removal: Remove explicit type arguments
So I tried something else:
val mockRepository = mock<JpaRepository> {
on { save(Mockito.any(Entity::class.java)) } doThrow RuntimeException("Some exception")
}
and IDE is not complaining at all, but it looks ugly. Any better solution?
The text was updated successfully, but these errors were encountered:
@mtwolak I your scenario you can also use onGeneric instead of on. Then it should work without explicit type.
But I have a similar issue but I don't want/can specify the mock behavior when the mock is created, but only inside the test. So I usually use something like:
whenever(repository.save(any())).doReturn(entity)
But this also fails without specifying the type exlicitly in the any (any<Entity>()). Is there something similar to onGeneric but for the whenever workflow.
Hi,
I'm mocking spring repository with signature:
Here is my try:
But then I got
Caused by: java.lang.NullPointerException: save(any()) must not be null
In order to fix this, I must do ugly thing, specyfing generic type like:
And it works, but IDE (Intellij) is suggesting generic removal:
Remove explicit type arguments
So I tried something else:
and IDE is not complaining at all, but it looks ugly. Any better solution?
The text was updated successfully, but these errors were encountered: