我正在编写一些代码来使用Mockito和Junit测试Web服务,因此我在hasSize(2)中遇到了问题,我注入了控制器并调用了findAll wich方法,该方法返回Employe的列表,但是出错坚持,在调试模式下它告诉我集合为空,但是不是。 错误是:
java.lang.AssertionError:JSON路径“ $”预期值: 大小<2>,但:集合大小为<0>
这是课程:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class GestionPointage3ApplicationTests {
private MockMvc mockMvc;
@InjectMocks
private EmployeController employeeController ;
@Mock
private EmployeService employeeService;
@Mock
private ModelMapper modelMapper;
@Before
public void setUp() throws Exception{
MockitoAnnotations.initMocks(this);
mockMvc=MockMvcBuilders.standaloneSetup(employeeController).build();
}
@Test
public void testgetAllEmployeeWithModelMapper() throws Exception{
Employe emp1 = new Employe("Hamza", "Khadhri", "hamza1007", "123");
Employe emp2 = new Employe("Oussema", "smi", "oussama", "1234");
List<Employe> Employees= Arrays.asList(emp1, emp2);
EmployeDTO dto1 = new EmployeDTO("Hamza", "Khadhri", "hamza1007", "123");
EmployeDTO dto2 = new EmployeDTO("Oussema", "smi", "oussama", "1234");
//when(modelMapper.map(emp1,EmployeDTO.class)).thenReturn(dto1);
// when(modelMapper.map(emp2,EmployeDTO.class)).thenReturn(dto2);
when(employeeService.findAll()).thenReturn(Employees);
mockMvc.perform(get("/employe/dto"))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].nom", is("Hamza")))
.andExpect(jsonPath("$[0].prenom", is("Khadhri")))
.andExpect(jsonPath("$[0].login", is("hamza1007")))
.andExpect(jsonPath("$[0].mp", is("123")))
.andExpect(jsonPath("$[1].nom", is("Oussema")))
.andExpect(jsonPath("$[1].prenom", is("smi")))
.andExpect(jsonPath("$[1].login", is("oussama")))
.andExpect(jsonPath("$[1].mp", is("1234")));
verify(employeeService,times(1)).findAll();
verifyNoMoreInteractions(employeeService);
}
}
这是控制器:
@CrossOrigin(origins = "*", allowedHeaders = "*")
@RestController
@RequestMapping("/employe")
public class EmployeController {
@Autowired
private EmployeService employeService;
@Autowired
private ModelMapper modelMapper;
@GetMapping("/dto")
public List<Employe> findAll() throws Exception{
return employeService.findAllEmployeActive();
}
}
方法签名:
public List<Employe>findAll() throws Exception;
public List<Employe>findAllEmployeActive() throws Exception;
StackTrace错误:
java.lang.AssertionError:预期状态:<200>,但在:<500> org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:55) 在 org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:82) 在 org.springframework.test.web.servlet.result.StatusResultMatchers.lambda $ matcher $ 9(StatusResultMatchers.java:619) 在 org.springframework.test.web.servlet.MockMvc $ 1.andExpect(MockMvc.java:178) 在 com.cynapsys.pointage.GestionPointage3ApplicationTests.testgetAllEmployeeWithModelMapper(GestionPointage3ApplicationTests.java:66) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)位于 sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)位于 java.lang.reflect.Method.invoke(来源未知) org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在 org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73) 在 org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83) 在 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 在 org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) 在..
答案 0 :(得分:1)
您正在嘲笑EmployeeService
并告诉被模仿的实例在调用Employee
方法时返回findAll()
列表(包含两个项目):
when(employeeService.findAll()).thenReturn(Employees)
但是在EmployeeController
中,您实际上是在EmployeeService
上调用另一种方法:
return employeService.findAllEmployeActive()
因此,您应该像这样更新嘲笑的期望:
when(employeeService.findAllEmployeActive()).thenReturn(Employees)