首页 文章

spring mockMVC测试方法GET

提问于
浏览
0

我在mockMVC中创建了post方法(在spring boot项目中)这是我的方法测试

这是我的方法测试

@Test
public void createAccount() throws Exception {
     AccountDTO accountDTO = new AccountDTO("SAVINGS", "SAVINGS");
     when(addaccountService.findByName("SAVING")).thenReturn(Optional.empty());
     when(addaccountService.createAccount(any())).thenReturn(createdAccountDTO);
    CreatedAccountDTO createdAccountDTO = new CreatedAccountDTO("a@wp.pl", "SAVINGS", "1234rds", uuid);

    mockMvc.perform(
             post("/account").contentType(MediaType.APPLICATION_JSON)
              .content(asJsonString(AccountNewDTO)))
            .andExpect(status().isCreated())
            .andExpect(header().string("location", containsString("/account/"+uuid.toString())));
    System.out.println("aaa");
}

我想写GET方法 .

如何在mock mvc中编写get方法?如何验证我扔的是什么?

2 回答

  • 0

    您可以使用类 MockMvcRequestBuilders 的静态 get 方法,请参阅:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/web/servlet/request/MockMvcRequestBuilders.html#get-java.lang.String-java.lang.Object...-

    例:

    mockMvc.perform(get("/account")).andExpect(...);
    

    如果在控制器方法中抛出异常,它通常会触发异常处理程序的执行,该异常处理程序将异常转换为HTTP错误响应 . 默认情况下,您可以检查响应的状态是否为500.如果您已实现自己的异常处理程序,则可能还需要检查响应正文以验证它是否包含预期的错误数据 .

  • 0
    You can try the below for Mockmvc perform get and post methods
    For get method
    
    @Autowired
    private MuffinRepository muffinRepository;
    
    @Test
    public void testgetMethod throws Exception(){
        Muffin muffin = new Muffin("Butterscotch");
        muffin.setId(1L);
    
        BddMockito.given(muffinRepository.findOne(1L)).
            willReturn(muffin);
    
        mockMvc.perform(MockMvcRequestBuilders.
            get("/muffins/1")).
            andExpect(MockMvcResutMatchers.status().isOk()).
            andExpect(MockMvcResutMatchers.content().string("{\"id\":1, "flavor":"Butterscotch"}"));    
    }
    
    //Test to do post operation
    @Test
    public void testgetMethod throws Exception(){
        Muffin muffin = new Muffin("Butterscotch");
        muffin.setId(1L);
    
        BddMockito.given(muffinRepository.findOne(1L)).
            willReturn(muffin);
    
        mockMvc.perform(MockMvcRequestBuilders.
            post("/muffins")
            .content(convertObjectToJsonString(muffin))
            .contentType(MediaType.APPLICATION_JSON)
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(MockMvcResutMatchers.status().isCreated())
            .andExpect(MockMvcResutMatchers.content().json(convertObjectToJsonString(muffin))); 
    }
    
    If the response is empty then make sure to override equals() and hashCode() method on the Entity your repository is working with
    
    //Converts Object to Json String
    private String convertObjectToJsonString(Muffin muffin) throws JsonProcessingException{
        ObjectWriter writer = new ObjectWriter().writer().withDefaultPrettyPrinter();
        return writer.writeValueAsString(muffin);
    }
    

相关问题