相关疑难解决方法(0)

Spring boot:@ConfigurationProperties 对测试不满意

我在测试执行中遇到此异常:

UnsatisfiedDependencyException:创建名为“net.gencat.transversal.espaidoc.mongo.GridFSTest”的bean时出错:通过字段“resourceProperties”表达的不满意依赖;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“net.gencat.transversal.espaidoc.ResourcesConfigProperties”类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选。

所以,我认为信息是如此清楚不过了:ResourcesConfigProperties is not satisfied。

我的测试:

RunWith(SpringRunner.class)
@SpringBootTest()
public class GridFSTest {

    @Autowired
    private GridFsTemplate gridFsTemplate;

    @Autowired
    private ResourcesConfigProperties resourceProperties;

    public URL getHugeResource() {
        try {
            return Paths
                .get(this.resourceProperties.getHuge())
                .toUri()
                .toURL();
        } catch (MalformedURLException e) {
            return null;
        }
    }

    @Test
    public void storeHugeFile() throws IOException {
        URL resource = this.getHugeResource();

        this.gridFsTemplate.store(
            resource.openStream(),
            resource.getPath(),
            "mime"
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

并且ResourcesConfigProperties是:

@ConfigurationProperties(prefix = "files")
public class ResourcesConfigProperties {

    private String huge; …
Run Code Online (Sandbox Code Playgroud)

spring-boot spring-boot-test

3
推荐指数
2
解决办法
4536
查看次数

标签 统计

spring-boot ×1

spring-boot-test ×1