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

Compartmentalization helps with Deep Work

I had been trying to learn Solidity/Ethereum over the weekends for the past few months and the first 3-4 weekends were a drag as no matter what I do I wasnt able to focus and getting no where. The problem was not with motivation as I was trying to do it for many weeks but all I was able to do was read 100s of blog posts about it but not able to code anything.

Aparently I realized that on weekdays most of my work is in the "study" room whereas on weekend I was trying to do it in the living room. Now working in study on wekeend was an issue as it felt more like work than fun so last 2 weekends I tried changing the schedule and went 3 hours every Sunday to library with my son and while he was reading books I was coding in solidity.

I also had trouble writing code after 7:00 PM as I thought my brain was tired but last week I tried sitting in study around 10:00 -11:00 in night and boy I was able to focus and code.

Net Net I realized that:
"Having a consistent Routine help wit…

Adventures of a nature lover - 5 national parks in 14 days

To unplug from work and recharge myself I do a 2-3 week trip every year where I am unplugged. Few of the reasons I can totally unplug from work is
Unlimited Vacation policy of Egnyte, Excellent support by the Infrastructure team Our ethos of pro-actively fixing issues before they become nuisance.
TLDR; It's a long post so you can scroll down and first see see images if you need motivation to read it entirely.

Me and my family like national parks and camping to recharge us as there is no cell phone  coverage in parks and you are completely unplugged from technology most of the times. We have done many of the national parks nearby and this year we want to see glacier national park as the glaciers may disappear in 10-15 years so see them before they are gone. Behind every successful trip is a "Trip planner" and for our family its my wife, she researched  and made a trip itinerary book.

She booked camp sites 6 months in advanceShe researched trails and as days are few she pres…

Seven things doing a 1000 piece puzzle has common with complex engineering projects

I was doing grocery shopping during the New Year holidays and the store had a lot of 1000 piece puzzle on sale for $11. My son had never done more than 100 pieces and  I was like hey this seems interesting for him, so I bought one. We started working it on Jan13th and finished between 4 people on Jan25th. During the journey of finishing I saw a lot of similarities with complex engineering projects. I think everyone in engineering should do one of these and here are some of the things I learned.


Underestimating the task: I grossly underestimated the task and amount of time it would take for my son to do it.Teamwork: After a day or two I realized my son lost interest, the whole family had to be involved to keep him motivated on it.Prep work: Like engineering projects, you need to do a lot of prep work like:Turn  the pieces down Study the patternsSort the piecesDivide and rule: Like engineering projects you need to pick some quick wins initially to get off the ground and start assigning t…