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

  • DancingIsAPassion

    DancingIsAPa

    29 AĞUSTOS 2009
  • FullMag

    FullMag

    15 ŞUBAT 2007
  • Liam Underwood

    Liam Underwo

    19 Mayıs 2009