Validate the JSON-formatted response body returned by RestController.
Spring Boot:2.1.1 Spock:1.2 Groovy:2.5 OS:Windows10
Add the required libraries for Spring Boot + Spock testing. By writing the dependency on spock-core, the libraries around Groovy will also drop. Also, if you don't have spock-spring, you can't use @Autowired etc. in Spock (Groovy), so add this if necessary.
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.2-groovy-2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-spring</artifactId>
<version>1.2-groovy-2.5</version>
<scope>test</scope>
</dependency>
The URL and response body of the RestController you want to test are as follows.
URL:
http://localhost:8080/employees
Response:
Status Code 200 OK
[
{
"id": "00000001",
"name": "Employee A",
"department": {
"id": "001",
"name": "Department A"
}
},
{
"id": "00000002",
"name": "Employee B",
"department": {
"id": "001",
"name": "Department A"
}
}
]
I will omit the steps from EmployeeService # getEmployees, but the Controller you want to test is as follows.
EmployeeController.java
@RestController
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping(value = "/employees")
public ResponseEntity<List<Employee>> getEmployees() {
List<Employee> employeeList = employeeService.getEmployees();
if (CollectionUtils.isEmpty(employeeList)) {
return new ResponseEntity<>(null, HttpStatus.NO_CONTENT);
}
return new ResponseEntity<List<Employee>>(employeeList, HttpStatus.OK);
}
}
Verify using Groovy's Json Slurper and Json Builder.
//ApplicationContext is loaded, but Web environment etc. is not provided
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
//Start the server on a random port
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
//The default is this, you don't have to write it. A simulated web environment is provided without starting the server
// @AutoConfigureMockMvc or@Can be used in combination with AutoConfigureWebTestClient
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
//The actual web environment is provided. The embedded server starts and the configured port (application).Listen on properties) or on the default port 8080
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
EmployeeControllerTest.groovy
@SpringBootTest
@AutoConfigureMockMvc
class EmployeeControllerTest extends Specification {
@SpyBean
private EmployeeService employeeService
@Autowired
MockMvc mockMvc
@Unroll
def "EmployeeController -Normal system-2 employees return"() {
given:
def department = new Department("001", "Department A");
def employeeList = new ArrayList<Employee>() { {
this.add(new Employee("00000001", "Employee A", department));
this.add(new Employee("00000002", "Employee B", department));
}
}
def jsonBuilder = new JsonBuilder(employeeList)
def jsonSlurper = new JsonSlurper()
when:
def actual = mockMvc.perform(MockMvcRequestBuilders.get("/employees")).andReturn().getResponse()
then:
actual.getStatus() == HttpStatus.OK.value
jsonSlurper.parseText(actual.getContentAsString()) == jsonSlurper.parseText(jsonBuilder.toPrettyString())
}
@Unroll
def "EmployeeController -Normal system-Nothing returns"() {
given:
when(employeeService.getEmployees()).thenReturn(null)
when:
def actual = mockMvc.perform(MockMvcRequestBuilders.get("/employees")).andReturn().getResponse()
then:
actual.getStatus() == HttpStatus.NO_CONTENT.value
actual.getContentAsString() == ""
}
}
In this article, I used Groovy's Json Slurper and Json Builder to verify JSON. Alternatively, [Spring Boot @JsonTest](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring You can also verify JSON by using -boot-applications-testing-autoconfigured-json-tests) etc.
Source used in this article: https://github.com/kenichi-nagaoka/spock-sample/tree/feature/1
that's all.
Recommended Posts