SORU
18 Mart 2010, PERŞEMBE


Nasıl JUnit Test açıklama ile durum mesajım iddia mı?

@Test açıklama ile birkaç JUnit test yazdım. Eğer istisna ile birlikte ileti iddia etmek istiyorsam eğer benim test kontrol yöntemi bir özel durum oluşturursa, bu yüzden @Test ek açıklama JUnit ile yapmak için bir yolu var mı? AFAIK, JUnit 4.7 bu özelliği sağlamaz ama herhangi bir gelecek sürümleri sağlar mı? Biliyorum .NET ileti ve özel durum sınıfı onaylayabilirsiniz. Java dünyada benzer özellik arıyor.

Bu istediğim şey:

@Test (expected = RuntimeException.class, message = "Employee ID is null")
public void shouldThrowRuntimeExceptionWhenEmployeeIDisNull() {}

CEVAP
29 HAZİRAN 2011, ÇARŞAMBA


Bu gibi ExpectedException ile @Rule işareti kullanabilirsiniz:

@Rule
public ExpectedException expectedEx = ExpectedException.none();

@Test
public void shouldThrowRuntimeExceptionWhenEmployeeIDisNull() {
    expectedEx.expect(RuntimeException.class);
    expectedEx.expectMessage("Employee ID is null");
    // do something that should throw the exception...
}

ExpectedException docs örnek (şu anda) yanlış olduğunu unutmayın - ortak kurucu yok, ExpectedException.none() kullanmak zorunda.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Greater Than Gatsby Photoshop Actions & Lightroom Presets

    Greater Than

    11 ŞUBAT 2013
  • iBand IguJoo

    iBand IguJoo

    25 AĞUSTOS 2006
  • Pál Zoltán Illés

    Pál Zoltán

    30 NİSAN 2007