2017-02-25 193 views
2

序列我有以下簡單的Java程序被稱爲SequenceExample執行順序三種方法:)))JUNIT - 測試方法

  1. 清洗(
  2. 乾燥(
  3. 摺疊(

我想寫一個JUNIT測試,以確保這個序列得到維護。在未來換句話說,如果代碼被改變爲執行Drying()Washing()在那之前我的JUnit應當失敗,併爲一條消息,「洗衣順序是不是爲了」

請讓我知道如果你有任何想法如何實現這一目標。 謝謝, Dayanand。

public class SequenceExample { 

    public static void main(String[] args) { 

     //Shows Sequential Steps for laundry 

     Washing(); //Sequence#1 
     Drying(); //Sequence#2 
     Folding(); //Sequence#3 
    } 

    private static void Washing(){ 
     System.out.println("Washing - This is Step One of Laundry"); 
    } 

    private static void Drying(){ 
     System.out.println("Drying - This is Step Two of Laundry"); 
    } 

    private static void Folding(){ 
     System.out.println("Folding - This is Step Three of Laundry"); 
    } 
} 

回答

1

我認爲這個檢查應該在代碼中執行。否則,代碼客戶端可能會允許不可接受的序列。
在單元測試中,你可以檢查如預期SequenceExample類行爲:

  • 如果序列是有效的,沒有異常上升。
  • 如果序列無效,則異常會阻止客戶端代碼繼續進行。

一些提示:

1)你窩在SequenceExample很多東西,不使用的SequenceExample實例。如果您不寫實用程序方法,則應該傾向於使用實例和實例方法。

2)方法名稱應該是動詞不定式形式(約定):洗滌和不洗滌,乾燥和摺疊。

3)每種方法都有特定的行爲,應該從客戶端代碼中調用。使他們私密似乎並不理想。

4)你可以在SequenceExample中引入一個字段來維護當前狀態。 Java枚舉可以完成這項工作。
每個方法都可以在執行任務之前檢查狀態,並在狀態不是預期的時候拋出異常。
在方法結束時,狀態將被修改。

這裏是你的類的修改版本:

public class SequenceExample { 

    public enum State { 
     WASHING, DRYING, FOLDING, 
    } 

    private State state; 

    public static void main(String[] args) { 
     // Shows Sequential Steps for laundry 
     SequenceExample sequenceExample = new SequenceExample(); 
     sequenceExample.wash(); // Sequence#1 
     sequenceExample.dry(); // Sequence#2 
     sequenceExample.fold(); // Sequence#3 
    } 

    public void wash() { 
     if (state != null) { 
      throw new IllegalStateException("state should be null"); 
     } 
     System.out.println("Washing - This is Step One of Laundry"); 
     state = State.WASHING; 
    } 

    public void dry() { 
     if (state != State.WASHING) { 
      throw new IllegalStateException("state should be WASHING"); 
     } 
     System.out.println("Drying - This is Step Two of Laundry"); 
     state = State.DRYING; 
    } 

    public void fold() { 
     if (state != State.DRYING) { 
      throw new IllegalStateException("state should be WASHING"); 
     } 
     System.out.println("Folding - This is Step Three of Laundry"); 
     state = State.FOLDING; 
    } 
} 

下面是測試類調用的良好秩序的方法和 3測試不等等等異常的測試上升。

import org.junit.Test; 

public class SequenceExampleTest { 

    SequenceExample sequenceExample = new SequenceExample(); 

    @Test 
    public void sequenceCorrect() throws Exception { 
     sequenceExample.wash(); 
     sequenceExample.dry(); 
     sequenceExample.fold(); 
    } 

    @Test(expected = IllegalStateException.class) 
    public void sequenceNotCorrectCaseOne() throws Exception { 
     sequenceExample.dry(); 
     sequenceExample.wash(); 
    } 

    @Test(expected = IllegalStateException.class) 
    public void sequenceNotCorrectCaseTwo() throws Exception { 
     sequenceExample.dry(); 
     sequenceExample.fold(); 
    } 

    @Test(expected = IllegalStateException.class) 
    public void sequenceNotCorrectCasethree() throws Exception { 
     sequenceExample.wash(); 
     sequenceExample.fold(); 
    } 
} 
0

你應該能夠利用Mockito窺視你的測試對象和驗證方法被稱爲按預期的順序。

@Test 
public void laundry() { 

    // given 
    Laundry subject = spy(new Laundry()); 

    // when 
    subject.wash(); 
    subject.dry(); 
    subject.fold(); 

    // then 
    InOrder inOrder = inOrder(subject); 
    inOrder.verify(subject).wash(); 
    inOrder.verify(subject).dry(); 
    inOrder.verify(subject).fold(); 
} 

如果由於某種原因,未來的開發者認爲這是一個明智的想法摺疊溼的衣物,則測試將失敗,出現以下信號:

org.mockito.exceptions.verification.VerificationInOrderFailure: 
Verification in order failure 
Wanted but not invoked: 
laundry.fold(); 
-> at LaundryTest.laundry(LaundryTest.java:24) 
Wanted anywhere AFTER following interaction: 
laundry.dry(); 
-> at LaundryTest.laundry(LaundryTest.java:18)