@RealObject

71 views
Skip to first unread message

Christine

unread,
Jan 21, 2014, 4:39:06 AM1/21/14
to robol...@googlegroups.com
I have a Shadow class, and I want to have an instance of the real class so I can use that for some methods. I have this code in my Shadow class:

@Implements(LoginApiImpl.class)
public class ShadowLoginApiImpl implements LoginApi {

@RealObject
private LoginApiImpl realAPI;

...

@Override
@Implementation
public synchronized List<Channel> getTopChannels() throws Exception {

List<Channel> list = realAPI.getTopChannels();


but the call to getTopChannels in realAPI gets Shadowed. What do I need to do to make the call to the real object, not the shadow?

Christine

Roman Mazur

unread,
Jan 21, 2014, 4:53:21 AM1/21/14
to robol...@googlegroups.com
Well, first of all, I would not recommend you to use Robolectric to create mocks of your application classes/interfaces. Personally I use Mockito for this.
Getting back to the business :), you might use reflection API to access your real object internals, yet again, mockito will free you from a lot of pain.


--
You received this message because you are subscribed to the Google Groups "Robolectric" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robolectric...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Best regards,
Roman Mazur 

Software engineer at Stanfy (http://stanfy.com.ua)
Skype: roman.mazur.f

Christine

unread,
Jan 21, 2014, 3:36:34 PM1/21/14
to robol...@googlegroups.com
Using Shadow classis is not my first choice either. But there's a class that uses HttpUrlConnection and I want to use mock data rather than server data. I have taken over a project from other people, I don't want to rewrite the whole thing so I'm kind of stuck with what's there. 
I'll see if I can use Mockito for what I want to do. Usually I'm very happy with Robolectric and with the way it handles httpClient. 

Roman Mazur

unread,
Jan 22, 2014, 3:19:27 AM1/22/14
to robol...@googlegroups.com
You might also try Robolectric.directlyOn set of methods.
Like Robolectric.directlyOn(realAPI, LoginApiImpl.class, "getTopChannels").invoke()

Mike Grafton

unread,
Jan 22, 2014, 10:54:59 AM1/22/14
to robol...@googlegroups.com
That's really interesting Christine, and it's too bad Robolectric doesn't support faking of HttpUrlConnection out of the box. Have you looked at (or thought about) what it would take to extend Robolectric in this manner? It would be a huge improvement.

Mike

Jake Wharton

unread,
Jan 22, 2014, 11:15:43 AM1/22/14
to robol...@googlegroups.com
On Wed, Jan 22, 2014 at 7:54 AM, Mike Grafton <mi...@pivotallabs.com> wrote:
That's really interesting Christine, and it's too bad Robolectric doesn't support faking of HttpUrlConnection out of the box. Have you looked at (or thought about) what it would take to extend Robolectric in this manner? It would be a huge improvement.

This should be pretty easy. Just call URL.setURLStreamHandlerFactory with a factory that delegates to the same crap that mocks the apache stuff.
Reply all
Reply to author
Forward
0 new messages