question-mark
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.

doNothing() cannot be used on suspend Unit functions

See original GitHub issue

Mocking Unit method where the first call should doNothing() and then 2nd doThrow(), produces the following error:

Only void methods can doNothing()!

Example:

doNothing().doThrow(RuntimeException::class.java).whenever(mock).foo() where foo() is suspend fun foo().

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
gkylafascommented, Dec 10, 2020

@iadcialim You can use

doReturn(Unit).whenever(spy).functionReturningUnit()

which does not execute functionReturningUnit() as

whenever(spy.functionReturningUnit()).thenReturn(Unit)

would.

1reaction
iadcialimcommented, Apr 12, 2020

@nhaarman In my case, when a suspend function of a mocked or spied object is called, I don’t want it to do whatever it has inside. So its like doNothing but that now for functions that return a Unit. But now it does not work as specified above. Any workaround for this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Non-void method with doNothing() in Mockito? - Stack Overflow
The create() method is not void, so you cannot mock it with doNoting() so you have to use doReturn instead. In you case...
Read more >
Mockito 2.3.4 API - javadoc.io
Mockito verifies argument values in natural java style: by using an equals() method. Sometimes, when extra flexibility is required then you might use...
Read more >
`when` and `do*` | Migrating from Mockito | MockK Guidebook
Answer only contains a single method, so Kotlin allows a function literal to be used instead through SAM conversion. val mockedFile = mock(File::class.java) ......
Read more >
Mockito 2.2.7 API
The Mockito library enables mock creation, verification and stubbing. This javadoc content is also available on the http://mockito.org web page.
Read more >
Mocking/stubbing stuff near the "bottom of the pile"...
Since touching a real database is not a unit test anymore, personally I prefer not to do just "hibernate tests" if not necessary....
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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found