dhamini-poornachandra / mockito

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

Inherited method of mock cannot be stubbed simultaneously with non-inherited #295

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Steps to reproduce.
1. Create a base class that is generically typed.
2. Have the base class manage an instance member that is of the generic type.
3. Create a class that inherits from said base class and uses the a parent 
method to get said instance member.
4. Mock the child class.
5. Stub the get method on the mock.
6. Stub another method.

What is the expected output? What do you see instead?
I would expect this to work.
Instead I get the following message (*Note I am not running multi-threaded 
tests)

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
String cannot be returned by getValueObject()
getValueObject() should return IValueObject
***
This exception *might* occur in wrongly written multi-threaded tests.
Please refer to Mockito FAQ on limitations of concurrency testing.

What version of the product are you using? On what operating system?

This is version 1.9.0-rc1 on ubuntu 10.04

Please provide any additional information below.
I have attached a source file with example code.

Original issue reported on code.google.com by cth...@gmail.com on 2 Dec 2011 at 5:27

Attachments:

GoogleCodeExporter commented 8 years ago
I've been trying to understand your issue, but I can't even get your example to 
compile.  Your ChildClass seems to be missing a constructor, and also missing a 
return type on the getId() method.  Is it supposed to return String, with the 
word return inserted at the start of the method?  Please clarify.

Cheers,
David.

Original comment by dmwallace.nz on 5 Dec 2011 at 7:53

GoogleCodeExporter commented 8 years ago
Hi David,

I wasn't intending that code example to be run, but rather demonstrate what I 
was having difficulty expressing in words. Sorry for wasting your time on that.

It is supposed to be returning a string, I have attached a clarification 
example, it not intended to be run, but in theory putting these classes into 
separate code files and then running a mockito test should work.

The idea is that the BaseClass is handling common functionality between all 
ChildClasses. ChildClasses (with BaseClass help) wrap a ValueType class. 
ChildClass exposes the same interface as ValueType and just accesses the 
underlying ValueType to return results. Think adapter pattern with some common 
functionality rolled into a BaseAdapter.

The problem I'm running into is that if I mock just the ChildClass the 
underlying ValueType is null. So any calls to when() on a particular method in 
order to stub that method out fail with a nullpointer because the underlying 
value object is null. My attempted solution to this was to have ChildClass use 
a BaseClass method "getValueType()" then to stub out that method on ChildClass 
to return a mocked ValueType Object.

I hope that ^^^ make sense.

Now the issue I ran into is that Mockito appears to be getting confused since 
I'm stubbing an inherited method. The "getValueType()" appears to only be 
stubbable when I don't stub anything on ChildClass. Once I stub something on 
ChildClass, the getValueType() stub appears to be using the subsequent stub 
instead, which then gets a type error.

Thanks for looking at this.

-Myles

Original comment by cth...@gmail.com on 5 Dec 2011 at 5:31

Attachments:

GoogleCodeExporter commented 8 years ago
could you make us a small runnable test?

Original comment by brice.du...@gmail.com on 5 Dec 2011 at 5:43

GoogleCodeExporter commented 8 years ago
Hi.  I followed your instructions, to build your example.  I had to add the 
word "return" on line 28.  Other than that, no problems.

For me, the test passes.  No error from Mockito.  Sorry.  This was Mockito 
1.8.5 on Windows XP.  I will try it in 1.9.0-rc1 and let you know how I go. 

Original comment by dmwallace.nz on 6 Dec 2011 at 5:44

GoogleCodeExporter commented 8 years ago
OK, I tried 1.9.0-rc1.  The test passed.  Sorry, but I don't think there's 
actually an issue here.

Original comment by dmwallace.nz on 6 Dec 2011 at 6:22

GoogleCodeExporter commented 8 years ago
That's alright, I'm having trouble reproducing it with a simple example right 
now, if that changes, I'll reopen this, otherwise I may just be doing something 
wrong.

Thanks for your time.

Original comment by cth...@gmail.com on 6 Dec 2011 at 2:50

GoogleCodeExporter commented 8 years ago
Shall we set this issue as invalid?

Original comment by brice.du...@gmail.com on 13 Jan 2012 at 10:25

GoogleCodeExporter commented 8 years ago

Original comment by dmwallace.nz on 13 Jan 2012 at 10:50