Skip to main content

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=GenericRestServiceException.class)
    public void testQueryMinimumChars() throws ApplicationException {
        sut.search(null, new FolderSearchRequest("te", 0, 20));
    }
    @Test(expected=GenericRestServiceException.class)
    public void testNegativeStart() throws ApplicationException {
        sut.search(null, new FolderSearchRequest("test", -1, 20));
    }

Only issue with this was that I wont know what exception is thrown. It seems Junit has a concept of ExpectedExceptions and you can check much more about expected exceptions than the annotation way.

The same code can be written as

public class FolderSearchEndPointUnitTest {
    private FolderSearchEndPoint sut = new FolderSearchEndPoint();
    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @Test
    public void testQueryMinimumChars() throws ApplicationException {
        expectedException.expect(GenericRestServiceException.class);
        expectedException.expectMessage(FolderSearchEndPoint.INVALID_QUERY_MSG);
        sut.search(null, new FolderSearchRequest("te", 0, 20));
    }

    @Test
    public void testNegativeStart() throws ApplicationException {
        expectedException.expect(GenericRestServiceException.class);
        expectedException.expectMessage(FolderSearchEndPoint.INVALID_START_MESSAGE);
        sut.search(null, new FolderSearchRequest("test", -1, 20));
    }
}

Edit: I had to change this code to do more detail exception checking and that is mentioned at http://astartupguy.blogspot.com/2014/09/junit-expected-exception-checking-part2.html

Comments

Popular posts from this blog

Consistency

Consistency is important as it makes people feel at home and instills trust in the brand. The iOS ecosystem makes it easy for people to transition from one device type to other because it has a consistent look and feel, actions and this way the brain doesn't have to apply any cognitive effort when moving from iPhone to iPad to a Mac. I learned about Keynotopia recently and was enlightened by its application. I saw the transition of a bootstrapped Startup to a consistent Enterprise Startup and this is why I see the importance of consistency introduction in every department at Startup as early as possible. The UX style needs to be consistent with the entire product line and a company-wide style guide maintained by the UX group is absolutely important from very early days of the Startup. Your button color, fonts, logos need to be consistent in each product and even company presentations. From the moment a customer lands on your Marketing website to the trial perio...

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 advance She researched trail...

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 d...