Класс контроллера
Код: Выделить всё
@RestController
@RequestMapping("/api/v1/demo")
public class UserController {
private final IUserService service;
public UserController(IUserService service) {
this.service = service;
}
@PostMapping(value = "/register", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity registerUser(@RequestBody @Valid UserRequest userRequest) {
UserResponse user = service.createUser(userRequest);
return ResponseEntity.status(CREATED).body(user);
}
@GetMapping(value = "/user/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasRole('client_user')")
public ResponseEntity hello(@PathVariable("id") Long id) {
UserResponse user = service.getUserById(id);
return ResponseEntity.status(OK).body(user);
}
@GetMapping(value = "/admin/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasRole('client_admin')")
public ResponseEntity hello2(@PathVariable("id") Long id) {
UserResponse user = service.getUserById(id);
return ResponseEntity.status(OK).body(user);
}
}
Тестовый класс
Код: Выделить всё
class UserControllerSpec extends ResourceSpecification {
static final String REGISTER_URL = '/api/v1/demo/register'
@Subject
UserController controller
IUserService service
@Override
protected createResource() {
service = Mock()
controller = new UserController(service)
}
def 'Successfully calling the preview endpoint'() {
given: 'a valid request'
def request = TestFixtures.createUserRequest()
and: 'an expected response'
def expectedResponse = TestFixtures.createUserResponse()
when: 'the controller is called'
def response = jerseyTest.target(REGISTER_URL).request().post(Entity.json(request))
then: 'response should have status 201'
response.getStatus() == 201
and: 'the cancel preview service is called once with the correct request parameters'
1 * service.createUser({
it.firstName == request.firstName &&
it.lastName == request.lastName &&
it.email == request.email &&
it.password == request.password &&
it.afm == request.afm &&
it.birthDate == request.birthDate &&
it.mobileNumber == request.mobileNumber &&
it.cardNumber == request.cardNumber
}) >> expectedResponse
and: 'the actual response should contain the expected values'
def actualResponse = response.readEntity(UserResponse.class)
actualResponse == expectedResponse
}
}
Код: Выделить всё
package util
import com.ebanking.system.controller.UserController
import groovy.json.JsonSlurper
import jakarta.ws.rs.core.Application
import jakarta.ws.rs.core.Response
import org.glassfish.jersey.server.ResourceConfig
import org.glassfish.jersey.test.JerseyTest
import spock.lang.Specification
abstract class ResourceSpecification extends Specification {
JerseyTest jerseyTest
JsonSlurper jsonSlurper
protected abstract createResource()
def setup() {
jerseyTest = new JerseyTest() {
@Override
protected Application configure() {
new ResourceConfig()
.register(createResource())
}
}
jerseyTest.setUp()
jsonSlurper = new JsonSlurper()
}
def cleanup() {
jerseyTest.tearDown()
}
}
Подробнее здесь: https://stackoverflow.com/questions/788 ... unit-tests