public class EmployeeService {
@Autowired
private EmployeeDao employeeDao;
public EmployeeService(EmployeeDao employeeDao){
this.employeeDao=employeeDao;
}
public void register(Employee employee) throws Exception {
validateEmployee(employee);
employeeDao.register(employee);
}
public boolean employeeExist(Employee employee) {
return employeeDao.employeeExist(employee);
}
public List<Employee> findAllEmployee() {
return employeeDao.findAllEmployee();
}
public List<Employee> findEmployeeUsingCriteia() {
return employeeDao.findEmployeeUsingCriteia();
}
public void deleteEmployee(int employeeId ) {
employeeDao.deleteEmployee(employeeId);
}
private void validateEmployee(Employee employee) throws Exception{
if(employeeDao.isUserNameExist(employee.getUsername())){
throw new Exception();
}
}
}
package com.bebo.service;
import junit.framework.Assert;
import org.junit.*;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import com.bebo.dao.EmployeeDao;
import com.bebo.model.Employee;
/*
* @author Anil.Thakur
*/
@RunWith(MockitoJUnitRunner.class)
public class EmployeeServiceTest {
@Mock
private EmployeeDao employeeDao;
private EmployeeService employeeService;
@Before
public void setUp(){
employeeService = new EmployeeService(employeeDao);
Mockito.when(employeeDao.employeeExist(Matchers.any(Employee.class))).thenReturn(true);
}
@Test
public void testEmployeeExist() {
Employee employee =new Employee();
employee.setAge(25);
boolean result = employeeService.employeeExist(employee);
Assert.assertEquals(true, result);
}
}
No comments:
Post a Comment