Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
how to mock return value in robolectric
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Vipin Aravind  
View profile  
 More options Sep 30 2012, 8:16 am
From: Vipin Aravind <vipina...@gmail.com>
Date: Sun, 30 Sep 2012 05:16:16 -0700 (PDT)
Local: Sun, Sep 30 2012 8:16 am
Subject: how to mock return value in robolectric

Hi,
     I  have  some  code  like  this:-
        bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,
RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING); Is there a way to mock the
return value of getMinBufferSize using robolectric. Thanks


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tyler Schultz  
View profile  
 More options Sep 30 2012, 10:04 am
From: Tyler Schultz <tylerschu...@gmail.com>
Date: Sun, 30 Sep 2012 07:04:35 -0700
Local: Sun, Sep 30 2012 10:04 am
Subject: Re: [robolectric] how to mock return value in robolectric

It appears that no one has created a shadow for AudioRecord yet, so you'll
have to start there.

--Tyler


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Vipin Aravind  
View profile  
 More options Sep 30 2012, 11:45 am
From: Vipin Aravind <vipina...@gmail.com>
Date: Sun, 30 Sep 2012 08:45:28 -0700 (PDT)
Local: Sun, Sep 30 2012 11:45 am
Subject: Re: [robolectric] how to mock return value in robolectric

Assume  there  is a  shadow  object.  let  us  take  MediaRecorder  for
 example.
How  do  you  tell  Robo  that  I  want  certain  method to  return  a
 value  like  we  do  with  mockito and  other  mock  frameworks.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tyler Schultz  
View profile  
 More options Sep 30 2012, 2:10 pm
From: Tyler Schultz <tylerschu...@gmail.com>
Date: Sun, 30 Sep 2012 11:10:14 -0700
Local: Sun, Sep 30 2012 2:10 pm
Subject: Re: [robolectric] how to mock return value in robolectric

If you'd like to do mockito style mocking, you should use that, Robolectric
should play fine.  Me personally? I'm not a fan of that style of testing,
but that's just my opinion.

Let's take the MediaRecorder.getAudioSourceMax() as an example. A pattern
we very often use is to put a setter method on the shadow class to set
values that will be returned.

@Implements(MediaRecorder.class)
public class ShadowMediaRecorder {

    ...

    private static int audioSourceMax;

    @Implementation
    public static int getAudioSourceMax() {
        return audioSourceMax;
    }

    /* non-Android setter */
    public static void setAudioSourceMax(int audioSourceMax) {
        this.audioSourceMax = audioSourceMax;
    }

    ...

}

You'd need to set the value to be returned in your test's setup by invoking
the method on the shadow.

@RunWith(WithTestDefaultsRunner.class)
public class MediaRecorderTest {
...
    public void setUp() {
        ShadowMediaRecorder.setAudioSourceMax(42);
    }

    public void testMaxAudio() {
       assertThat(MediaRecorder.getAudioSourceMax(), equalTo(42));
    }

}

One last thing to note here is that we're using the static. This will cause
test pollution, so you need to do teardown.  We make a point to do this
type of cleanup in one place:

public class Robolectric {
    ...
    public static void resetStaticState() {
        ...
        ShadowMediaRecorder.reset();
    }
    ...

}

--Tyler


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gal Ben-Haim  
View profile  
 More options Oct 1 2012, 4:12 am
From: Gal Ben-Haim <gbenh...@gmail.com>
Date: Mon, 1 Oct 2012 01:12:31 -0700 (PDT)
Local: Mon, Oct 1 2012 4:12 am
Subject: Re: [robolectric] how to mock return value in robolectric

what's so different between this approach and the Mockito approach for this
scenario ?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Levi  
View profile  
 More options Oct 1 2012, 7:48 am
From: Levi <l...@leviwilson.com>
Date: Mon, 1 Oct 2012 04:48:09 -0700 (PDT)
Local: Mon, Oct 1 2012 7:48 am
Subject: Re: [robolectric] how to mock return value in robolectric

I believe what Tyler is referring to (please correct me if I'm wrong) is the difference between classicists and mockists as described in Martin Fowler's article "Mocks Aren't Stubs" (http://martinfowler.com/articles/mocksArentStubs.html).  I'm not sure that either approach is wrong, but it's important to know the difference and to be aware of the pitfalls of each style.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tyler Schultz  
View profile  
 More options Oct 1 2012, 10:10 am
From: Tyler Schultz <tylerschu...@gmail.com>
Date: Mon, 1 Oct 2012 07:10:14 -0700
Local: Mon, Oct 1 2012 10:10 am
Subject: Re: [robolectric] how to mock return value in robolectric

Robolectric clouds this discussion. It's doing all sorts of whacky stuff
(bytecode manipulation, resource parsing, to name a few...) under the
covers making your tests run slower than they might otherwise. One of my
complaints about easy mock and mockito is the slow speed at which the tests
run, but tests written with Robolectric are fairly heavy weight themselves.
I will say that if you are using Robolectric that mocking Android will only
make your tests run slower.

One argument I like to make against mocking is the setup. Setup in
Robolectric is not simple, but once it's done, all other users of
Robolectric have the setup that I've created (reuse). I've not seen a
codebase where the use of mocking framework mocks are shared in this way
(anecdote alert!). I have a harder time coming up to speed on a given test
where mocks are used. I feel like Robolectric helps by adding a separation
of concern - faking out Android (Robolectric) and testing the application's
behavior (your application test).

--Tyler


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »