使用 Junit 和 Mock put 请求进行测试
Testing using Junit and Mock put request
我正在尝试对放置请求进行测试,它在这种类型的请求中给了我空指针错误我不知道为什么请有人帮助我,这是我的控制器:
@PutMapping ("/update/{id}")
public Entreprise updateEntreprise(@PathVariable Long id,@RequestBody Entreprise entreprise ) {
Entreprise e=entrepriseService.getEntreprise(id);
e.setDescription(entreprise.getDescription());
e.setNom(entreprise.getNom());
e.setNumberCertificats(entreprise.getNumberCertificats());
e.setNumberClients(entreprise.getNumberClients());
e.setNumberYears(entreprise.getNumberYears());
e.setNumberCollaborators(entreprise.getNumberCollaborators());
entrepriseService.updateEntreprise(e);
return e;
}
测试方法:
@RunWith(SpringRunner.class)
@WebMvcTest(value = EntrepriseController.class, secure = false)
public class TestsEntrepriseController {
@Autowired
private MockMvc mockMvc;
@MockBean
EntrepriseService entrepriseService;
@Test
public void givenEntrepriseURIWithPut_whenMockMVC_thenVerifyResponse() throws Exception {
Entreprise entreprise = new Entreprise();
entreprise.setId(1);
entreprise.setNom("oumaima");
entreprise.setDescription("description");
entreprise.setNumberCertificats(12);
entreprise.setNumberClients(15);
entreprise.setNumberCollaborators(20);
entreprise.setNumberYears(12);
Services services = new Services();
services.setNom("cloud");
services.setId(1);
Set<Services> allServices = new HashSet<>(Arrays. asList(services));
entreprise.setServices(allServices);
mockMvc.perform(put("/entreprise/update/1")
.contentType(IntegrationTestUtil.APPLICATION_JSON_UTF8)
.content(IntegrationTestUtil.convertObjectToJsonBytes(entreprise))
)
.andExpect(status().isBadRequest())
.andExpect(content().string("{\"fieldErrors\":[{\"path\":\"title\",\"message\":\"The title cannot be empty.\"}]}"));
}
}
我没有看到你模拟服务调用。
添加 - Mockito.when(entrepriseService.getEntreprise(Mockito.anyLong())).thenReturn(new Entreprise())
您需要模拟来自 entrepriseService
的两个调用
- 对于 getEntreprise(id)
- 更新企业(e)
尝试 运行 下面的代码
@RunWith(SpringRunner.class)
@WebMvcTest(value = EntrepriseController.class, secure = false)
public class TestsEntrepriseController {
@Autowired
private MockMvc mockMvc;
@MockBean
EntrepriseService entrepriseService;
@Test
public void givenEntrepriseURIWithPut_whenMockMVC_thenVerifyResponse() throws Exception {
Entreprise entreprise = new Entreprise();
entreprise.setId(1);
entreprise.setNom("oumaima");
entreprise.setDescription("description");
entreprise.setNumberCertificats(12);
entreprise.setNumberClients(15);
entreprise.setNumberCollaborators(20);
entreprise.setNumberYears(12);
Services services = new Services();
services.setNom("cloud");
services.setId(1);
Set<Services> allServices = new HashSet<>(Arrays. asList(services));
entreprise.setServices(allServices);
Mockito.when(entrepriseService.getEntreprise(Mockito.any())).thenReturn(new Entreprise());
Mockito.when(entrepriseService.updateEntreprise(Mockito.any(Entreprise .class))).thenReturn(entreprise);
mockMvc.perform(put("/entreprise/update/1")
.contentType(IntegrationTestUtil.APPLICATION_JSON_UTF8)
.content(IntegrationTestUtil.convertObjectToJsonBytes(entreprise))
)
.andExpect(status().isBadRequest())
.andExpect(content().string("{\"fieldErrors\":[{\"path\":\"title\",\"message\":\"The title cannot be empty.\"}]}"));
}
}
我正在尝试对放置请求进行测试,它在这种类型的请求中给了我空指针错误我不知道为什么请有人帮助我,这是我的控制器:
@PutMapping ("/update/{id}")
public Entreprise updateEntreprise(@PathVariable Long id,@RequestBody Entreprise entreprise ) {
Entreprise e=entrepriseService.getEntreprise(id);
e.setDescription(entreprise.getDescription());
e.setNom(entreprise.getNom());
e.setNumberCertificats(entreprise.getNumberCertificats());
e.setNumberClients(entreprise.getNumberClients());
e.setNumberYears(entreprise.getNumberYears());
e.setNumberCollaborators(entreprise.getNumberCollaborators());
entrepriseService.updateEntreprise(e);
return e;
}
测试方法:
@RunWith(SpringRunner.class)
@WebMvcTest(value = EntrepriseController.class, secure = false)
public class TestsEntrepriseController {
@Autowired
private MockMvc mockMvc;
@MockBean
EntrepriseService entrepriseService;
@Test
public void givenEntrepriseURIWithPut_whenMockMVC_thenVerifyResponse() throws Exception {
Entreprise entreprise = new Entreprise();
entreprise.setId(1);
entreprise.setNom("oumaima");
entreprise.setDescription("description");
entreprise.setNumberCertificats(12);
entreprise.setNumberClients(15);
entreprise.setNumberCollaborators(20);
entreprise.setNumberYears(12);
Services services = new Services();
services.setNom("cloud");
services.setId(1);
Set<Services> allServices = new HashSet<>(Arrays. asList(services));
entreprise.setServices(allServices);
mockMvc.perform(put("/entreprise/update/1")
.contentType(IntegrationTestUtil.APPLICATION_JSON_UTF8)
.content(IntegrationTestUtil.convertObjectToJsonBytes(entreprise))
)
.andExpect(status().isBadRequest())
.andExpect(content().string("{\"fieldErrors\":[{\"path\":\"title\",\"message\":\"The title cannot be empty.\"}]}"));
}
}
我没有看到你模拟服务调用。 添加 - Mockito.when(entrepriseService.getEntreprise(Mockito.anyLong())).thenReturn(new Entreprise())
您需要模拟来自 entrepriseService
- 对于 getEntreprise(id)
- 更新企业(e)
尝试 运行 下面的代码
@RunWith(SpringRunner.class)
@WebMvcTest(value = EntrepriseController.class, secure = false)
public class TestsEntrepriseController {
@Autowired
private MockMvc mockMvc;
@MockBean
EntrepriseService entrepriseService;
@Test
public void givenEntrepriseURIWithPut_whenMockMVC_thenVerifyResponse() throws Exception {
Entreprise entreprise = new Entreprise();
entreprise.setId(1);
entreprise.setNom("oumaima");
entreprise.setDescription("description");
entreprise.setNumberCertificats(12);
entreprise.setNumberClients(15);
entreprise.setNumberCollaborators(20);
entreprise.setNumberYears(12);
Services services = new Services();
services.setNom("cloud");
services.setId(1);
Set<Services> allServices = new HashSet<>(Arrays. asList(services));
entreprise.setServices(allServices);
Mockito.when(entrepriseService.getEntreprise(Mockito.any())).thenReturn(new Entreprise());
Mockito.when(entrepriseService.updateEntreprise(Mockito.any(Entreprise .class))).thenReturn(entreprise);
mockMvc.perform(put("/entreprise/update/1")
.contentType(IntegrationTestUtil.APPLICATION_JSON_UTF8)
.content(IntegrationTestUtil.convertObjectToJsonBytes(entreprise))
)
.andExpect(status().isBadRequest())
.andExpect(content().string("{\"fieldErrors\":[{\"path\":\"title\",\"message\":\"The title cannot be empty.\"}]}"));
}
}