Reputation: 547
I am trying to change the following Spring Test Framework to test for a the following json return:
{"user":"John ","name":"JohnSmith"}
Here is my test code and I just dont know how to change it to check for JSON and the value in JSON. It will be great if someone can tell me what to change.. thanks
@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"))
;
}
}
Upvotes: 1
Views: 120
Reputation: 47290
.andExpect(jsonPath("$.user").value("john"));
with dependency :
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>0.8.1</version>
</dependency>
and one static import like this
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
adds the static method...
Upvotes: 1