Play Framework Testで匿名のコールバックの内側から例外を渡すことはできません。
-
20-12-2019 - |
質問
Play Frameworkプロジェクトの一部として、次の統合テストを行います。
@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
}
});
}
.
線oAuthClient.accessToken(request);
はOAuthProblemExceptionをスローします。これは正しいです。私の問題は、匿名の内部コールバックのために、例外を伝播する可能性があり、コードのように@Test(expected = OAuthProblemException.class)
のようなものをする可能性があります。
私は例外を引いて、キャッチセクションの中の成功としてテストをマークすることができましたが、Play Frameworkテストは成功()または失敗()メソッド(私が見つけることができたこと)に構築されていません。この
のように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
}
.
これは非常に直感的に見えるとは思わない。より良い方法でこれを解決する方法はありますか?
ありがとう!
解決
OK、fail()関数: - )
import static org.junit.Assert.*;
.
これを使えば、次のことができます。
try {
oAuthClient.accessToken(request);
fail();
} catch (OAuthProblemException e) {
//success
}
.
これは私が思うともっとよく見えます。例外を上書きする方法などの代替ソリューションを考えることができれば、私はそれについて聞いてほしいのです。
(愚かな私)
所属していません StackOverflow