Deb
Deb

Reputation: 193

How to mock a Unit method in scala

I have a method which doesn't return any value actually it process a data frame and registers as temp table, when I try to mock that method for testing I get below error.

is a *void method* and it *cannot* be stubbed with a *return value*!
Voids are usually stubbed with Throwables:
doThrow(exception).when(mock).someVoidMethod();

sample code:

val mock_testmethod=mock[objectwrapper](Answers.RETURNS_DEEP_STUBS)
  when mock_testmethod.unitmethod(any[String]).thenReturn(dataframe)

I am new with mocking and scala.

Upvotes: 2

Views: 5106

Answers (3)

Salavat Shirgaleev
Salavat Shirgaleev

Reputation: 48

doNothing().when(mock).someVoidMethod();

Upvotes: 0

What helped was the following:

when(mockedObject.method()).thenReturn(())

The empty braces in the thenReturn helped me return a Unit.

Upvotes: 0

rstar
rstar

Reputation: 1006

Actually the error message has given some hints. So you can:

doNothing().when(mock_testmethod).unitmethod(any[String])

Upvotes: 2

Related Questions