Need example of Robolectric with Mockito to write the unit test case
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
add a comment |
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
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
add a comment |
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
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
android unit-testing automated-tests mockito robolectric
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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:
(/test) Simple JUnit
(/test) JUnit + Mockito - classic unit testing in design patterns
(/test) AndroidJUnit (Robolectric) + Mockito without starting activity - unit testing that requires both android sdk instances and mocking
(/test) **AndroidJUnit (Robolectric) - starts activity, you test UI with espresso
- (/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.
"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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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:
(/test) Simple JUnit
(/test) JUnit + Mockito - classic unit testing in design patterns
(/test) AndroidJUnit (Robolectric) + Mockito without starting activity - unit testing that requires both android sdk instances and mocking
(/test) **AndroidJUnit (Robolectric) - starts activity, you test UI with espresso
- (/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.
"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
add a comment |
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:
(/test) Simple JUnit
(/test) JUnit + Mockito - classic unit testing in design patterns
(/test) AndroidJUnit (Robolectric) + Mockito without starting activity - unit testing that requires both android sdk instances and mocking
(/test) **AndroidJUnit (Robolectric) - starts activity, you test UI with espresso
- (/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.
"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
add a comment |
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:
(/test) Simple JUnit
(/test) JUnit + Mockito - classic unit testing in design patterns
(/test) AndroidJUnit (Robolectric) + Mockito without starting activity - unit testing that requires both android sdk instances and mocking
(/test) **AndroidJUnit (Robolectric) - starts activity, you test UI with espresso
- (/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.
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:
(/test) Simple JUnit
(/test) JUnit + Mockito - classic unit testing in design patterns
(/test) AndroidJUnit (Robolectric) + Mockito without starting activity - unit testing that requires both android sdk instances and mocking
(/test) **AndroidJUnit (Robolectric) - starts activity, you test UI with espresso
- (/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.
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
add a comment |
"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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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