-1

I am new to Junit test cases can somebody please me with the below.

I need to write a junit test case for user password reset. I am not sure how can i use assert here:

public @ResponseBody userInfo updatePassword(@RequestBody Map<String, String> userLookup, HttpServletRequest request) {
        userInfo userInfo = new userInfo();
        try {
            Users user = Service.getUserDetailsByUsername(userLookup.get("username"));
            user.setPassword(userLookup.get("newPassword"));
            Service.updateUserPw(user);
            logger.debug("Password updated successfully");
        } catch (Exception ex) {
            logger.debug("Error while updating user password!", ex);
        }
        return userInfo;
    }



    method:getUserDetailsByUsername returns a user with the username passed.
    method:setPassword returns return void
    method: updateUserPw returns void 
fiddle
  • 1,095
  • 5
  • 18
  • 33
  • You are trying to assert on void returning methods, would request you to check this link http://stackoverflow.com/questions/16043819/junit-testing-void-methods – Cleonjoys Jul 12 '15 at 17:44
  • given this is a controller are you trying to test this via spring mvc? meaning via "http"? or do you want a unit test that leaves http out of it? – xenoterracide Jul 12 '15 at 17:46
  • i could leave http out of it. – fiddle Jul 12 '15 at 17:48

1 Answers1

0

To write a JUnit test, you must first create and fill all the objects and parameters needed to the call:

@Test
public void updatePasswordWithAValidPassword()
{
    MyObject my=new MyObject();
    Map<String, String> userLookup=new HashMap<String, String>();
    userLookup.put("username", "...");
    userLookup.put("newPassword", "january209");
    HttpServletRequest request=null; // Fortunately, this method does not use the request parameter.

Second, perform the call:

    userInfo user=my.updatePassword(userLookup, request);

And last, check the results using Assert.assert* and Assert.fail:

    Assert.assertEquals("january209", user.getPassword());
}

In order to reach a high code coverage, you should write a test for each possible alternative way your method could have at execution. In this case, there should be a test for changing the password sucessfully (the example above), and other test that produce an exception while changing the password (maybe introducing a null password, or a password with low level of security - too short, etc). But, unfortunately, given the code as it is, it is not easy for the caller (=the JUnit tester) to know when there has been an exception, because it is systematically catched and omitted to the caller.

If you want to write such a test, you must first refactorize your method removing the catch (Exception e) and letting the exceptions be propagated to the caller (assuming there is specific exception to each situation: PasswordTooShortException, InvalidNullPasswordException, etc), so you can catch it from the tester.

Little Santi
  • 8,563
  • 2
  • 18
  • 46
  • Hi Little Santi - could you please help me understand your code.Thanks In advance . – fiddle Jul 12 '15 at 21:27
  • 1
    The code I put above is an example on how to test your method updatePassword. You need to enclose it into a class typically named *Test and run it with JUnit. – Little Santi Jul 12 '15 at 21:37
  • i have trouble understanding: userInfo user=my.updatePassword(userLookup, request); – fiddle Jul 12 '15 at 21:48
  • That is the line that invokes your method updatePassword, passing the proper parameters and receiving the result into a userInfo object, as it is the sign of the code you have included in your question. – Little Santi Jul 12 '15 at 23:37