SORU
21 Kasım 2008, Cuma


Sistem çağrı yöntemlerini Nasıl test.Java: () çıkış?

Bazı girişler System.exit() aramalı birkaç yöntem var. Ne yazık ki, bu gibi durumlarda test JUnit sonlanmasına neden olur! Yeni bir iş Parçacığı yöntem çağrıları koyarak System.exit() JVM, yalnızca geçerli iş parçacığı sona erer beri yardımcı olarak görünmüyor. Bu ile başa çıkmak için ortak bir düzen var? Örneğin, System.exit() için bir saplama subsitute miyim?

[DEĞİŞTİR] sınıf soru içinde JUnit test etmeye çalışıyorum. aslında komut satırı aracıdır Belki JUnit sadece iş için doğru aracı değil mi? Tamamlayıcı regresyon test araçları için önerilerinizi bekliyoruz (iyi JUnit ve EclEmma ile entegre tercihen bir şey).

CEVAP
21 Kasım 2008, Cuma


Nitekim Derkeiler.com öneriyor:

  • Neden System.exit() ?

Sistem ile sonlandırmak yerine.çıkış(whateverValue), neden denetimsiz bir özel durum değil mi? Normal kullanır drift onca yolu için JVM son-hendek tutucu ve kapalı senaryonu aşağı (sürece karar yakalamaya bir yerlerde, hangi olabilir yararlı bir gün).

JUnit senaryoda rapor edecek JUnit çerçeve tarafından yakalanmış olacak bu tür-ve-böyle bir test başarısız oldu ve sorunsuz gelecek için birlikte hareket.

  • System.exit() aslında JVM çıkış önlemek için:

Sistem arama engelleyen bir güvenlik yöneticisi ile çalıştırmak için deneme durumu değiştirmeyi deneyin.çıkın, sonra SecurityException yakalamak.

public class NoExitTestCase extends TestCase 
{

    protected static class ExitException extends SecurityException 
    {
        public final int status;
        public ExitException(int status) 
        {
            super("There is no escape!");
            this.status = status;
        }
    }

    private static class NoExitSecurityManager extends SecurityManager 
    {
        @Override
        public void checkPermission(Permission perm) 
        {
            // allow anything.
        }
        @Override
        public void checkPermission(Permission perm, Object context) 
        {
            // allow anything.
        }
        @Override
        public void checkExit(int status) 
        {
            super.checkExit(status);
            throw new ExitException(status);
        }
    }

    @Override
    protected void setUp() throws Exception 
    {
        super.setUp();
        System.setSecurityManager(new NoExitSecurityManager());
    }

    @Override
    protected void tearDown() throws Exception 
    {
        System.setSecurityManager(null); // or save and restore original
        super.tearDown();
    }

    public void testNoExit() throws Exception 
    {
        System.out.println("Printing works");
    }

    public void testExit() throws Exception 
    {
        try 
        {
            System.exit(42);
        } catch (ExitException e) 
        {
            assertEquals("Exit status", 42, e.status);
        }
    }
}

Aralık 2012 Güncelleme:

Will in the comments java.lang.System kullanan System Rules test kodu için JUnit(4.9 ) kurallar bütünü kullanmayı öneriyor.
Bu başlangıçta söz vardıStefan BirknerAralık 2011 his answer.

System.exit(…)

ExpectedSystemExit kural System.exit(…) denir doğrulamak için kullanın.
Çıkış durumu da doğrulayacak.

Örneğin:

public void MyTest {
    @Rule
    public final ExpectedSystemExit exit = ExpectedSystemExit.none();

    @Test
    public void noSystemExit() {
        //passes
    }

    @Test
    public void systemExitWithArbitraryStatusCode() {
        exit.expectSystemExit();
        System.exit(0);
    }

    @Test
    public void systemExitWithSelectedStatusCode0() {
        exit.expectSystemExitWithStatus(0);
        System.exit(0);
    }
}

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Avast

    Avast

    27 NİSAN 2006
  • Facebook Developers

    Facebook Dev

    24 ŞUBAT 2009
  • Rayone GB

    Rayone GB

    14 Temmuz 2007