没有在测试用例类中注入存储库对象。下面是我的测试类代码
@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>
}
}
你能帮我如何注入我的"EmployeeRepository",我需要写任何额外的逻辑。
这是因为您正在使用SpringJUnit4ClassRunner
而不是MockitoJUnitRunner
运行测试。
模拟需要用MockitoAnnotations.initMocks
初始化:
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}