SORU
13 Temmuz 2009, PAZARTESİ


Sistem için JUnit test.dışarı.()sertifika

Kötü tasarlanmış eski bir uygulama için JUnit testleri yazmak istiyorum ve standart çıktıya hata bir sürü mesaj yazıyor. getResponse(String request) yöntemi doğru davranır zaman bir XML yanıt verir:

@BeforeClass
public static void setUpClass() throws Exception {
    Properties queries = loadPropertiesFile("requests.properties");
    Properties responses = loadPropertiesFile("responses.properties");
    instance = new ResponseGenerator(queries, responses);
}

@Test
public void testGetResponse() {
    String request = "<some>request</some>";
    String expResult = "<some>response</some>";
    String result = instance.getResponse(request);
    assertEquals(expResult, result);
}

Ama hatalı biçimlendirilmiş XML veya istek anlamaz null döner ve standart çıktıya bir şeyler yazar.

JUnit çıkış konsolu savunmak için herhangi bir yolu var mı? Gibi durumlarda yakalamak için:

System.out.println("match found: "   strExpr);
System.out.println("xml not well formed: "   e.getMessage());

CEVAP
13 Temmuz 2009, PAZARTESİ


ByteArrayOutputStream ve Sistemi kullanarak.setXXX basittir:

private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();

@Before
public void setUpStreams() {
    System.setOut(new PrintStream(outContent));
    System.setErr(new PrintStream(errContent));
}

@After
public void cleanUpStreams() {
    System.setOut(null);
    System.setErr(null);
}

örnek test durumları:

@Test
public void out() {
    System.out.print("hello");
    assertEquals("hello", outContent.toString());
}

@Test
public void err() {
    System.err.print("hello again");
    assertEquals("hello again", errContent.toString());
}

Komut satırı seçeneği test etmek için bu kodu (sürüm Sürüm dizesi, vs vs çıkışları olduğunu beyan ederek) kullandım

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Cristina Landa

    Cristina Lan

    28 Ocak 2010
  • Michelle Phan

    Michelle Pha

    18 Temmuz 2006
  • TechShowsYou

    TechShowsYou

    3 Mart 2011