如何更改此Spring测试框架测试以使用JSON返回?
我正在尝试将以下Spring测试框架更改为测试以下json返回:如何更改此Spring测试框架测试以使用JSON返回?,spring,spring-mvc,spring-test-mvc,Spring,Spring Mvc,Spring Test Mvc,我正在尝试将以下Spring测试框架更改为测试以下json返回: {"user":"John ","name":"JohnSmith"} 这是我的测试代码,我只是不知道如何更改它来检查JSON和JSON中的值。如果有人能告诉我该改变什么,那就太好了。。谢谢 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {WebMVCConfig.class}) @WebAppConfiguration publi
{"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"))
;
}
}
依赖关系:
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>0.8.1</version>
</dependency>
添加静态方法
依赖关系:
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>0.8.1</version>
</dependency>
添加静态方法…什么是jsonPath?我的代码不会生成!它不知道jsonpath是什么is@user2428795“什么都没有”是什么意思?您将得到一个导入错误,因为jsonpath不可用。或者它将编译并运行执行assertjsonpath its not available您当时没有添加依赖项,或者没有正确导入,否则您不会得到“its not available”什么是jsonPath?我的代码不会生成!它不知道jsonpath是什么is@user2428795“什么都没有”是什么意思?您将得到一个导入错误,因为jsonpath不可用。或者它将编译并运行执行assertjsonpath its not available如果您当时没有添加依赖项,或者没有正确导入,则不会得到“its not available”
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;