Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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 使用@TestPropertySource时,属性在@ConfigurationProperties时始终为null_Java_Spring_Unit Testing_Dependency Injection - Fatal编程技术网

Java 使用@TestPropertySource时,属性在@ConfigurationProperties时始终为null

Java 使用@TestPropertySource时,属性在@ConfigurationProperties时始终为null,java,spring,unit-testing,dependency-injection,Java,Spring,Unit Testing,Dependency Injection,使用@TestPropertySource 配置文件application.yml: 配置类 @Component @Data @ConfigurationProperties(prefix="integration.some.configuration") public class SomeProperties { private String userInfo; } 服务类别: @Service @RequiredArgsConstructor @Ena

使用
@TestPropertySource

配置文件application.yml:

配置类

@Component
@Data
@ConfigurationProperties(prefix="integration.some.configuration")
public class SomeProperties {    
    private String userInfo;
}
服务类别:

@Service
@RequiredArgsConstructor
@EnableConfigurationProperties(SomeProperties.class)
public class SomeServiceImpl {
        private final SomeProperties someProperties;
    public String get() {
            return someProperties.getUserInfo();
        }
}
测试夹具:

@SpringJUnitConfig({
        SomeServiceImpl.class,
        SomeProperties.class})
@TestPropertySource(properties = {
        "integration.some.configuration.userInfo=someUser"
})
class SomeServiceImplTest {
    @Autowired
    SomeServiceImpl someServiceImpl;

    @Test
    void someTest() {
       String user = someServiceImpl.get();
       ....
    } 
}

在上面的测试中,注入的用户总是
null

您错过了自动连线

@Service
@RequiredArgsConstructor
@EnableConfigurationProperties(SomeProperties.class)
public class SomeServiceImpl {

    @Autowired // <--------
    private final SomeProperties someProperties;

    public String get() {
            return someProperties.getUserInfo();
        }
}
@服务
@所需参数构造函数
@EnableConfigurationProperties(SomeProperties.class)
公共类SomeServiceImpl{

@Autowired//您可以发布准确的配置和类型声明吗?我非常确定您没有使用
集成。在这种情况下,一些.configuration
属性和命名非常重要。我不能,我不允许这样做。我不确定这是否是问题所在,因为他没有共享全部源代码,他可能正在使用constructor或setter注射。
@Service
@RequiredArgsConstructor
@EnableConfigurationProperties(SomeProperties.class)
public class SomeServiceImpl {

    @Autowired // <--------
    private final SomeProperties someProperties;

    public String get() {
            return someProperties.getUserInfo();
        }
}