Question

I have the following integration test as a part of a Play Framework project.

@Test(expected = OAuthProblemException.class)
public void testFailedCredentials() throws OAuthProblemException, OAuthSystemException {
    running(testServer(3333, fakeApplication(inMemoryDatabase())), HTMLUNIT, new F.Callback<TestBrowser>() {
        public void invoke(TestBrowser browser) throws OAuthProblemException, OAuthSystemException {
            OAuthClientRequest request = OAuthClientRequest
                    .tokenLocation("http://localhost:3333/oauth2/access_token")
                    .setGrantType(GrantType.PASSWORD)
                    .setClientId("client_id")
                    .setClientSecret("client_secret")
                    .setUsername("username")
                    .setPassword("password_should_not_pass")
                    .buildBodyMessage();

            OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());

            oAuthClient.accessToken(request); //throws OAuthProblemException
        }
    });
}

The line oAuthClient.accessToken(request); throws OAuthProblemException, which is correct. My problem is that because of the anonymous inner callback, I don't have the possibility to propagate the exception up and do something like @Test(expected = OAuthProblemException.class) as in my code.

I could catch the exception and mark the test as a success inside the catch section, but Play Framework testing has no built in the success() or fail() methods (that I could find), resulting in me doing something stupid like this

try {
    oAuthClient.accessToken(request);
    assertThat("This credentials should fail").isEmpty();//Force a fail
} catch (OAuthProblemException e) {
    assertThat(e).isInstanceOf(OAuthProblemException.class);//Force a success. I could probably skip this line
}

I don't think this looks very intuitive. Any suggestions how to solve this in a better manner?

Thanks!

Was it helpful?

Solution

Ok, found the fail() function :-)

import static org.junit.Assert.*;

With this I can do the following:

try {
    oAuthClient.accessToken(request);
    fail();
} catch (OAuthProblemException e) {
    //success
}

Which looks much better I think. If you can think of alternative solutions, such as a way to propagate the exception up or similar, I would love to hear about it.

(Stupid me)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top