ssacher-tgm / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

Create an answer recorder #199

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Some time ago, I needed to verify a sequence of "actions" (returns or throws) 
in a test. For example, if a given method is called on an object, it should 
return a value. If it's called again, it should throw an exception. Since I 
couldn't find any way of stubbing this sequence with Mockito, I created an 
"AnswerRecorder", so that I could do something like this:

Answer<String> answer = AnswerRecorder
                            .forClass(String.class)
                            .recordReturn("Foo")
                            .recordThrow(new IOException.class)
                            .getAnswer();

given(someObject.doSomething()).willAnswer(answer);

The doSomething() method will return "Foo" on the first call and then throw an 
IOException on the second call.

The class and its test are attached.

Original issue reported on code.google.com by otavioma...@gmail.com on 18 Jun 2010 at 8:03

Attachments:

GoogleCodeExporter commented 8 years ago
Hey Otavio,

I'm curious why didn't you simply do:

given(someObject.doSomething()).willReturn("Foo").willThrow(new IOException());

Is it because you overlooked it in the docs (happens to everyone ;) or there 
something more into the AnswerRecorder I don't yet follow?

Original comment by szcze...@gmail.com on 7 Jul 2010 at 5:21

GoogleCodeExporter commented 8 years ago
Oh, sorry... I did overlook it in the docs.

Thank you very much.

Original comment by otavioma...@gmail.com on 7 Jul 2010 at 12:31

GoogleCodeExporter commented 8 years ago
no probs. closing this one

Original comment by szcze...@gmail.com on 7 Jul 2010 at 6:35