Unit testing login method









up vote
3
down vote

favorite
2












I am developing an Android application where testing is part of development process. Testing in general is new domain for me therefore, i struggle often. I need an advice on unit testing a method which I use to perform a user login. Since method is void and implements callbacks I assume that either class or some of the objects should be mocked. In my application I use JUnit, Robolectric and Mockito for testing purposes.



I was able to perform a test to check if method with its parameters is being called but I am curious(dont know) how can I test the onResponse & onErrorResponse callbacks. I have provided the code below. Thanks in advance.



 public void loginUser(String email,String pass)
try
requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
StringRequest request = new StringRequest(Request.Method.POST, Url+"/checkUser.php", new Response.Listener<String>()
@Override
public void onResponse(String response)

if(response.contains("present"))
Toast.makeText(getActivity(), "Welcome", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);

else
Toast.makeText(getActivity(), response, Toast.LENGTH_SHORT).show();


, new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();

)
@Override
protected Map<String, String> getParams() throws AuthFailureError
Map<String,String> parameters = new HashMap<String,String>();
parameters.put("email",email);
parameters.put("password",pass);

return parameters;

;
requestQueue.add(request);
catch (Exception o)Log.w("TAG",o.getMessage());
//loginUser



@Test
public void testLoginCall() throws Exception
loginFragment = Mockito.mock(LoginFragment.class);
doNothing().when(loginFragment).loginUser("email","pass");
loginFragment.loginUser("email","pass");
verify(loginFragment,times(1)).loginUser("email","pass");










share|improve this question









New contributor




JDoe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    How did you try to test this so far and what does not work about it? Whcih aspect do you actually want to test about this method? Please add details / source code.
    – Selaron
    yesterday











  • @Selaron I have added some info
    – JDoe
    yesterday










  • You can group under a function your response code and call it by your test function.
    – Saret
    yesterday











  • @Saret I have an understanding of what are you saying but I am was not able to implement it...
    – JDoe
    yesterday














up vote
3
down vote

favorite
2












I am developing an Android application where testing is part of development process. Testing in general is new domain for me therefore, i struggle often. I need an advice on unit testing a method which I use to perform a user login. Since method is void and implements callbacks I assume that either class or some of the objects should be mocked. In my application I use JUnit, Robolectric and Mockito for testing purposes.



I was able to perform a test to check if method with its parameters is being called but I am curious(dont know) how can I test the onResponse & onErrorResponse callbacks. I have provided the code below. Thanks in advance.



 public void loginUser(String email,String pass)
try
requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
StringRequest request = new StringRequest(Request.Method.POST, Url+"/checkUser.php", new Response.Listener<String>()
@Override
public void onResponse(String response)

if(response.contains("present"))
Toast.makeText(getActivity(), "Welcome", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);

else
Toast.makeText(getActivity(), response, Toast.LENGTH_SHORT).show();


, new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();

)
@Override
protected Map<String, String> getParams() throws AuthFailureError
Map<String,String> parameters = new HashMap<String,String>();
parameters.put("email",email);
parameters.put("password",pass);

return parameters;

;
requestQueue.add(request);
catch (Exception o)Log.w("TAG",o.getMessage());
//loginUser



@Test
public void testLoginCall() throws Exception
loginFragment = Mockito.mock(LoginFragment.class);
doNothing().when(loginFragment).loginUser("email","pass");
loginFragment.loginUser("email","pass");
verify(loginFragment,times(1)).loginUser("email","pass");










share|improve this question









New contributor




JDoe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    How did you try to test this so far and what does not work about it? Whcih aspect do you actually want to test about this method? Please add details / source code.
    – Selaron
    yesterday











  • @Selaron I have added some info
    – JDoe
    yesterday










  • You can group under a function your response code and call it by your test function.
    – Saret
    yesterday











  • @Saret I have an understanding of what are you saying but I am was not able to implement it...
    – JDoe
    yesterday












up vote
3
down vote

favorite
2









up vote
3
down vote

favorite
2






2





I am developing an Android application where testing is part of development process. Testing in general is new domain for me therefore, i struggle often. I need an advice on unit testing a method which I use to perform a user login. Since method is void and implements callbacks I assume that either class or some of the objects should be mocked. In my application I use JUnit, Robolectric and Mockito for testing purposes.



I was able to perform a test to check if method with its parameters is being called but I am curious(dont know) how can I test the onResponse & onErrorResponse callbacks. I have provided the code below. Thanks in advance.



 public void loginUser(String email,String pass)
try
requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
StringRequest request = new StringRequest(Request.Method.POST, Url+"/checkUser.php", new Response.Listener<String>()
@Override
public void onResponse(String response)

if(response.contains("present"))
Toast.makeText(getActivity(), "Welcome", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);

else
Toast.makeText(getActivity(), response, Toast.LENGTH_SHORT).show();


, new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();

)
@Override
protected Map<String, String> getParams() throws AuthFailureError
Map<String,String> parameters = new HashMap<String,String>();
parameters.put("email",email);
parameters.put("password",pass);

return parameters;

;
requestQueue.add(request);
catch (Exception o)Log.w("TAG",o.getMessage());
//loginUser



@Test
public void testLoginCall() throws Exception
loginFragment = Mockito.mock(LoginFragment.class);
doNothing().when(loginFragment).loginUser("email","pass");
loginFragment.loginUser("email","pass");
verify(loginFragment,times(1)).loginUser("email","pass");










share|improve this question









New contributor




JDoe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am developing an Android application where testing is part of development process. Testing in general is new domain for me therefore, i struggle often. I need an advice on unit testing a method which I use to perform a user login. Since method is void and implements callbacks I assume that either class or some of the objects should be mocked. In my application I use JUnit, Robolectric and Mockito for testing purposes.



I was able to perform a test to check if method with its parameters is being called but I am curious(dont know) how can I test the onResponse & onErrorResponse callbacks. I have provided the code below. Thanks in advance.



 public void loginUser(String email,String pass)
try
requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
StringRequest request = new StringRequest(Request.Method.POST, Url+"/checkUser.php", new Response.Listener<String>()
@Override
public void onResponse(String response)

if(response.contains("present"))
Toast.makeText(getActivity(), "Welcome", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);

else
Toast.makeText(getActivity(), response, Toast.LENGTH_SHORT).show();


, new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();

)
@Override
protected Map<String, String> getParams() throws AuthFailureError
Map<String,String> parameters = new HashMap<String,String>();
parameters.put("email",email);
parameters.put("password",pass);

return parameters;

;
requestQueue.add(request);
catch (Exception o)Log.w("TAG",o.getMessage());
//loginUser



@Test
public void testLoginCall() throws Exception
loginFragment = Mockito.mock(LoginFragment.class);
doNothing().when(loginFragment).loginUser("email","pass");
loginFragment.loginUser("email","pass");
verify(loginFragment,times(1)).loginUser("email","pass");







java android testing






share|improve this question









New contributor




JDoe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




JDoe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 22 hours ago





















New contributor




JDoe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









JDoe

162




162




New contributor




JDoe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





JDoe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






JDoe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 1




    How did you try to test this so far and what does not work about it? Whcih aspect do you actually want to test about this method? Please add details / source code.
    – Selaron
    yesterday











  • @Selaron I have added some info
    – JDoe
    yesterday










  • You can group under a function your response code and call it by your test function.
    – Saret
    yesterday











  • @Saret I have an understanding of what are you saying but I am was not able to implement it...
    – JDoe
    yesterday












  • 1




    How did you try to test this so far and what does not work about it? Whcih aspect do you actually want to test about this method? Please add details / source code.
    – Selaron
    yesterday











  • @Selaron I have added some info
    – JDoe
    yesterday










  • You can group under a function your response code and call it by your test function.
    – Saret
    yesterday











  • @Saret I have an understanding of what are you saying but I am was not able to implement it...
    – JDoe
    yesterday







1




1




How did you try to test this so far and what does not work about it? Whcih aspect do you actually want to test about this method? Please add details / source code.
– Selaron
yesterday





How did you try to test this so far and what does not work about it? Whcih aspect do you actually want to test about this method? Please add details / source code.
– Selaron
yesterday













@Selaron I have added some info
– JDoe
yesterday




@Selaron I have added some info
– JDoe
yesterday












You can group under a function your response code and call it by your test function.
– Saret
yesterday





You can group under a function your response code and call it by your test function.
– Saret
yesterday













@Saret I have an understanding of what are you saying but I am was not able to implement it...
– JDoe
yesterday




@Saret I have an understanding of what are you saying but I am was not able to implement it...
– JDoe
yesterday

















active

oldest

votes











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',
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
);



);






JDoe is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53210228%2funit-testing-login-method%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes








JDoe is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















JDoe is a new contributor. Be nice, and check out our Code of Conduct.












JDoe is a new contributor. Be nice, and check out our Code of Conduct.











JDoe is a new contributor. Be nice, and check out our Code of Conduct.













 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53210228%2funit-testing-login-method%23new-answer', 'question_page');

);

Post as a guest














































































這個網誌中的熱門文章

Is there any way to eliminate the singular point to solve this integral by hand or by approximations?

Why am i infinitely getting the same tweet with the Twitter Search API?

Solve: $(3xy-2ay^2)dx+(x^2-2axy)dy=0$