使用 Mockito 测试摘要 class 未给出预期结果

Testing an abstract class with Mockito does not give the expected result

我有一个class类似于下面的结构

 public abstract class AbstractStep {
     private final Range RANGE;

     AbstractStep(AbstractStepBuilder builder) {
          RANGE = builder.range;   
     }


      public abstract static class AbstractStepBuilder {
           Range range;

           public AbstractStepBuilder setRange(int start, end end) {
                this.range = new Range(start, end);
                return self();
           }

           abstract AbstractStepBuilder self();
      }

      public static class Range() {
           private final int START;
           private final int END;

           private Range(int start, int end) {
                if(start < 0 || end < 0 || start >= end)
                      throw new IllegalArgumentException();
                START = start;
                END = end;
           }
      }
 }

我想在 AbstractStepBuilder 中测试 setRange(int, int) 以查看是否抛出了 IllegalArgumentException。我使用 TestNG 和 Mockito,并且在 this.

的帮助下尝试了以下使用
 final class RangeTest {
      AbstractStepBuilder builder;

      @BeforeSuite 
      void setup() {
           builder = Mockito.mock(AbstractStepBuilder.class);
           Mockito.when(builder.self()).thenReturn(null);
       }

     @Test(expectedExceptions = IllegalArgumentException.class)
     final void testCreatingRangeWithNegativeStart() {
          builder.setRange(-1, 2);
     }
}

这个测试失败了。我也试过用 Mockito.mock(AbstractStepBuilder.class, Mockito.CALLS_REAL_METHODS) 替换 Mockito.mock(AbstractStepBuilder.class) 作为 this 问题的最佳答案。

请注意,如果我将 CodeRange 作为其自身的外部 class,则此测试通过,因此我不认为它可能是测试本身。

为什么这个测试失败了,是否可以修复它而不必在测试中使用具体的 class?

您在模拟上调用一个方法,除非您告诉它,否则它永远不会抛出异常。你永远不会嘲笑你想要测试的class。

如果您想测试实际的 class,您需要创建步骤构建器的子class,创建一个实例并测试它。

我想你也可以创建一个间谍(通过 Mockito.spy(AbstractStepBuilder.class))来避免创建一个 subclass 只是为了测试。