0

Repository object not injecting in testcase class. Here is my below test class code

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classesExternalProviderMain.class)
@ActiveProfiles(ApplicationConstants.DEVELOPMENT_PROFILE)
@WebAppConfiguration
public class EmployeeServiceTest {
@InjectMocks
EmployeeService employeeService; //not injected null 
@Mock
EmployeeRepository employeeRepository;//not injected null
@Test
public void testEmployee() {
    Mockito.when(employeeRepository.findByName(Stringname)).thenReturn(getEmployee());
    List<Employee> resultedTrackbles = employeeService.getEmployeeByName("mike");
}
private List<Employee> getEmployee(){
//here is the logic to return List<Employees>
}
}

Can you please help me how to inject my "EmployeeRepository" and Do need to write any additional logic.

Venkata Rama Raju
  • 1,325
  • 2
  • 10
  • 15

1 Answers1

2

That's because you are running your test with SpringJUnit4ClassRunner and not with MockitoJUnitRunner.

The mocks need to be initialized with MockitoAnnotations.initMocks:

@Before
public void init() {
    MockitoAnnotations.initMocks(this);
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • One more question After invoking this method "employeeService.getEmployeeByName("mike")" in test case. It's internally calls "employeeRepository.findByName(Stringname)" metod. It's already moked in test case but I didn't get List; getting empty list. When I debug it returns empty list in my service So can you plese help what I did wrong – Venkata Rama Raju Sep 27 '15 at 14:48
  • @VenkataRamaRaju You should post another question as it is not related to this one. – Tunaki Sep 27 '15 at 14:49
  • Sure, Thanks I will be add a new question – Venkata Rama Raju Sep 27 '15 at 14:56