小编kor*_*lar的帖子

覆盖单个Spring Boot测试的属性

考虑以下示例:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
    properties = {
        "some.property=valueA"
    })
public class ServiceTest {
    @Test
    public void testA() { ... }

    @Test
    public void testB() { ... }

    @Test
    public void testC() { ... }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用SpringBootTest注释的properties属性来设置some.property此测试套件中所有测试的属性值。现在,我想为其中一个测试(假设testC)设置此属性的另一个值,而不影响其他测试。我该如何实现?我已经阅读了Spring Boot docs的“测试”一章,但是没有找到与我的用例匹配的内容。

java spring spring-boot

8
推荐指数
3
解决办法
4210
查看次数

解析时DateTimeFormatter的替代区域如何​​工作?

解析时,我对DateTimeFormatter' withZone方法的行为感到困惑。根据它的文档:

解析时,要考虑两种不同的情况。如果直接从文本中解析了区域,则可能是因为使用了DateTimeFormatterBuilder.appendZoneId(),所以此覆盖区域无效。如果没有解析任何区域,则该覆盖区域将包含在解析结果中,可在该结果中用于构建瞬间和日期时间。

基于此以及DateTimeFormatter.ISO_DATE_TIME解析时区的事实,我希望以下两项测试能够通过。

@Test
public void testNoZoneInInput() {
    final ZonedDateTime expected = ZonedDateTime.of(2017, 2, 2, 9, 0, 0, 0, ZoneId.of("UTC"));
    final ZonedDateTime actual = ZonedDateTime.parse("2017-02-02T10:00:00", DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.of("UTC+1")));
    Assert.assertTrue("Expected " + expected + ", got " + actual + " instead.", expected.isEqual(actual));
}

@Test
public void testWithZoneInInput() {
    final ZonedDateTime expected = ZonedDateTime.of(2017, 2, 2, 9, 0, 0, 0, ZoneId.of("UTC"));
    final ZonedDateTime actual = ZonedDateTime.parse("2017-02-02T09:00:00Z", DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.of("UTC+1")));
    Assert.assertTrue("Expected " + expected + ", got " + actual …
Run Code Online (Sandbox Code Playgroud)

java java-time

5
推荐指数
1
解决办法
188
查看次数

标签 统计

java ×2

java-time ×1

spring ×1

spring-boot ×1