Java 模拟存储库对象从控制器testcase返回空结果?
存储库对象未从controller testcase模拟返回空对象下面是代码Java 模拟存储库对象从控制器testcase返回空结果?,java,unit-testing,mockito,spring-data-jpa,springjunit4classrunner,Java,Unit Testing,Mockito,Spring Data Jpa,Springjunit4classrunner,存储库对象未从controller testcase模拟返回空对象下面是代码 @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Main.class) @WebAppConfiguration @ActiveProfiles(ApplicationConstants.DEVELOPMENT_PROFILE) public class EmployeeControllerRealTes
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Main.class)
@WebAppConfiguration
@ActiveProfiles(ApplicationConstants.DEVELOPMENT_PROFILE)
public class EmployeeControllerRealTest {
@Autowired
private WebApplicationContext webAppContext;
private MockMvc mockMvc;
@Mock
EmployeeRepository employeeRepository;
@InjectMocks
EmployeeCompositeService employeeCompositeService;
@InjectMocks
EmployeeService employeeService;
@InjectMocks
EmployeeController employeeController;
String name = "mike";
@Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build();
MockitoAnnotations.initMocks(this);
}
@Test
public void testGetEmployees() throws Exception {
Mockito.when(employeeRepository.findByName(name)).thenReturn(getEmployees());
String url = URIConstants.ROOT_CONTEXT + URIConstants.EMPLOYEE;
MvcResult result =
mockMvc.perform(post(url)
.contentType(APPLICATION_JSON_UTF8)
.content(convertObjectToJsonBytes(name))
.andExpect(status().isOk())
.andExpect(content().contentType(APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$[0].employeeName").value("Mike"))
.andReturn();
String jsonContent = result.getResponse().getContentAsString();
LOGGER.debug("jsonContent: {}",jsonContent);
}
protected byte[] convertObjectToJsonBytes(Object object) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return mapper.writeValueAsBytes(object);
}
private List<Employee> getEmployees(){
//here is the logic to get List of employees to return. When the mockito call is invoked.
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(类=Main.class)
@WebAppConfiguration
@ActiveProfiles(ApplicationConstants.DEVELOPMENT\u配置文件)
公共类EmployeeControllerRealTest{
@自动连线
私有WebApplicationContext-webAppContext;
私有MockMvc-MockMvc;
@嘲弄
雇员职位雇员职位;
@注射模拟
雇员综合服务雇员综合服务;
@注射模拟
员工服务员工服务;
@注射模拟
员工控制员员工控制员;
String name=“mike”;
@以前
public void setUp()引发异常{
mockMvc=MockMvcBuilders.webAppContextSetup(webAppContext.build();
initMocks(this);
}
@试验
public void testGetEmployees()引发异常{
Mockito.when(employeeRepository.findByName(name)).thenReturn(getEmployees());
字符串url=URIConstants.ROOT\u上下文+URIConstants.EMPLOYEE;
MVC结果=
mockMvc.perform(post(url)
.contentType(应用程序\u JSON\u UTF8)
.content(convertObjectToJsonBytes(名称))
.andExpect(状态().isOk())
.andExpect(content().contentType(应用程序\u JSON\u UTF8))
.andExpect(jsonPath(“$[0].employeeName”).value(“Mike”))
.andReturn();
字符串jsonContent=result.getResponse().getContentAsString();
debug(“jsonContent:{}”,jsonContent);
}
受保护字节[]convertObjectToJsonBytes(对象对象)引发IOException{
ObjectMapper mapper=新的ObjectMapper();
setSerializationInclusion(JsonInclude.Include.NON_NULL);
返回mapper.writeValueAsBytes(对象);
}
私有列表getEmployees(){
//下面是调用mockito调用时获取要返回的员工列表的逻辑。
}
}
我在employeeServiceImpl中调用了存储库调用来调用findByName(“mike”),所以我不想访问数据库,所以我在控制器方法(即testGetEmployees()中进行了模拟
当我运行这个测试用例时,它调用EmployeeController方法并调用EmployeeCompositeService方法,而不是在此服务方法中调用EmployeeService方法
有一个存储库调用,该调用在此控制器测试方法中被模拟。调试时,它返回空列表。我在控制器测试用例中做错了什么。
你能帮我提前谢谢吗。Mockito中不是有带括号的代码吗。when(employeeRepository.findByName(name)。thenReturn(getEmployees());我想应该是Mockito。when(employeeRepository.findByName(name))。thenReturn(getEmployees());抱歉,我在发布问题时忘了写右括号好的,所以当您调试repository类时,它说了什么?您应该将其视为类似repositoryEnchncedWithCGLib或类似的内容。您还可以验证getEmployees是否返回了一些值。通常,如果方法未被模拟,则应改为null在EmployeeServiceImpl中执行这段代码(即employeeRepository.findByName(“Mike”)后,它显示空列表,即使我们在控制器测试方法(即testGetEmployees()中进行了模拟。好的,那么getEmployees()呢?您是否在测试开始时尝试将其分配给局部变量并检查其包含的内容?它不是空的吗?