Mockito throws NullpointerException when using mock
I am trying to create test cases for a web service, but I am getting a nullpointer exception. This is a web service:
@Path("friendservice")
public class FriendWebService {
private static final Logger logger = Logger.getLogger(FriendWebService.class);
@EJB
private FriendRequestServiceInterface friendRequestService;
@GET
@Path("friendrequest")
@Produces(MediaType.TEXT_PLAIN)
public String createFriendRequest(
@Context HttpServletRequest request) {
logger.info("createFriendRequest called");
String result = "false";
User user = (User) request.getSession().getAttribute("user");
User otherUser = (User) request.getSession().getAttribute("profileuser");
if ((user != null) && (otherUser != null)) {
logger.info("Got two users from session, creating friend request.");
if (friendRequestService.createFriendRequest(user, otherUser)) {
result = "true";
}
}
return result;
}
}
This is my test class:
public class FriendWebServiceTest {
@Mock
FriendRequestServiceInterface FriendRequestService;
@Mock
Logger mockedLogger = mock(Logger.class);
@Mock
HttpServletRequest mockedRequest = mock(HttpServletRequest.class);
@Mock
HttpSession mockedSession = mock(HttpSession.class);
@Mock
User mockedUser = mock(User.class);
@Mock
User mockedOtherUser = mock(User.class);
@InjectMocks
FriendWebService friendWebService = new FriendWebService();
@Before
public void setUp() throws Exception {
}
@Test
public void testCreateFriendRequest() throws Exception {
when(mockedRequest.getSession()).thenReturn(mockedSession);
when(mockedSession.getAttribute("user")).thenReturn(mockedUser);
when(mockedSession.getAttribute("profileuser")).thenReturn(mockedOtherUser);
when(FriendRequestService.createFriendRequest(mockedUser, mockedOtherUser)).thenReturn(true);
assertTrue(friendWebService.createFriendRequest(mockedRequest) == "true");
}
NullPointerException occurs when "when (FriendRequestService.createFriendRequest (mockedUser, mockedOtherUser)). ThenReturn (true);"
What am I doing wrong?
source to share
You are invoking invocation method calls on your mocked instance:
@Mock
HttpServletRequest mockedRequest = mock(HttpServletRequest.class);
First of all, you don't need to do both, either use annotation @Mock
or method mock
. This way you first assign a Mock and then replace that instance with a different mock. I recommend annotation as it adds some context to the layout, like the field name. This may have already called yours NullPointerException
, since you never activate annotations by calling:
MockitoAnnotations.initMocks(this);
since you are therefore not mocking all instances with both measures. However, even this will cause your exception to be thrown further, so let's move forward.
Internally, friendWebService.createFriendRequest(mockedRequest)
you call:
User user = (User) request.getSession().getAttribute("user");
User otherUser = (User) request.getSession().getAttribute("profileuser");
where you are calling a method on two mocks for which you have not specified any behavior. These mocks then default return null
. You need to specify behavior for this, for example:
when(request.getSession()).thenReturn(myMockedSession);
before making this chained call. Based on this, you can specify how to respond to calls on this mocked instance, such as returning custom mocks.
source to share