Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 配置不适用于jacksontester,但适用于jackson_Java_Json_Spring Boot_Jackson - Fatal编程技术网

Java 配置不适用于jacksontester,但适用于jackson

Java 配置不适用于jacksontester,但适用于jackson,java,json,spring-boot,jackson,Java,Json,Spring Boot,Jackson,我在弹簧靴上使用JacksonTester时遇到问题。我不想在序列化对象上保留null属性,因此我在application.properties和application-test.properties上都设置了此属性 spring.jackson.default-property-inclusion=non_null 问题在于,配置在默认环境中按预期工作,但未在测试环境中应用。请注意,我正在使用JacksonTester测试我的代码 下面是一个显示我的问题的简短示例: @ActiveProfi

我在弹簧靴上使用JacksonTester时遇到问题。我不想在序列化对象上保留null属性,因此我在application.properties和application-test.properties上都设置了此属性

spring.jackson.default-property-inclusion=non_null
问题在于,配置在默认环境中按预期工作,但未在测试环境中应用。请注意,我正在使用JacksonTester测试我的代码

下面是一个显示我的问题的简短示例:

@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@JsonTest
public class PanelistDTOJsonTest {

    @Autowired
    private JacksonTester<PanelistDTOOutput> jsonOutput;


    @Test
    public void testSerializeToOutputWithTokenNull() throws Exception {

        PanelistDTOOutput panelist = new PanelistDTOOutput();

        // This test should pass but the property spring.jackson.default-property-inclusion=non_null is ignored by JakcsonTester
        assertThat(this.jsonOutput.write(panelist)).doesNotHaveEmptyJsonPathValue("@.token");

    }
}
测试未通过,并显示以下错误消息:

java.lang.AssertionError: Expected a non-empty value at JSON path "@.token" but found: null
生成的JSON如下所示:

{"id":null,"created":null,"token":null}

请注意,我使用的是spring boot 1.5.4和jackson 2.9.1。

您的测试使用的是
application-dev.properties
文件吗?我没有application-dev.properties文件,所以为了测试,我在测试中添加了一个文件,但它没有改变任何内容。文档说明您应该以这种方式初始化JacksonTester:JacksonTester.initFields(这是新的ObjectMapper());您在什么地方初始化了它吗?
{"id":null,"created":null,"token":null}