2013-07-18 10 views
7

JUnit en ExpectedException kuralı kullanımıyla ilgili bir soru var junit ExpectedException Rule 4.7 biri böyle istisnalar test edebilirsiniz JUnit başlayarak (@Test (beklenen = Exception.class sonra çok daha iyidir)) : Bir ExpectedException Kuralı kullanarak bir testte birkaç istisnayı nasıl test edebilirim? Burada önerildiği gibi</p> <p>:

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

@Test 
public void testFailuresOfClass() { 
Foo foo = new Foo(); 
exception.expect(Exception.class); 
foo.doStuff(); 
} 

Şimdi bir test yönteminde çeşitli istisnalar test etmek gerekli ve şu testi çalıştırdıktan sonra yeşil bir bar var ve böylece her bir test geçti düşündüm.

@Test 
public void testFailuresOfClass() { 
Foo foo = new Foo(); 

exception.expect(IndexOutOfBoundsException.class); 
foo.doStuff(); 

//this is not tested anymore and if the first passes everything looks fine 
exception.expect(NullPointerException.class); 
foo.doStuff(null); 

exception.expect(MyOwnException.class); 
foo.doStuff(null,""); 

exception.expect(DomainException.class); 
foo.doOtherStuff(); 
} 

Ancak bir süre sonra, ilk kontrol geçtikten sonra test metninin bittiğini fark ettim. Bu en az söylemek için belirsiz. 3 nolu toplantıda bu mümkün oldu ... İşte benim sorum:

ExpectedException Kuralı kullanarak bir testte birkaç istisnayı nasıl test edebilirim?

+0

Hala 3 yolunu kullanabilirsiniz, ancak @ Test (beklenen) veya ExpectedException kullanılarak mümkün değildir. –

+0

Korktum ;-) – Lonzak

+1

O kadar da kötü değil. Genellikle her testte bir şeyi test etmek daha iyidir. Aynı sınamadaki bir listenin boyutunu, boyutunu ve içeriğini sınamak, ancak bir sınamada iki kavramsal farklı şeyi sınamanız gerekmez. Daha az okunabilir. Test başına bir şeyi test ederseniz, 'testException' yerine, nelerin test edildiğini tanımlamak için yöntem adını kullanabilirsiniz. –

cevap

6

Kısa cevap: Yapamazsınız.

İlk aramaya - foo.doStuff() - bir istisna atarsa, asla foo.doStuff(null)'a ulaşmazsınız. Birkaç içine testini bölmek gerekecek (ve bu önemsiz bir durum için ben ExpectedException olmadan, basit gösterimde geri dönüyor teklif ediyorum):

private Foo foo; 

@Before 
public void setUp() { 
foo = new Foo(); 
} 

@Test(expected = IndexOutOfBoundsException.class) 
public void noArgsShouldFail() { 
foo.doStuff(); 
} 

@Test(expected = NullPointerException.class) 
public void nullArgShouldFail() { 
foo.doStuff(null); 
} 

@Test(expected = MyOwnException.class) 
public void nullAndEmptyStringShouldFail() { 
foo.doStuff(null,""); 
} 

@Test(expected = DomainException.class) 
public void doOtherStuffShouldFail() { 
foo.doOtherStuff(); 
} 

gerçekten bir ve yalnızca bir test istiyorsanız, size hiçbir hata atılır eğer fail ve beklediğiniz şeyler yakalayabilirsiniz:

@Test 
public void testFailuresOfClass() { 
Foo foo = new Foo(); 

try { 
    foo.doStuff(); 
    fail("doStuff() should not have succeeded"); 
} catch (IndexOutOfBoundsException expected) { 
    // This is what we want. 
} 
try { 
    foo.doStuff(null); 
    fail("doStuff(null) should not have succeeded"); 
} catch (NullPointerException expected) { 
    // This is what we want. 
} 
// etc for other failure modes 
} 

Bu alır oldukça dağınık oldukça hızlı, ilk beklenti başarısız olursa başka bir şey de başarısız olursa da, ve, sen görmez, sorun giderme sırasında can sıkıcı olabilir.

İlgili konular