This question already has an answer here:
public class UserTest {
private User user;
private Account account;
private Customer customer;
private UserService userService=new UserService();
private CustomerService customerService=new CustomerService();
private EmployeeService employeeService=new EmployeeService();
private SessionFactory sessionFactory;
private Session session;
@Before
public void setUp() throws Exception
{
sessionFactory=Mockito.mock(SessionFactory.class);
session=Mockito.mock(Session.class);
}
@Test
public void shouldNotValidateWrongPassword()
{
String newPassword="xyz";
user = new Customer(1,"",null,' ',' ',"dgh",' ',"");
field("sessionFactory").ofType(SessionFactory.class).in(userService).set(sessionFactory);
Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);
Mockito.when(session.get(Customer.class, 1)).thenReturn(user);
assertNotSame(userService.validate(1, newPassword), false);
}
@Test
public void testgetUser()
{
user = new Customer(1,"",null,' ',' ',"hello",' ',"");
field("sessionFactory").ofType(SessionFactory.class).in(customerService).set(sessionFactory);
Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session);
Mockito.when(session.get(User.class, 1)).thenReturn(user);
assertNotNull(employeeService.getUser(1));
}
Methods for which I am writing test cases is as follows:
@Transactional
public boolean validate(int username, String password) {
User user = getUser(username);
System.out.println(username + password);
return user.validate(password);
}
@Transactional
public User getUser(int userId) {
System.out.println("Employee service called!");
User user = (User) sessionFactory.getCurrentSession().get(User.class, userId);
return user;
}
I have these two cases...for which I am getting null pointer Exception.I am very new to write test cases.please anyone help to figure out where it goes wrong?
Aucun commentaire:
Enregistrer un commentaire