Skip to main content

Posts

Showing posts from September, 2014

Junit Expected Exception checking Part2

It seems in my previous post the way I was sending error response was not the way UI was expecting it so I had to refactor code and used hibernate validator but it posed some issues in exception validation.

The code in REST api changed to

    public Response search(@Context User user, FolderSearchRequest folderSearchRequest) throws ApplicationException {

        Set<ConstraintViolation<FolderSearchRequest>> violationResult = validator.validate(folderSearchRequest);
        if (!violationResult.isEmpty()) {
            Set<ConstraintViolation<?>> violations = new HashSet<ConstraintViolation<?>>(violationResult);
            throw new ValidationException(violations);
        }

and the pojo changed to

@Data
@AllArgsConstructor
@NoArgsConstructor
public class FolderSearchRequest {
    public static final Integer DEFAULT_LIMIT = 30;
    public static final Integer DEFAULT_OFFSET = 0;
    @NotBlank
    @Size(min = 3)
    private String query;

    @Range(min = 0)
    pri…

Junit Expected Exception checking

I was writing a temporary REST api for folder search until the elasticsearch based api is live. The api has some validation code and I was trying to test it.

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response search(@Context User user, FolderSearchRequest folderSearchRequest) throws ApplicationException {
        if (StringUtils.length(folderSearchRequest.getQuery()) < 3) {
            throw new GenericRestServiceException(ErrorCode.FailedValidation, INVALID_QUERY_MSG);
        }
        if (folderSearchRequest.getStart() < 0) {
            throw new GenericRestServiceException(ErrorCode.FailedValidation,
                    INVALID_START_MESSAGE);
        }
        if (folderSearchRequest.getLimit() <= 0) {
            throw new GenericRestServiceException(ErrorCode.FailedValidation,
                    INVALID_LIMIT_MESSAGE);
        }

At first I was writing code like

    @Test(expected=GenericRestServiceExcepti…