Spring Boot中的Junit测试不会注入服务

时间:2017-05-29 20:52:38

标签: java spring spring-mvc spring-boot junit

我有一个基本的SpringBoot应用程序。使用Spring Initializer,嵌入式Tomcat,Thymeleaf模板引擎和包作为可执行的JAR文件。

我有这项服务:

@Service
public class TdkRestApiService {
    ...
}

我想测试一下:

@ContextConfiguration(classes={TdkApplicationConfig.class, TdkDevelopmentConfig.class}) 
@RunWith(SpringRunner.class)
public class TdkRestApiServiceTests {


    /**
     * The object being tested.
     */
    @Autowired
    TdkRestApiService tdkRestApiService;

    @Test
    public void getCallbacksByDeviceTypeTest () throws IOException {

        tdkRestApiService.getCallbacksByDeviceType("2", "3");

    }
}

但是我收到了一个错误:

ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@dd3b207] to prepare test instance [com.tdk.backend.service.TdkRestApiServiceTests@6db9f5a4]
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.tdk.backend.service.TdkRestApiServiceTests': Unsatisfied dependency expressed through field 'tdkRestApiService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.tdk.backend.service.TdkRestApiService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:386)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)

4 个答案:

答案 0 :(得分:3)

这里:

@ContextConfiguration
@RunWith(SpringRunner.class)
public class TdkRestApiServiceTests {

您没有指定classes注释的ContextConfiguration属性。

您应该使用根Spring配置类设置此属性,该配置类设置所有配置,尤其是TdkRestApiService bean。 例如,如果您的根Spring配置类是MyConfig,则可以指定它:

@ContextConfiguration(classes = MyConfig.class)
@RunWith(SpringRunner.class)
public class TdkRestApiServiceTests {

您可以在Spring documentation discussing of detecting test configuration上获得更多信息。

提取物下方:

  

如果您熟悉Spring Test Framework,您可能会习惯   使用@ContextConfiguration(classes = ...)来指定哪个   Spring @Configuration加载。或者,您可能经常这样做   在测试中使用嵌套的@Configuration类。

答案 1 :(得分:2)

这解决了我的问题:

<add key="UnobtrusiveJavaScriptEnabled" value="true" />

答案 2 :(得分:0)

通过创建一个类似于以下内容的TestConfig.class,可以找到一个整洁的解决方案:

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class TestConfig {

}

然后,您可以使用 @Autowired 组件将其添加到测试类中,如下所示:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestConfig.class)
public class Test { 

   @Autowired
   private Component component;

}

之后,您将可以在测试类中使用Spring组件。

答案 3 :(得分:0)

尝试将@SpringBootTest 添加到测试类中:

@SpringBootTest
@RunWith(SpringRunner.class)
public class TdkRestApiServiceTests {
   ...
}