我在我的 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 返回由第一次交互声明的值。
- 我想了解为什么这种交互没有改变。
我正在寻找如何重新设计应用程序的想法。我的目标是
时间:
//具有某些值的 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/