如何在Java 8中模拟年度



我试图用Mockito模拟YearMonth.class

我正在调用getYeargetMonth方法,因此我想控制我正在测试的方法中使用的值。但是我概述了以下问题。

org.mockito.exceptions.base.MockitoException: 
Cannot mock/spy class java.time.YearMonth
Mockito cannot mock/spy following:
  - final classes
  - anonymous classes
  - primitive types
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl$1.withBefores(JUnit45AndHigherRunnerImpl.java:27)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:246)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

测试具有以下对模拟的定义

YearMonth yearMonthMock = mock(YearMonth.class);

我正在测试的方法具有以下

YearMonth yearMonth = YearMonth.now(ZoneId.of("UTC"));
String year = Integer.toString(yearMonth.getYear());
String month = Integer.toString(yearMonth.getMonthValue());

我如何解析例外以继续使用YearMonth.class,或者我应该使用替代方法。

通过静态方法访问日期时间特定功能很棘手。您正在尝试模拟取决于系统时钟的静态方法。这使得在不采取一些严厉措施的情况下很难嘲笑/间谍。(就像更改系统上的时间。大声笑)

解决此问题的一种方法可能是将您可以控制的接口背后的该功能访问。

public interface YearMonthProvider {
    YearMonth now(ZoneId zone);
}

生产中的实施看起来像是这样

public class SystemYearMonth implements YearMonthProvider {
    public YearMonth now(ZoneId zone) {
        return YearMonth.now(zone);
    }
}

正在测试的方法取决于抽象而不是具体

public class MyClass {
    private YearMonthProvider clock;
    public MyClass(YearMonthProvider clock) {
        this.clock = clock;
    }
    public void MyMethod(){
        //...other code
        YearMonth yearMonth = clock.now(ZoneId.of("UTC"));
        String year = Integer.toString(yearMonth.getYear());
        String month = Integer.toString(yearMonth.getMonthValue());
        //... other code
    }
}

现在,这允许灵活地根据需要交换依赖关系。

在测试中

//Arrange
YearMonth yearMonthStub = YearMonth.of(2016,11);
YearMonthProvider yearMonthMock = mock(YearMonthProvider.class);
when(yearMonthMock.now(ZoneId.of("UTC"))).thenReturn(yearMonthStub);
MyClass sut = new MyClass(yearMonthMock);
//Act
sut.MyMethod();
//Assert
//...

相关内容

  • 没有找到相关文章

最新更新