【问题标题】:Testing using Junit and Mock put request使用 Junit 和 Mock put 请求进行测试
【发布时间】:2020-06-14 23:08:59
【问题描述】:

我正在尝试对 put 请求进行测试,它在这种类型的请求中给了我空指针错误我不知道为什么有人可以帮助我,这是我的控制器:

@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.\"}]}"));

    }
}

【问题讨论】:

    标签: java spring-boot junit mockito


    【解决方案1】:

    你需要模拟来自entrepriseService的两个调用

    1. 对于 getEntreprise(id)
    2. 对于 updateEntreprise(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.\"}]}"));
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      我没有看到您模拟服务调用。 添加 - Mockito.when(entrepriseService.getEntreprise(Mockito.anyLong())).thenReturn(new Entreprise())

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-24
        • 2016-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多