لا يمكن تمرير الاستثناء من داخل رد الاتصال المجهول في اختبار إطار التشغيل

StackOverflow https://stackoverflow.com//questions/21003479

سؤال

لدي اختبار التكامل التالي كجزء من مشروع 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
}

لا أعتقد أن هذا يبدو بديهيًا جدًا.أي اقتراحات حول كيفية حل هذه المشكلة بطريقة أفضل؟

شكرًا!

هل كانت مفيدة؟

المحلول

حسنًا، وجدت الدالة Fail() :-)

import static org.junit.Assert.*;

وبهذا أستطيع أن أفعل ما يلي:

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

الذي يبدو أفضل بكثير على ما أعتقد.إذا كان بإمكانك التفكير في حلول بديلة، مثل طريقة لنشر الاستثناء أو ما شابه ذلك، فأنا أرغب في سماع ذلك.

(لي غبي)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top