Java 在JUnit测试中使用autowire的Spring引导字段注入不起作用

Java 在JUnit测试中使用autowire的Spring引导字段注入不起作用,java,spring-boot,junit,dependency-injection,autowired,Java,Spring Boot,Junit,Dependency Injection,Autowired,我想在测试中注射Demorgenariclescraper @RunWith(SpringJUnit4ClassRunner.class) public class DeMorgenArticleScraperTest { @Autowired private DeMorgenArticleScraper deMorgenArticleScraper; ... } DeMorgenArticleScraper组件本身正在进行一些配置,但IDE/编译器并没有对此抱怨

我想在测试中注射Demorgenariclescraper

@RunWith(SpringJUnit4ClassRunner.class)
public class DeMorgenArticleScraperTest {

    @Autowired
    private DeMorgenArticleScraper deMorgenArticleScraper;

    ...
}
DeMorgenArticleScraper组件本身正在进行一些配置,但IDE/编译器并没有对此抱怨

@Component
public class DeMorgenArticleScraper extends NewsPaperArticleScraper {

    @Autowired
    public DeMorgenArticleScraper(
            @Qualifier("deMorgenSelectorContainer") SelectorContainer selector,
            GenericArticleScraper genericArticleScraper,
            @Qualifier("deMorgenCompany") Company company) {
        super(selector, genericArticleScraper, company);
    }

    ...
}
用@Qualifier注释的构造函数参数在带有@Bean的Config.class中定义。类本身具有@Configuration。我想问题不在这里

IDE已经警告我,没有找到bean…必须在bean中定义自动连接的成员。但据我所知,它是在带有@Component注释的bean中定义的。当Spring启动应用程序可以启动时(当我注释掉测试类时),所有其他bean连接看起来都正常。

@RunWith(SpringJUnit4ClassRunner.class)


这似乎工作得很好:我看到SpringBoot启动并加载bean。我将暂时保留这个问题,以获得更好的建议。

@SpringBootTest
相当重,而且无论出于何种目的,它都将加载整个应用程序,相当重,并且会显著影响测试时间。根据您试图测试的内容,您可能需要进行调查

  • 切片测试,例如
    @JsonTest
    @DataJpaTest
    @WebMvcTest
    等。这些测试的好处不仅在于它们不会加载所有内容,因此速度更快,而且会尝试找出相关的配置
  • 普通的旧
    @ContextConfiguration
    ,并指向加载测试所需bean所需的相关
    @Configuration

    • 我也有同样的问题

    • 有一个服务,我想注入它进行测试,但我只想测试
      platformToSql(..)
      方法,而不启动整个Spring引导应用程序

      @服务
      公共类ConverterToSqImpl实现IConverterToSql{
      私有存储库;
      专用最终IUtilService utilService;
      公共转换器OSQLMain构造函数ServiceImpl(
      IWorkerUtilService workerUtilService,
      异构存储库(存储库){
      this.utilService=utilService;
      this.someRepository=someRepository;
      }
      @凌驾
      公共字符串platformToSql(字符串platformName){
      返回“平台=”+platformName+”;
      }
      @凌驾
      公共字符串方法whichUserDepositoryAndUtilBeans(){
      //一些我们不感兴趣的代码
      }
      }
      
    • 我创建了一个测试类。有必要模拟bean,因为测试方法不需要它们

      @RunWith(SpringRunner.class)
      @SpringBootTest(类={ConverterToSqlImpl.class})
      //@ContextConfiguration(类={ConverterToSqlImpl.class})
      @MockBean({@MockBean(IUtilService.class),@MockBean(isormonepository.class)})
      公共类SqlConverterConstructorTest{
      @自动连线
      专用IConverterToSqlMain构造函数服务convertToSql;
      @试验
      公共void平台测试(){
      字符串platformSql=convertToSql.platformToSql(“平台1”);
      字符串expectedSql=“platform='platform_1'”;
      Assert.assertEquals(platformSql,expectedSql);
      }
      }
      
    • @springbootest(classess={})
      加载到
      ApplicationContext
      中,只有在
      classess
      部分指出的classess,not将启动包含所有bean的整个应用程序


      另外,作为使用
      类={..}
      @SpringBootTest
      的替代方法,您可以使用
      @ContextConfiguration
      取消对行的注释。这是一种简单的老式风格。

      凭记忆评论,应该是“@InjectMocks”而不是“@Autowired”进入DeMorgenArticleScraperTest”。我不记得JUnitTest中有“@Autowired”Anotion,我在你的评论后对测试的类注释进行了修改,发现了一些有用的东西。谢谢。为了避免堆栈溢出,我建议您用解决方案回答自己的问题!很高兴它能帮上忙。我最好先回答,然后再评论:)啊,现在我找到我要找的了。我读过ContextConfiguration注释,但忽略了一个事实,即可以将组件注释类传递给它。泰!我同意这个答案,SpringBootTest对于我想要测试的东西来说太多了。我需要自动连接一个包含属性值的
      @Configuration
      类。在本例中,对我来说,它使用
      @ContextConfiguration
      ,加上
      @TestPropertySource(“prop file”)
      @EnableConfigurationProperties(Conf.class)
      使用切片测试,并使用
      @MockBean
      注释模拟某些依赖但“测试不需要”的bean
      @SpringBootTest
      @RunWith(SpringRunner.class)