Java @使用弹簧启动启动器测试时,部件不能在测试中工作

Java @使用弹簧启动启动器测试时,部件不能在测试中工作,java,spring,unit-testing,spring-boot,component-scan,Java,Spring,Unit Testing,Spring Boot,Component Scan,我正在尝试使用spring boot starter test测试项目中的@服务和@存储库类,而@Autowired不适用于我正在测试的类 单元测试: @RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration(classes = HelloWorldConfiguration.class //@SpringApplicationConfiguration(classes = HelloWorldRs.class) //@

我正在尝试使用spring boot starter test测试项目中的
@服务
@存储库
类,而
@Autowired
不适用于我正在测试的类

单元测试:

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = HelloWorldConfiguration.class
//@SpringApplicationConfiguration(classes = HelloWorldRs.class)
//@ComponentScan(basePackages = {"com.me.sbworkshop", "com.me.sbworkshop.service"})
//@ConfigurationProperties("helloworld")
//@EnableAutoConfiguration
//@ActiveProfiles("test")
// THIS CLASS IS IN src/test/java/ AND BUILDS INTO target/test-classes
public class HelloWorldTest {

    @Autowired
    HelloWorldMessageService helloWorldMessageService;

    public static final String EXPECTED = "je pense donc je suis-TESTING123";

    @Test
    public void testGetMessage() {
        String result = helloWorldMessageService.getMessage();
        Assert.assertEquals(EXPECTED, result);
    }
}
服务:

@Service
@ConfigurationProperties("helloworld")
// THIS CLASS IS IN /src/main/java AND BUILDS INTO target/classes
public class HelloWorldMessageService {

    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String  message) {
        this.message=message;
    }

}
单元测试上的注释类注释代表了我试图让它工作的各种事情。测试和项目包位于相同的包路径中,
@ComponentScan
从我的入口点(
@RestController
类with main方法)就可以正常工作。服务
@ComponentScan
@Autowire
在src/main/java端的我的
@RestController
类中没有问题,但在测试中没有。我需要在我的
@Configuration
类中将它再次添加为
@Bean
,以便
@Autowired
工作。该类在其他方面在范围内很好,我可以从测试中引用和实例化它。问题似乎是
@ComponentScan
似乎无法正确遍历我的测试运行程序类路径中的多个条目,在本例中为/target/test类和/target/classes

我使用的IDE是IntelliJ IDEA 13

更新-以下是HelloWorlds及其配置:

@RestController
@EnableAutoConfiguration
@ComponentScan
public class HelloWorldRs {

    //    SPRING BOOT ENTRY POINT - main() method
    public static void main(String[] args) {
        SpringApplication.run(HelloWorldRs.class);
    }

    @Autowired
    HelloWorldMessageService helloWorldMessageService;

    @RequestMapping("/helloWorld")
    public String helloWorld() {
        return helloWorldMessageService.getMessage();
    }

}

@配置
公共类HelloWorldConfiguration{
@豆子
公共地图(){
返回新的HashMap();
}
//这个bean是作为@ComponentScan问题的解决方法手动添加的
@豆子
公共HelloWorldMessageService HelloWorldMessageService(){
返回新的HelloWorldMessageService();
}
//这个bean是作为@ComponentScan问题的解决方法手动添加的
@豆子
公共HelloWorlds HelloWorlds(){
返回新的HelloWorlds();
}
}

我不知道这是否是解决方案,但不要使用默认包(即不要直接将*.java放在“src/main/java”中),也绝对不要在默认包中使用
@ComponentScan
@EnableAutoConfiguration
)。当应用程序试图扫描类路径上的所有内容(包括所有Spring库)时,您将在启动时终止应用程序。

首先,我建议使用更新的
@RunWith(SpringRunner.class)
,但这没有什么区别,它只是更短(并且是推荐的)

其次,从
@EnableAutoConfiguration
中,我看到您使用的是spring boot,这当然是一件好事。不直接使用
@ComponentScan
有一些很好的理由。你能试试下面的吗

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes=YourApplication_or_other_Configuration.class)
public class HelloWorldTest {
... etc.

HelloWorlsRs
看起来像什么?您的测试用例不是一个配置,所有这些注释都与配置无关。请参阅更新的帖子了解HelloWorlsRs等。发生了什么?你解决问题了吗?如何操作?从
@EnableAutoConfiguration
中,我看到您正在使用spring boot,这当然是一件好事。您的项目中是否有
@springboot应用程序
?您是否可以尝试将您的测试注释为
@SpringBootTest
?重点是-仅尝试让组件加载,因此尝试从src/main/java加载,以确保没有遗漏任何内容
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes=YourApplication_or_other_Configuration.class)
public class HelloWorldTest {
... etc.