mercredi 27 avril 2016

Expecting custom exception instead of null pointer exception while junit testing

I have method in java class :

@Context
UriInfo uriInfo;
public void processRequest(@QueryParam ("userId") @DefaultValue("") String userId)
{
     String baseURI = uriInfo.getBaseUri().toString();
     if(userId == null)
     {
         //UserIdNotFoundException is my custom exception which extends Exceptition
         throw new UserIdNotFoundException();
     }
 }

When I'm junit testing the above method expecting for UserIdNotFoundException when userId parameter is Null, I get the following Assertion error : expected an instance of UserIdNotFoundException but <java.lang.NullPointerException> is java.lang.NullPointerException.

@Test
public void testProcessRequest_throws_UserIdNotFoundException()
{
     expectedException.expect(UserIdNotFoundException.class);
     processRequest(null);
}

My custom exception class :

public class UserIdNotFoundException extends Exception
{

     public UserIdNotFoundException()
     {

     }

     public UserIdNotFoundException(String message)
     {
          super(message);
     }
}

Aucun commentaire:

Enregistrer un commentaire