Mockito fails for reified kotlin methods in Java tests
See original GitHub issueActual behavior (the bug)
Running simple unit tests on Java with Mockito fails when mocking the .header()
method of class Context.kt. Apparently Mockito can not choose the correct method to mock. This behavior is not 100% reproducible, it fails for 90% of the time but runs fine in roughly 10% of the time. A user on Stackoverflow was able to reproduce the issue as well.
Exception:
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
String cannot be returned by header()
header() should return Validator
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
- with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.
at my.package.stupidTest1(JavalinTest.java:28)
Expected behavior Java test runs fine and the header() method can be mocked
To Reproduce
- set up Mockito in a java project
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.2.0</version>
<scope>test</scope>
</dependency>
- create the file
resources/mockito-extensions/org.mockito.plugins.MockMaker
with the contentmock-maker-inline
so that final Kotlin classes can be mocked - create a test class with a single test method:
@Test
void stupidTest1() {
Context context = mock(Context.class);
String test1 = "hello123";
when(context.header("Authorization")).thenReturn(test1);
}
- run mvn tests, it should fail almost every time with the exception shown above
Issue Analytics
- State:
- Created 3 years ago
- Comments:21 (13 by maintainers)
Top Results From Across the Web
Final Kotlin class can not be mocked because method "should ...
I have enabled the inline mock maker as described in the Mockito documentation by creating the file resources/mockito-extensions/org.mockito.
Read more >Kotlin unit test failing when using generics and Mockito - Support
The original code needs to be an interface w/ a generic, so I don't see any reason to change it to accommodate the...
Read more >Troubleshooting Mockito-Kotlin in Android Testing - mvndy
Scenario 1: Invalid use of matchers? (ノ`Д´)ノ ... Mockito is pretty descriptive about this mistake. Be sure you're not combining raw values with...
Read more >Mockito-kotlin + coroutines: Mocked return value classes fail to ...
I am having an issue when using value classes into a corroutine block method and then trying to mock that method. The issue...
Read more >Using mockito with kotlin - Derek Wilson
Getting matchers to work with Kotlin can be a problem. If you have a method written in kotlin that does not take a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This will be fixed soon: https://github.com/tipsy/javalin/pull/1162
A suggestion (
Mockito.<String>when(context.header("Authorization")).thenReturn(test1);
) was recently posted in mockito/mockito#1943, might be worth a shot.