簡體   English   中英

自動填充JUnit設置和拆卸

[英]Automatically populate JUnit setup and teardown

對於我為我的應用程序執行的大部分測試,我有一個共同的setUp和tearDown。

當我創建一個新的JUnit Test Case類時,我有一個eclipse選項可以自動創建setUp()和tearDown()方法。

有沒有辦法更改默認的setUp和TearDown方法以獲得一些自定義代碼行?

因此,當我使用setUp方法創建一個新的測試類時,我得到:

@Before
public void setUp() throws Exception
{
    UnitTestSetup.standardSetup();
}

導入應該不是問題,因為它會在保存時自動導入所需的包。

如果您希望更改Eclipse新JUnit測試用例向導的輸出,那么在不重寫插件源代碼的情況下這是不可能的。 生成的代碼硬編碼到向導中

        buffer.append("void "); //$NON-NLS-1$
        buffer.append(methodName);
        buffer.append("() throws "); //$NON-NLS-1$
        buffer.append(imports.addImport("java.lang.Exception")); //$NON-NLS-1$
        buffer.append(" {}"); //$NON-NLS-1$
        buffer.append(delimiter);

JUnit支持繼承。 將常見的@Before@After應用於@Before一種方法是將它們放在一個公共父類中,並在測試用例中繼承它:

public class Common {
     @Before
     public void setUp() {
         ...
     }
     @After
     public void tearDown() {
         ...
     }
}

public class MyTest extends Common {
    @Test
    public void test() {
        // Common.setUp() will have been run before this test
        // Common.tearDown() will run after the test
    }
}

如果你真的覺得它值得,你可以編寫自己的注釋處理器。 然后讓它處理具有@Test注釋的所有內容,或者可以擁有自己的@VerySpecialTestWithGeneratedSetupAndTeardown注釋,並將其添加到要處理的類/方法中。

這將是一大堆代碼,所以你需要有大量的測試來證明這一點,但是這里是編寫注釋處理器的一個很好的演練: http//hannesdorfmann.com/annotation-processing/annotationprocessing101/

為什么不創建一個類來執行代碼來執行設置,另一個類來執行拆卸並使用這些類來封裝分別進行設置和拆除所需的邏輯。 然后,您可以從任何地方和何時需要它來調用此邏輯。

像這樣的東西:

商務課程:

package com.foo.bar.businesslogic.woozinator;

public class Woozinator {

    public void doSomething() {
        System.out.println("Doing something.");
        System.out.println("Done.");
    }

}

測試類:

package com.foo.bar.businesslogic.woozinator;

import org.junit.Test;

import com.foo.bar.businesslogic.testutil.WidgitUserTestSetupUtil;
import com.foo.bar.businesslogic.testutil.WidgitUserTestTearDownUtil;

public class WoozinatorTest {

    @Test
    public void shouldPassTest() {
        WidgitUserTestSetupUtil.setUp();
        // do test stuff here
        WidgitUserTestTearDownUtil.tearDown();
    }

}

設置類:

package com.foo.bar.businesslogic.testutil;

public class WidgitUserTestSetupUtil {

    public static void setUp() {
        System.out.println("Setting up widget environment for widget user test");
        // do stuff here
        System.out.println("Done setting up environment");
    }

}

撕下課程:

package com.foo.bar.businesslogic.testutil;

public class WidgitUserTestTearDownUtil {

    public static void tearDown() {
        System.out.println("Doing woozinator tear down.");
        // undo stuff here
        System.out.println("Done with woozinator tear down.");
    }

}

您可以編寫一個JUnit規則來擴展TestWatcher並將代碼移動到startingfinished的方法。

public class YourRule extends TestWatcher {
  @Override
  protected void starting(Description description) {
    UnitTestSetup.standardSetup();
    //more of your code
  }

  @Override
  protected void finished(Description description) {
    //your code
  }
}

在每個測試中使用此規則:

public class YourTest {
  @Rule
  public final YourRule yourRule = new YourRule();

  ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM