java - Java 测试时间。更改 spock stub 值

标签 java spring spock java-time

我在我的 Spring 应用程序中定义了 bean。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Clock;

@Configuration
public class ClockConfiguration {

    @Bean
    Clock getSystemDefaultZoneClock() {
        return Clock.systemDefaultZone();
    }
}

然后在我的测试中我想 stub 这个bean。

import io.github.jhipster.config.JHipsterConstants
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.context.annotation.Bean
import org.springframework.test.context.ActiveProfiles
import spock.lang.Specification
import spock.mock.DetachedMockFactory

import java.time.Clock
import java.time.Instant
import java.time.LocalDateTime

import static java.time.ZoneId.systemDefault

@ActiveProfiles(profiles = JHipsterConstants.SPRING_PROFILE_TEST)
@EnableConfigurationProperties
@SpringBootTest(classes = [MyApp])
class ClockTest extends Specification {


    @Autowired
    Clock clock

    //2018-04-01 at 10:00am
    private static Instant REFERENCE_DATE_TIME = LocalDateTime.of(2018, 4, 1, 10, 0)
        .atZone(systemDefault())
        .toInstant()

    //2018-04-01 at 10:00am
    private static Instant OTHER_REFERENCE_DATE_TIME = LocalDateTime.of(2018, 4, 2, 10, 0)
        .atZone(systemDefault())
        .toInstant()

    def "should return different date"() {
        when:
        clock.instant() >> REFERENCE_DATE_TIME
        then:
        clock.instant() == REFERENCE_DATE_TIME
        when:
        clock.instant() >> OTHER_REFERENCE_DATE_TIME
        then:
        clock.instant() == OTHER_REFERENCE_DATE_TIME

    }


    @TestConfiguration
    static class Mocks {
        def detachedMockFactory = new DetachedMockFactory()

        @Bean
        Clock clock() {
            return detachedMockFactory.Stub(Clock)
        }
    }

}

由于第二个断言,此测试失败。我的 stub bean 返回由第一次交互声明的值。

  1. 我想了解为什么这种交互没有改变。
  2. 我正在寻找如何重新设计应用程序的想法。我的目标是

    时间:

    //具有某些值的 stub 时钟。

    //做一些逻辑。多次使用时钟。

    //将时钟更改为另一个实例。

    //做一些其他的逻辑。多次使用时钟。

    然后:

    //做出一些断言。 (例如检查时差)

最佳答案

我提出了解决方案,我使用可以计算返回值的功能。

http://spockframework.org/spock/docs/1.2/all_in_one.html#_computing_return_values

并在那里替换返回值。

import spock.lang.Specification

import java.time.Clock
import java.time.Instant
import java.time.LocalDateTime

import static java.time.ZoneId.systemDefault

class ClockStubTest extends Specification {
    private static Instant REFERENCE_DATE_TIME = LocalDateTime.of(2018, 4, 1, 10, 0)
        .atZone(systemDefault())
        .toInstant()

    private static Instant OTHER_REFERENCE_DATE_TIME = LocalDateTime.of(2018, 4, 2, 10, 0)
        .atZone(systemDefault())
        .toInstant()

    Clock clock = Stub()

    def "should return different date"() {
        given:
        def now
        clock.instant() >> { now }
        when:
        now = REFERENCE_DATE_TIME
        then:
        clock.instant() == REFERENCE_DATE_TIME
        clock.instant() == REFERENCE_DATE_TIME

        when:
        now = OTHER_REFERENCE_DATE_TIME
        then:
        clock.instant() == OTHER_REFERENCE_DATE_TIME
        clock.instant() == OTHER_REFERENCE_DATE_TIME
        clock.instant() == OTHER_REFERENCE_DATE_TIME
    }
}

关于java - Java 测试时间。更改 spock stub 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53723386/

相关文章:

unit-testing - Gradle忽略了我的Spock单元测试之一,并没有告诉我为什么

java - 在 Java 中,当评估构造函数调用的参数抛出异常时会发生什么?

java - 无法将子节点添加到树节点

java - 在 Chrome 中加载小程序时出现 ClassNotFound 异常

unit-testing - 带有继承的单元测试方法 Controller 调用

grails - 打印grails测试应用失败到控制台

java - Tomcat 8 安装在 windows 7.Localhost 空白页面

java - 如何处理 Spring 中已发布事件的异常

java - Web 应用程序上下文和服务上下文之间的双向依赖

spring - 如何使用 Spring Data JPA 和 Spring Security 实现 AuditorAware?