1

我正在尝试更改以下 Spring Test Framework 以测试以下 json 返回:

{"user":"John ","name":"JohnSmith"}

这是我的测试代码,我只是不知道如何更改它以检查 JSON 和 JSON 中的值。如果有人能告诉我要改变什么,那就太好了..谢谢

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WebMVCConfig.class})
@WebAppConfiguration
public class TestHelloWorldWeb
{
    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setup()
    {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    @Test
    public void getFoo() throws Exception
    {
        this.mockMvc.perform(get("/ask")
                .accept(MediaType.TEXT_HTML))
                .andExpect(status().isOk())
                .andExpect(view().name("helloworld"))
                .andExpect(MockMvcResultMatchers.model().attribute("user", "JohnSmith"))
        ;


    }
}
4

1 回答 1

1
.andExpect(jsonPath("$.user").value("john"));

有依赖:

<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>0.8.1</version>
</dependency>

和一个像这样的静态导入

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

添加静态方法...

于 2013-06-11T18:46:05.560 回答