ssacher-tgm / mockito

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

Concurrent mocking and invocation lead to WrongTypeOfReturnValue exception #203

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Setup a loop to repeatedly mock Object1.method1():Type1 method.
2. Run a concurrent task to invoke Object1.method2():OtherType method.
3. First loop will be interrupted with WrongTypeOfReturnValue exception.

What is the expected output? What do you see instead?
Mocking in one thread must not interfere with invocation in another thread.

What version of the product are you using? On what operating system?
Bundled versions: 1.6, 1.8, 1.8.5

Please provide any additional information below.
I've attached a unit test that fails every time it's run.

Original issue reported on code.google.com by ivankob...@gmail.com on 13 Jul 2010 at 7:16

Attachments:

GoogleCodeExporter commented 8 years ago
Hey,

Good tests, thanks!

This is not supported by Mockito. See the FAQ entry on multi-threading. 
when/verify is not thread safe because if you do it in multiple threads it will 
lead to non-deterministic results. However you can do when/verify outside of 
concurrency but allow mocks to be simply called/used in concurrency. Makes 
sense?

Original comment by szcze...@gmail.com on 18 Jul 2010 at 1:40

GoogleCodeExporter commented 8 years ago
Hi,

Fair enough, fixing the tests wasn't that hard. Although it took me a while to 
understand the reason of the failure as it wasn't obvious from the error 
message. 

Maybe it's worth putting it into the FAQ? Something along the lines: if you 
concurrently mock and use a shared mock (sorry for tautology) you *will* see 
exceptions like org.mockito.exceptions.misusing.WrongTypeOfReturnValue or 
java.util.ConcurrentModificationException.

Anyway, thanks for the wonderful library!

Original comment by ivankob...@gmail.com on 19 Jul 2010 at 7:09

GoogleCodeExporter commented 8 years ago
I like your idea, will update the FAQ.

Cheers!

Original comment by szcze...@gmail.com on 19 Jul 2010 at 8:28

GoogleCodeExporter commented 8 years ago
Updated FAQ and changed the code a bit so that ConcurrentModificationException 
should not occur (that often :). It is still not supported but at least it is 
better documented.

Original comment by szcze...@gmail.com on 13 Aug 2010 at 9:02

GoogleCodeExporter commented 8 years ago

Original comment by szcze...@gmail.com on 3 Jul 2011 at 12:43

GoogleCodeExporter commented 8 years ago
can we use synchronization here means we create an object
Object obj=new Object
and then use this like

synchronized (obj){
Object1.method1()
}
synchronized (obj){
Object1.method2()
}

Original comment by anil.goy...@gmail.com on 22 May 2014 at 8:54

GoogleCodeExporter commented 8 years ago
yes that should work. (if it's not about stubbing)

Original comment by brice.du...@gmail.com on 27 Jun 2014 at 4:14