BAEL-2736 rest assured spring mock mvc (#6466)
* Include rest-assured.spring-mock-mvc dependency BAEL-2736 * Rest Assured Spring Mock MVC Tests BAEL-2736
This commit is contained in:
committed by
KevinGilmore
parent
d84dedbb95
commit
fda8a29eb6
+40
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.restassured.learner;
|
||||
|
||||
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
import static org.springframework.http.HttpStatus.NOT_FOUND;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = RANDOM_PORT)
|
||||
public class CourseControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webApplicationContext;
|
||||
|
||||
@Before
|
||||
public void initialiseRestAssuredMockMvcWebApplicationContext() {
|
||||
RestAssuredMockMvc.webAppContextSetup(webApplicationContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNoMatchingCourseCodeWhenGetCourseThenRespondWithStatusNotFound() {
|
||||
String nonMatchingCourseCode = "nonMatchingCourseCode";
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/courses/" + nonMatchingCourseCode)
|
||||
.then()
|
||||
.log().ifValidationFails()
|
||||
.statusCode(NOT_FOUND.value());
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.restassured.learner;
|
||||
|
||||
import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import static io.restassured.http.ContentType.JSON;
|
||||
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.http.HttpStatus.NOT_FOUND;
|
||||
import static org.springframework.http.HttpStatus.OK;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CourseControllerUnitTest {
|
||||
|
||||
@Mock
|
||||
private CourseService courseService;
|
||||
@InjectMocks
|
||||
private CourseController courseController;
|
||||
@InjectMocks
|
||||
private CourseControllerExceptionHandler courseControllerExceptionHandler;
|
||||
|
||||
@Before
|
||||
public void initialiseRestAssuredMockMvcStandalone() {
|
||||
RestAssuredMockMvc.standaloneSetup(courseController, courseControllerExceptionHandler);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNoExistingCoursesWhenGetCoursesThenRespondWithStatusOkAndEmptyArray() {
|
||||
when(courseService.getCourses()).thenReturn(Collections.emptyList());
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/courses")
|
||||
.then()
|
||||
.log().ifValidationFails()
|
||||
.statusCode(OK.value())
|
||||
.contentType(JSON)
|
||||
.body(is(equalTo("[]")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNoMatchingCoursesWhenGetCoursesThenRespondWithStatusNotFound() {
|
||||
String nonMatchingCourseCode = "nonMatchingCourseCode";
|
||||
|
||||
when(courseService.getCourse(nonMatchingCourseCode)).thenThrow(new CourseNotFoundException(nonMatchingCourseCode));
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/courses/" + nonMatchingCourseCode)
|
||||
.then()
|
||||
.log().ifValidationFails()
|
||||
.statusCode(NOT_FOUND.value());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user