2016-04-07 22 views
0

testinde multipe özel durum için junit @Rule, expectMessage(), matcher'ı kullanma Android projemde, aynı @Test ile farklı mesajlarla aynı istisnayı birkaç kez atabilecek bir sınıfı test etmek istiyorum. Testimin belirli bir mesaj listesi için geçmesini ve başkaları için başarısız olmasını istiyorum. Junit biraz araştırma yapma1 test yönteminde

Bunu kullanarak @Rule, expectMessage() ve hamcrest eşleyici uygulamaya çalışmıştır.

Uygulamam şu anda here numaralı "Özel eşleştirici" ye dayanmaktadır.

@RunWith(AndroidJUnit4.class) 
public class TestException extends ApplicationTestCase { 

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

    public TestException(){ 
     super(AplicatyApplication.class); 
    } 

    @Test 
    public void testException() throws Exception { 
     thrown.expect(IndexOutOfBoundsException.class); 
     thrown.expectMessage(new MatchesPattern("*")); 
     Dummy.exec(0); 
     // do more stuff here ... 
     Dummy.exec(1); 
     // ... 
     Dummy.exec(2); 
     // ... 
     Dummy.exec(3); // I want my test to fail here 
     // ... 
    } 

    class MatchesPattern extends TypeSafeMatcher<String> { 
     private String pattern; 

     public MatchesPattern(String pattern) { 
      this.pattern = pattern; 
     } 

     @Override 
     protected boolean matchesSafely(String item) { 
      return item.matches(pattern) 
        && 
        item.startsWith("My message") 
        && (
         item.endsWith("1") 
         || 
         item.endsWith("2") 
        ); 
     } 

     @Override 
     public void describeTo(Description description) { 
      description.appendText("matches pattern ").appendValue(pattern); 
     } 

     @Override 
     protected void describeMismatchSafely(String item, Description mismatchDescription) { 
      mismatchDescription.appendText("does not match"); 
     } 
    } 

    static class Dummy { 
     static void exec(int i){ 
      if(i == 0) 
       return; 
      if(i == 1) 
       throw new IndexOutOfBoundsException("My message1"); 
      if(i == 2) 
       throw new IndexOutOfBoundsException("My message2"); 
      if(i == 3) 
       throw new IndexOutOfBoundsException("My message3"); 
     } 
    } 
} 

bu testi ben Dummy.exec(1); yürütme, eşleştirici sadece bir kez denir görebilirsiniz yayınlanıyor.

matchesSafely(String item), true değerini döndürür ve bu test Geçti ile biter. Tüm bunlar, @Rule anlayışımı ile Tamam görünüyor. Bir istisna bekliyordum: Anladım; Verilen bir mesajı bekliyordum: Anladım.

İlk istisna atıldıktan sonra testimin yürütülmesine devam etmenin bir yolunu bulamıyorum.

  • mümkün birden fazla istisna bir test yöntemine atılan kontrol etmek @Rule kullanmak mı, yoksa her özel durum iletisi test tipik try/catch kullanmak zorunda yapın:

    Sorularım şunlardır yakala blok?

  • Bu tür endişeleri test etmenin başka bir daha zarif yolu var mı?

cevap

1

Test yöntemini, her gereksinim için birden çok sınamaya bölmenizi öneririm.

@Test 
public void testException_1() throws Exception { 
    thrown.expect(IndexOutOfBoundsException.class); 
    thrown.expectMessage("My message1"); 

    Dummy.exec(1); 
} 

durumda ben try-catch ve ErrorCollector ile kuracağına, bir test yönteminde olması gerekiyor.

@Test 
public void testException_1() throws Exception { 
    try { 
     Dummy.exec(1); 
     fail(); 
    } catch (IndexOutOfBoundsException e) { 
     errorCollector.checkThat(e.getMessage(), is("My message1")); 
    } 

    try { 
     Dummy.exec(2); 
     ... 
    } ... 
} 

Özel bir Matcher oluşturmamaya çalışacağım.

İlgili konular