2017-02-25 170 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)