java-tutorials/spring-rest-http/src/test/java/com/baeldung/requestmapping/FooMappingExamplesControllerUnitTest.java

36 lines
1.3 KiB
Java
Raw Normal View History

package com.baeldung.requestmapping;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringRunner.class)
@WebMvcTest(FooMappingExamplesController.class)
public class FooMappingExamplesControllerUnitTest {
@Autowired
private MockMvc mvc;
@Test
public void givenAcceptsJson_whenGetDuplicate_thenJsonResponseReturned() throws Exception {
mvc.perform(get("/ex/foos/duplicate")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string("{\"message\":\"Duplicate\"}"));
}
@Test
public void givenAcceptsXml_whenGetDuplicate_thenXmlResponseReturned() throws Exception {
mvc.perform(get("/ex/foos/duplicate")
.accept(MediaType.APPLICATION_XML))
.andExpect(status().isOk())
.andExpect(content().string("<message>Duplicate</message>"));
}
}