0

tried

doAnswer(new Answer<String>() {
                @Override
                public String answer(InvocationOnMock invocation) throws Throwable {               
                    return "String";
                }
            }).when(object).voidMethod();
            Assert.assertEquals("String",object.voidMethod()); 

and

doReturn("String").when(object.voidMethod())

but this not works because voidMethod() is void. Is any variants not to use doThrow()?
I want to emulate answer of some service this void method sends some data

neklans
  • 13
  • 1
  • 5
  • Possible duplicate of [How to mock/test method that returns void, possibly in Mockito](http://stackoverflow.com/questions/6382955/how-to-mock-test-method-that-returns-void-possibly-in-mockito) – Andrew Tobilko Apr 15 '16 at 10:09
  • Why would you want a void method to return something anyway? What do you test by doing so? – mareckmareck Apr 15 '16 at 10:26
  • I want to emulate answer of some service this void method sends some data – neklans Apr 15 '16 at 10:30

1 Answers1

0

I don't see a way to ever make a method return something (via Mockito), if the signature of that method says it returns void. I think it's pretty much impossible.

mareckmareck
  • 1,560
  • 13
  • 18