Need example of Robolectric with Mockito to write the unit test case










0















How to use with Mocking (Mockito) with Robolectric can any one suggest me and i want to write the unit test cases for an API call also, i try with Robolectric but it only runs the label and actions but for API calls how to write the unit test in Android Test package,Please help me out










share|improve this question
























  • Welcome to StackOverflow! Unfortunately, I don't think your question is a good match for this site; the question is too broad to be answered here, and it's considered off-topic to find external tutorials or examples. See more about what's on topic at StackOverflow here. Good luck with your testing!

    – Jeff Bowman
    Sep 24 '18 at 17:24















0















How to use with Mocking (Mockito) with Robolectric can any one suggest me and i want to write the unit test cases for an API call also, i try with Robolectric but it only runs the label and actions but for API calls how to write the unit test in Android Test package,Please help me out










share|improve this question
























  • Welcome to StackOverflow! Unfortunately, I don't think your question is a good match for this site; the question is too broad to be answered here, and it's considered off-topic to find external tutorials or examples. See more about what's on topic at StackOverflow here. Good luck with your testing!

    – Jeff Bowman
    Sep 24 '18 at 17:24













0












0








0


0






How to use with Mocking (Mockito) with Robolectric can any one suggest me and i want to write the unit test cases for an API call also, i try with Robolectric but it only runs the label and actions but for API calls how to write the unit test in Android Test package,Please help me out










share|improve this question
















How to use with Mocking (Mockito) with Robolectric can any one suggest me and i want to write the unit test cases for an API call also, i try with Robolectric but it only runs the label and actions but for API calls how to write the unit test in Android Test package,Please help me out







android unit-testing automated-tests mockito robolectric






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 25 '18 at 4:35







user3733080

















asked Sep 24 '18 at 7:24









user3733080user3733080

53




53












  • Welcome to StackOverflow! Unfortunately, I don't think your question is a good match for this site; the question is too broad to be answered here, and it's considered off-topic to find external tutorials or examples. See more about what's on topic at StackOverflow here. Good luck with your testing!

    – Jeff Bowman
    Sep 24 '18 at 17:24

















  • Welcome to StackOverflow! Unfortunately, I don't think your question is a good match for this site; the question is too broad to be answered here, and it's considered off-topic to find external tutorials or examples. See more about what's on topic at StackOverflow here. Good luck with your testing!

    – Jeff Bowman
    Sep 24 '18 at 17:24
















Welcome to StackOverflow! Unfortunately, I don't think your question is a good match for this site; the question is too broad to be answered here, and it's considered off-topic to find external tutorials or examples. See more about what's on topic at StackOverflow here. Good luck with your testing!

– Jeff Bowman
Sep 24 '18 at 17:24





Welcome to StackOverflow! Unfortunately, I don't think your question is a good match for this site; the question is too broad to be answered here, and it's considered off-topic to find external tutorials or examples. See more about what's on topic at StackOverflow here. Good luck with your testing!

– Jeff Bowman
Sep 24 '18 at 17:24












1 Answer
1






active

oldest

votes


















2














you don't combine Mockito with Robolectric, as far as I understand testing on Android.



You use Robolectric in order to not having to test your instrumented tests on emulator or physical device, instead, you test it on JVM. That is Robolectric. This allows you also to get access to android framework, and is slower.



Mockito, on the other hand, is a library that allows you to :mock: your dependencies inside a unit test. Unit test's purpose is to test the behaviour of the specific software entity, concretely its logic, that you wish to test. Since it don't have access to android framework (it don't need that), it is quite faster.



Edit:



It's actually more complicated. You can have Robolectric together with Mockito (by introducing Mockito rule as a Runner - you have 2 runners then, one @RunWith(AndroidJunit4) - robolectric one, with MockitoRule). And you can just unit test something that needs access to android SDK with Robolectric without actually starting an activity... So these are test configurations I can come up with on Android:



  1. (/test) Simple JUnit


  2. (/test) JUnit + Mockito - classic unit testing in design patterns


  3. (/test) AndroidJUnit (Robolectric) + Mockito without starting activity - unit testing that requires both android sdk instances and mocking



  4. (/test) **AndroidJUnit (Robolectric) - starts activity, you test UI with espresso



    1. (/instrumentedTest) AndroidJUnit - the same thing, test ui with espresso


Or you can also have sharedTest. Just one folder for both. With this you can maybe achieve an option to run one test both on device and on VM, I'm just not sure how. Maybe Nitrogen will introduce this option in the future.
https://blog.danlew.net/2015/11/02/sharing-code-between-unit-tests-and-instrumentation-tests-on-android/



The ways you can set up your test environment for particular class are many nowadays. I hope that project Nitrogen will simplify this decision making and provide a good documentation on this.






share|improve this answer

























  • "and is slower" AND HOW.

    – jdv
    Nov 14 '18 at 17:38











  • @Stanislav Kinzl Has Android Nitrogen launched under a different project name as I don't see any mention in the testing documentation? developer.android.com/training/testing

    – Adam Hurwitz
    Mar 12 at 20:06







  • 1





    @AdamHurwitz check this newsvideo.su/tech/video/272257 I don't think it launched yet

    – Stanislav Kinzl
    2 days ago










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52474455%2fneed-example-of-robolectric-with-mockito-to-write-the-unit-test-case%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














you don't combine Mockito with Robolectric, as far as I understand testing on Android.



You use Robolectric in order to not having to test your instrumented tests on emulator or physical device, instead, you test it on JVM. That is Robolectric. This allows you also to get access to android framework, and is slower.



Mockito, on the other hand, is a library that allows you to :mock: your dependencies inside a unit test. Unit test's purpose is to test the behaviour of the specific software entity, concretely its logic, that you wish to test. Since it don't have access to android framework (it don't need that), it is quite faster.



Edit:



It's actually more complicated. You can have Robolectric together with Mockito (by introducing Mockito rule as a Runner - you have 2 runners then, one @RunWith(AndroidJunit4) - robolectric one, with MockitoRule). And you can just unit test something that needs access to android SDK with Robolectric without actually starting an activity... So these are test configurations I can come up with on Android:



  1. (/test) Simple JUnit


  2. (/test) JUnit + Mockito - classic unit testing in design patterns


  3. (/test) AndroidJUnit (Robolectric) + Mockito without starting activity - unit testing that requires both android sdk instances and mocking



  4. (/test) **AndroidJUnit (Robolectric) - starts activity, you test UI with espresso



    1. (/instrumentedTest) AndroidJUnit - the same thing, test ui with espresso


Or you can also have sharedTest. Just one folder for both. With this you can maybe achieve an option to run one test both on device and on VM, I'm just not sure how. Maybe Nitrogen will introduce this option in the future.
https://blog.danlew.net/2015/11/02/sharing-code-between-unit-tests-and-instrumentation-tests-on-android/



The ways you can set up your test environment for particular class are many nowadays. I hope that project Nitrogen will simplify this decision making and provide a good documentation on this.






share|improve this answer

























  • "and is slower" AND HOW.

    – jdv
    Nov 14 '18 at 17:38











  • @Stanislav Kinzl Has Android Nitrogen launched under a different project name as I don't see any mention in the testing documentation? developer.android.com/training/testing

    – Adam Hurwitz
    Mar 12 at 20:06







  • 1





    @AdamHurwitz check this newsvideo.su/tech/video/272257 I don't think it launched yet

    – Stanislav Kinzl
    2 days ago















2














you don't combine Mockito with Robolectric, as far as I understand testing on Android.



You use Robolectric in order to not having to test your instrumented tests on emulator or physical device, instead, you test it on JVM. That is Robolectric. This allows you also to get access to android framework, and is slower.



Mockito, on the other hand, is a library that allows you to :mock: your dependencies inside a unit test. Unit test's purpose is to test the behaviour of the specific software entity, concretely its logic, that you wish to test. Since it don't have access to android framework (it don't need that), it is quite faster.



Edit:



It's actually more complicated. You can have Robolectric together with Mockito (by introducing Mockito rule as a Runner - you have 2 runners then, one @RunWith(AndroidJunit4) - robolectric one, with MockitoRule). And you can just unit test something that needs access to android SDK with Robolectric without actually starting an activity... So these are test configurations I can come up with on Android:



  1. (/test) Simple JUnit


  2. (/test) JUnit + Mockito - classic unit testing in design patterns


  3. (/test) AndroidJUnit (Robolectric) + Mockito without starting activity - unit testing that requires both android sdk instances and mocking



  4. (/test) **AndroidJUnit (Robolectric) - starts activity, you test UI with espresso



    1. (/instrumentedTest) AndroidJUnit - the same thing, test ui with espresso


Or you can also have sharedTest. Just one folder for both. With this you can maybe achieve an option to run one test both on device and on VM, I'm just not sure how. Maybe Nitrogen will introduce this option in the future.
https://blog.danlew.net/2015/11/02/sharing-code-between-unit-tests-and-instrumentation-tests-on-android/



The ways you can set up your test environment for particular class are many nowadays. I hope that project Nitrogen will simplify this decision making and provide a good documentation on this.






share|improve this answer

























  • "and is slower" AND HOW.

    – jdv
    Nov 14 '18 at 17:38











  • @Stanislav Kinzl Has Android Nitrogen launched under a different project name as I don't see any mention in the testing documentation? developer.android.com/training/testing

    – Adam Hurwitz
    Mar 12 at 20:06







  • 1





    @AdamHurwitz check this newsvideo.su/tech/video/272257 I don't think it launched yet

    – Stanislav Kinzl
    2 days ago













2












2








2







you don't combine Mockito with Robolectric, as far as I understand testing on Android.



You use Robolectric in order to not having to test your instrumented tests on emulator or physical device, instead, you test it on JVM. That is Robolectric. This allows you also to get access to android framework, and is slower.



Mockito, on the other hand, is a library that allows you to :mock: your dependencies inside a unit test. Unit test's purpose is to test the behaviour of the specific software entity, concretely its logic, that you wish to test. Since it don't have access to android framework (it don't need that), it is quite faster.



Edit:



It's actually more complicated. You can have Robolectric together with Mockito (by introducing Mockito rule as a Runner - you have 2 runners then, one @RunWith(AndroidJunit4) - robolectric one, with MockitoRule). And you can just unit test something that needs access to android SDK with Robolectric without actually starting an activity... So these are test configurations I can come up with on Android:



  1. (/test) Simple JUnit


  2. (/test) JUnit + Mockito - classic unit testing in design patterns


  3. (/test) AndroidJUnit (Robolectric) + Mockito without starting activity - unit testing that requires both android sdk instances and mocking



  4. (/test) **AndroidJUnit (Robolectric) - starts activity, you test UI with espresso



    1. (/instrumentedTest) AndroidJUnit - the same thing, test ui with espresso


Or you can also have sharedTest. Just one folder for both. With this you can maybe achieve an option to run one test both on device and on VM, I'm just not sure how. Maybe Nitrogen will introduce this option in the future.
https://blog.danlew.net/2015/11/02/sharing-code-between-unit-tests-and-instrumentation-tests-on-android/



The ways you can set up your test environment for particular class are many nowadays. I hope that project Nitrogen will simplify this decision making and provide a good documentation on this.






share|improve this answer















you don't combine Mockito with Robolectric, as far as I understand testing on Android.



You use Robolectric in order to not having to test your instrumented tests on emulator or physical device, instead, you test it on JVM. That is Robolectric. This allows you also to get access to android framework, and is slower.



Mockito, on the other hand, is a library that allows you to :mock: your dependencies inside a unit test. Unit test's purpose is to test the behaviour of the specific software entity, concretely its logic, that you wish to test. Since it don't have access to android framework (it don't need that), it is quite faster.



Edit:



It's actually more complicated. You can have Robolectric together with Mockito (by introducing Mockito rule as a Runner - you have 2 runners then, one @RunWith(AndroidJunit4) - robolectric one, with MockitoRule). And you can just unit test something that needs access to android SDK with Robolectric without actually starting an activity... So these are test configurations I can come up with on Android:



  1. (/test) Simple JUnit


  2. (/test) JUnit + Mockito - classic unit testing in design patterns


  3. (/test) AndroidJUnit (Robolectric) + Mockito without starting activity - unit testing that requires both android sdk instances and mocking



  4. (/test) **AndroidJUnit (Robolectric) - starts activity, you test UI with espresso



    1. (/instrumentedTest) AndroidJUnit - the same thing, test ui with espresso


Or you can also have sharedTest. Just one folder for both. With this you can maybe achieve an option to run one test both on device and on VM, I'm just not sure how. Maybe Nitrogen will introduce this option in the future.
https://blog.danlew.net/2015/11/02/sharing-code-between-unit-tests-and-instrumentation-tests-on-android/



The ways you can set up your test environment for particular class are many nowadays. I hope that project Nitrogen will simplify this decision making and provide a good documentation on this.







share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 11 at 11:49

























answered Nov 14 '18 at 16:43









Stanislav KinzlStanislav Kinzl

464




464












  • "and is slower" AND HOW.

    – jdv
    Nov 14 '18 at 17:38











  • @Stanislav Kinzl Has Android Nitrogen launched under a different project name as I don't see any mention in the testing documentation? developer.android.com/training/testing

    – Adam Hurwitz
    Mar 12 at 20:06







  • 1





    @AdamHurwitz check this newsvideo.su/tech/video/272257 I don't think it launched yet

    – Stanislav Kinzl
    2 days ago

















  • "and is slower" AND HOW.

    – jdv
    Nov 14 '18 at 17:38











  • @Stanislav Kinzl Has Android Nitrogen launched under a different project name as I don't see any mention in the testing documentation? developer.android.com/training/testing

    – Adam Hurwitz
    Mar 12 at 20:06







  • 1





    @AdamHurwitz check this newsvideo.su/tech/video/272257 I don't think it launched yet

    – Stanislav Kinzl
    2 days ago
















"and is slower" AND HOW.

– jdv
Nov 14 '18 at 17:38





"and is slower" AND HOW.

– jdv
Nov 14 '18 at 17:38













@Stanislav Kinzl Has Android Nitrogen launched under a different project name as I don't see any mention in the testing documentation? developer.android.com/training/testing

– Adam Hurwitz
Mar 12 at 20:06






@Stanislav Kinzl Has Android Nitrogen launched under a different project name as I don't see any mention in the testing documentation? developer.android.com/training/testing

– Adam Hurwitz
Mar 12 at 20:06





1




1





@AdamHurwitz check this newsvideo.su/tech/video/272257 I don't think it launched yet

– Stanislav Kinzl
2 days ago





@AdamHurwitz check this newsvideo.su/tech/video/272257 I don't think it launched yet

– Stanislav Kinzl
2 days ago



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52474455%2fneed-example-of-robolectric-with-mockito-to-write-the-unit-test-case%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Darth Vader #20

Ondo