Skip to main content

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)
    private Integer offset = DEFAULT_OFFSET;

    @Range(min = 1)
    private Integer limit = DEFAULT_LIMIT;

}

Only issue is the ValidationException had a constant message so my expectMessage wont work. I had to now really get the real exception out of ExpectedException and match it, for this I had to write an Exception Matcher (not the most beautiful code but for the temporary api its ok).


    @AllArgsConstructor
    private static class ValidationExceptionMatcher extends TypeSafeMatcher<ValidationException> {

        private String descriptionText;
        private String msgPrefix;

        @Override
        public boolean matchesSafely(ValidationException item) {
            Set<ConstraintViolation<?>> violations = item.getConstraintViolations();
            if (violations.size() == 1) {
                ConstraintViolation<?> violation = violations.iterator().next();
                return violation.getMessage().startsWith(msgPrefix);
            }
            return false;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText(descriptionText);
        }
    }

    @Test
    public void testQueryMinimumChars() throws ApplicationException {
        expectedException.expect(ValidationException.class);
        expectedException.expectMessage("constraint violations");
        expectedException.expect(new ValidationExceptionMatcher("Size", "Size must be between 3 and"));
        sut.search(null, new FolderSearchRequest("te", 0, 20));
    }



Comments

Popular posts from this blog

Docker Aha moment

I had read many articles about docker before but today was the day when I realized the aha moment.  I had designed database structure of a new application where different micro-services uses their own schemas and each can be located on its own server depending on the scale requirement. Locally I had kept them on same mysql host but on production it will be used on different Google CloudSQL instances.  The aha moment for me was when I was able to fire up multiple mysql containers and map them to a different port and do end to end testing of my sharding code all on local box.

All I had to do was

docker run -p 3306:3306 -d  gcr.io/xx-us/mysql:5.6
docker run -p 3307:3306 -d  gcr.io/xx-us/mysql:5.6
docker run -p 3308:3306 -d  gcr.io/xx-us/mysql:5.6
docker run -p 3309:3306 -d  gcr.io/xx-us/mysql:5.6
docker run -p 3310:3306 -d  gcr.io/xx-us/mysql:5.6
.....

and I had many mysql servers ready to be tested.   In the VM world I would not even had dared to start 5 vm on my laptop.

IPhone will beat DSLR in long run

I started taking interest in photography recently and have accumulated a decent amount of gear but I am realizing that the ease of taking out your phone and clicking picture will beat the DSLR in long run. A friend recently visited me from NY and we wanted to take a family picture and I was setting up Tripod and Flash and doing settings changes and he was like leave all this, lets take a Selfie and that’s it, in 2 second the picture was done and he shared it on facebook in another 1 second.  Now one can argue that DSLR would have clicked a better picture but DSLR has many things going against it:-
Learning curve : I must have spent 200+hours on reading about photography but still cant take decent pictures as my bar is high. Not everyone is interested in spending this much time.Amount of gear to be carried : On hikes its a pain to carry your DSLR whereas your phone has to be anyway with you.Cognitive effort of tweaking the gear: You have to have a different lens/settings for different s…

Embracing "Deep Work" for productivity

Do you have that feeling where you worked all day but at the end it feels like you got nothing done?  In past 7 years at Egnyte to reduce burnout every 6 months to an year I have to fight this constant productivity battle. I have accumulated several habits in the process to increase productivity, some of them are:-
Give yourself scheduled time: Allocate 3 hours for creative work, I had my calendar open whole day and I would get interviews and meeting scheduled randomly all over the day. When you are on a maker's schedule this is disastrous. I recently allocated 3 hours of calendar time and I reject meeting invite unless its absolutely urgent and I am contributing to it.Walk in middle of the day: by the middle of the day brain feels tired and I cant code or focus so I started 30 min walk. I tried listening to podcast or listening music on the walk but that felt more work so I stopped doing it. I just walk and think on the current problem on hand.5 minute rule: If you can finish the …