获取“无法找到@SpringBootConfiguration,您需要使用@ContextConfiguration”

时间:2018-07-30 13:07:33

标签: java spring spring-boot

我正在尝试一个简单的springboot应用程序,其中我想使用属性文件中的值初始化一个组件,然后使用该组件运行junit test

我的maven项目层次结构如下:

src/main/java
 +--com.company.project.package1
     +--Component1.java
src/test/java
 +--com.company.project.package1
     +--Component1Test.java
src/test/resources
 +--application.properties

pom.xml依赖项

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.0.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>2.7.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>2.0.3.RELEASE</version>
        <scope>test</scope>
    </dependency>

Component1.java

package com.company.project.package1;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Component1 
{   
    @Value("$property1")
    private String attribute1;

    public void method1()
    {
        System.out.println("Attr 1: " + attribute1);
    }
}

Component1Test.java

package com.company.project.package1;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Component1Test
{
    @Test
    public void method1Test()
    {
        Component1 comp1 = new Component1();
        comp1.method1();
    }
}

application.properties

property1:val1

通过测试返回:

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

我在这里想念什么?

0 个答案:

没有答案