JUNIT - 测试方法的顺序

时间:2017-02-25 14:33:14

标签: junit

我有一个名为SequenceExample的简单JAVA程序,它按顺序执行三个方法:

  1. 洗涤()
  2. 干燥()
  3. 折叠()
  4. 我想写一个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");
        }
    }
    

2 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分: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)