Skip to content

Instantly share code, notes, and snippets.

@gokman
Created February 7, 2017 06:12
Show Gist options
  • Save gokman/50f80e9017b656d9478d2aae85b2a468 to your computer and use it in GitHub Desktop.
Save gokman/50f80e9017b656d9478d2aae85b2a468 to your computer and use it in GitHub Desktop.
Spring Boot Controller Unit Test
@SpringBootTest(classes = XXXApplication.class) //if we change Application.class we must declare here what the application main class is
@RunWith(SpringRunner.class)
public class UserControllerTests {
private MockMvc mockMvc;
@Autowired
WebApplicationContext webApplicationContext;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
}
@Test
public void createUseerWithBadCredentialsWillReturnBadRequest() throws Exception{
User user = new User();
this.mockMvc.perform(post("/v1/user")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(new ObjectMapper().writeValueAsString(customer)))
.andExpect(status().isBadRequest())
.andExpect(content().string(containsString("check your credentials")));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment