Unit testing login method
up vote
3
down vote
favorite
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
New contributor
add a comment |
up vote
3
down vote
favorite
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
New contributor
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
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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
New contributor
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
java android testing
New contributor
New contributor
edited 22 hours ago
New contributor
asked yesterday
JDoe
162
162
New contributor
New contributor
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
add a comment |
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
add a comment |
active
oldest
votes
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.
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.
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
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
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
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
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
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