java Spring Boot 测试类不会注入 bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27798932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:24:52  来源:igfitidea点击:

Spring Boot test class wont inject beans

javaspringspring-bootspring-test

提问by rayman

I am using the following dependencies:

我正在使用以下依赖项:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'

 compile("com.caucho:resin-hessian:4.0.23")
    compile("mysql:mysql-connector-java")
    compile('org.springframework.boot:spring-boot-starter-web:1.2.0.RELEASE')
    compile("org.springframework.boot:spring-boot-starter-batch:1.2.0.RELEASE")
    compile("org.springframework.boot:spring-boot-starter-actuator")
    compile("javax.inject:javax.inject:1")
    compile('org.springframework.batch:spring-batch-admin-manager:1.3.0.RELEASE') {
        exclude module: 'slf4j-log4j12'
    }
    //SI
    compile("org.springframework.integration:spring-integration-core:$springIntegrationVersion")
    testCompile('org.springframework.boot:spring-boot-starter-test:1.2.0.RELEASE')

Created new test class:

创建新的测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
@ContextConfiguration(locations={"classpath:**/*.xml"})
@ComponentScan({"com.mycompany.notification.processor.service"})
public class DownloadFileTaskletTest  {

    @Autowired
     private DownloadFileTasklet downloadFileTasklet;

    @Test
    public void execute()
    {
        System.out.printf("test123");

    }
}

I created test case inside Spring boot However i get this error:

我在 Spring boot 中创建了测试用例但是我收到了这个错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'batch.DownloadFileTaskletTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mycompany.notification.processor.service.batch.tasks.DownloadFileTasklet batch.DownloadFileTaskletTest.downloadFileTasklet; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.mycompany.notification.processor.service.batch.tasks.DownloadFileTasklet] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

This is my application class:

这是我的应用程序类:

@ComponentScan({"com.mycompany.notification.processor.service"})
@EnableAutoConfiguration
@Configuration
@ImportResource({
        "classpath:integration-context.xml","classpath:launch-context.xml","classpath:applicationContext-NotificationProcessorService.xml"
})
//@Import({ ServletConfiguration.class, WebappConfiguration.class })
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        System.out.printf("hello man");
    }

}

Any idea why I cant inject beans into my test class?

知道为什么我不能将 bean 注入我的测试类吗?

I removed @ContextConfiguration,@ComponentScan,@IntegrationTes as suggested Now I see different exception:

我按照建议删除了 @ContextConfiguration,@ComponentScan,@IntegrationTes 现在我看到了不同的异常:

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:160)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:101)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-test</artifactId>
   <version>3.2.13.RELEASE</version>
</dependency>
0(ParentRunner.java:42) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:184) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) at org.junit.runner.JUnitCore.run(JUnitCore.java:157) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) Caused by: java.lang.IllegalArgumentException: Cannot load an ApplicationContext with a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration or @ContextHierarchy. at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:57) at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91) ... 28 more

回答by acoder2013

just remove this part:

只需删除这部分:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

instead ,using this:

相反,使用这个:

##代码##