mardi 16 juillet 2019

How to replace dao method by mockito

I have a problem when i want to change dao fuction in method 'when' in mockito.

There always want get connect, and I get an error javax.naming.NoInitialContextException, where DAO class trying to connect with dataSource and get environment context.

public class OrderingMainActionTest extends Mockito {

@Mock
HttpSession mockedSession = mock(HttpSession.class);

@Mock
OrderingMainAction applicationServiceBean;

@Mock
InitialContext ic;

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
}

@Test
public void orderingActionByUserTest() throws ServletException, IOException, AppException, SQLException {
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    OrderingMainAction ordering = new OrderingMainAction();
    UserDao userDao = mock(UserDao.class);

    when(request.getSession()).thenReturn(mockedSession);

    User user = new User();
    user.setId(1);

    when(mockedSession.getAttribute("user")).thenReturn(user);
    when(request.getParameter("checkedSeats")).thenReturn("1");
    when(userDao.getFullNameByUserId(anyInt())).thenReturn("test test");

    assertEquals(ordering.execute(request,response),"/login");
}

}

Aucun commentaire:

Enregistrer un commentaire