Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mockito fails for reified kotlin methods in Java tests

See original GitHub issue

Actual 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.



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( syntax. It is safer to stub spies - 
   - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.

    at my.package.stupidTest1(

More info:

Expected behavior Java test runs fine and the header() method can be mocked

To Reproduce

  1. set up Mockito in a java project
  1. create the file resources/mockito-extensions/org.mockito.plugins.MockMaker with the content mock-maker-inline so that final Kotlin classes can be mocked
  2. create a test class with a single test method:
  void stupidTest1() {
    Context context = mock(Context.class);

    String test1 = "hello123";
  1. run mvn tests, it should fail almost every time with the exception shown above

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:21 (13 by maintainers)

github_iconTop GitHub Comments

tipsycommented, Feb 14, 2021
tipsycommented, Jul 29, 2020

A suggestion (Mockito.<String>when(context.header("Authorization")).thenReturn(test1);) was recently posted in mockito/mockito#1943, might be worth a shot.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found