dimanche 27 septembre 2015

Mocked object not getting in my service?

Mocked object not getting in service Here is my below code

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classesExternalProviderMain.class)
@ActiveProfiles(ApplicationConstants.DEVELOPMENT_PROFILE)
@WebAppConfiguration
public class EmployeeServiceTest {

    @InjectMocks
    EmployeeService employeeService; 
    @Mock
    EmployeeRepository employeeRepository;
    String name;
    @Before
    public void init() {
        MockitoAnnotations.initMocks(this);
    }
    @Test
    public void testEmployee() {
        Mockito.when(employeeRepository.findByName(name)).thenReturn(getEmployee());
        List<Employee> resultedEmployees = employeeService.getEmployeeByName("mike");
    }
    private List<Employee> getEmployee(){
        //here is the logic to return List<Employees>
    }
}

public EmployeeServiceImpl implements EmployeeService{
    @Autowired
    EmployeeRepository employeeRepository;
    employeeService.
    public List<Employee> getEmployeeByName(String name){
        List<Employee> employees =  employeeRepository.findByName(name);
        //Here I does't want to hit the real database so I have mocked it in testcase but I am getting empty list when I debug it. So can you please tell me what i did wrong in my "EmployeeServiceTest" to get List<Employee>
    }
}

So is there any additional logic required in testcase to get the mocked object.

Aucun commentaire:

Enregistrer un commentaire