Search in sources :

Example 11 with PostController

use of io.restassured.module.mockmvc.http.PostController in project rest-assured by rest-assured.

the class MockMvcRequestSpecificationMergingTest method mock_mvc_instance_is_overwritten_when_defined_in_specification.

@Test
public void mock_mvc_instance_is_overwritten_when_defined_in_specification() {
    // Given
    MockMvc otherMockMvcInstance = MockMvcBuilders.standaloneSetup(new PostController()).build();
    MockMvc thisMockMvcInstance = MockMvcBuilders.standaloneSetup(new GreetingController()).build();
    MockMvcRequestSpecification specToMerge = new MockMvcRequestSpecBuilder().setMockMvc(otherMockMvcInstance).build();
    // When
    MockMvcRequestSpecification spec = RestAssuredMockMvc.given().mockMvc(thisMockMvcInstance).spec(specToMerge);
    // Then
    assertThat(Whitebox.getInternalState(implOf(spec).getMockMvcFactory(), "mockMvc")).isSameAs(otherMockMvcInstance);
}
Also used : MockMvcRequestSpecification(io.restassured.module.mockmvc.specification.MockMvcRequestSpecification) PostController(io.restassured.module.mockmvc.http.PostController) MockMvcRequestSpecBuilder(io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder) GreetingController(io.restassured.module.mockmvc.http.GreetingController) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 12 with PostController

use of io.restassured.module.mockmvc.http.PostController in project rest-assured by rest-assured.

the class GreetingControllerVanillaMockMvcTest method mock_mvc_example_for_post_greeting_controller.

@Test
public void mock_mvc_example_for_post_greeting_controller() throws Exception {
    MockMvc mockMvc = standaloneSetup(new PostController()).setMessageConverters(new MappingJackson2HttpMessageConverter()).build();
    String contentAsString = mockMvc.perform(post("/greetingPost").param("name", "Johan").contentType(MediaType.APPLICATION_FORM_URLENCODED)).andReturn().getResponse().getContentAsString();
    JsonPath jsonPath = new JsonPath(contentAsString);
    assertThat(jsonPath.getInt("id"), equalTo(1));
    assertThat(jsonPath.getString("content"), equalTo("Hello, Johan!"));
}
Also used : PostController(io.restassured.module.mockmvc.http.PostController) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) JsonPath(io.restassured.path.json.JsonPath) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 13 with PostController

use of io.restassured.module.mockmvc.http.PostController in project rest-assured by rest-assured.

the class RequestLoggingTest method doesnt_log_if_request_validation_succeeds_when_request_logging_if_validation_fails_is_enabled.

@Test
public void doesnt_log_if_request_validation_succeeds_when_request_logging_if_validation_fails_is_enabled() {
    RestAssuredMockMvc.given().log().ifValidationFails().standaloneSetup(new PostController()).param("name", "Johan").when().post("/greetingPost").then().body("id", equalTo(1)).body("content", equalTo("Hello, Johan!"));
    assertThat(writer.toString(), isEmptyString());
}
Also used : PostController(io.restassured.module.mockmvc.http.PostController) Test(org.junit.Test)

Example 14 with PostController

use of io.restassured.module.mockmvc.http.PostController in project rest-assured by rest-assured.

the class ResponseLoggingTest method logging_if_response_validation_fails_works.

@Test
public void logging_if_response_validation_fails_works() {
    try {
        RestAssuredMockMvc.given().standaloneSetup(new PostController()).param("name", "Johan").when().post("/greetingPost").then().log().ifValidationFails().body("id", equalTo(1)).body("content", equalTo("Hello, Johan2!"));
        fail("Should throw AssertionError");
    } catch (AssertionError e) {
        assertThat(writer.toString(), equalTo("200\nContent-Type: application/json;charset=UTF-8\n\n{\n    \"id\": 1,\n    \"content\": \"Hello, Johan!\"\n}\n"));
    }
}
Also used : PostController(io.restassured.module.mockmvc.http.PostController) Test(org.junit.Test)

Aggregations

PostController (io.restassured.module.mockmvc.http.PostController)14 Test (org.junit.Test)14 LogConfig (io.restassured.config.LogConfig)5 RestAssuredMockMvcConfig (io.restassured.module.mockmvc.config.RestAssuredMockMvcConfig)4 ResponseSpecBuilder (io.restassured.builder.ResponseSpecBuilder)2 MockMvcRequestSpecBuilder (io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder)2 MockMvc (org.springframework.test.web.servlet.MockMvc)2 GreetingController (io.restassured.module.mockmvc.http.GreetingController)1 MockMvcRequestSpecification (io.restassured.module.mockmvc.specification.MockMvcRequestSpecification)1 JsonPath (io.restassured.path.json.JsonPath)1 PrintStream (java.io.PrintStream)1 StringWriter (java.io.StringWriter)1 WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)1 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)1