전달할 수 없기에 예외가 내부에서 익명의 콜백에서 플레이 테스트
-
20-12-2019 - |
문제
나는 다음과 같은 통합 테스트의 일환으로 플레이 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)
내 코드입니다.
나를 잡을 수 있 제외하고 테스트를 성공으로 내부 catch 섹션이지만,재생 Framework 테스트가 없장에서 성공을()또는 fail()메소드(수)결과에서 나에게 무언가를 하 바보처럼 이
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
}
나는 생각하지 않는 이것은 아주 직관적입니다.어떤 제안을 해결하는 방법 이 더 나은 방식으로?
감사합니다!
해결책
확인을 발견하고,fail()function:-)
import static org.junit.Assert.*;
이 나가는 다음을 수행할 수 있습니다.
try {
oAuthClient.accessToken(request);
fail();
} catch (OAuthProblemException e) {
//success
}
보이는 훨씬 더 나는 생각한다.의 생각할 수 있는 다른 솔루션과 같은 방법으로 전파하는 예외 또는 이와 유사한,사랑하는 것을 자세한 정보가 필요합니다.
(멍게)
제휴하지 않습니다 StackOverflow