2017-05-29 93 views
0

我有一個基本的SpringBoot應用程序。使用Spring初始化程序,嵌入式Tomcat,Thymeleaf模板引擎和包作爲可執行JAR文件。Junit在Spring Boot中測試不會注入服務

我有這樣的服務:

@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.springframewor[email protected]dd3b207] to prepare test instance [[email protected]] 
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) 

回答

0

這解決我的問題:

@RunWith(SpringRunner.class) 
@SpringBootTest(classes = TdkApplication.class) 
public class SigfoxRestApiServiceTests { 
. 
} 
1

這裏:

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

您不指定ContextConfiguration註釋的classes屬性。

您應該使用您的根Spring配置類設置此屬性,該類設置您的所有配置,特別是設置bean的TdkRestApiService
例如,如果你的根彈簧的配置類是MyConfig,你可以指定它:

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

可以對Spring documentation discussing of detecting test configuration的更多信息。

下面的摘錄:

If you’re familiar with the Spring Test Framework, you may be used to using @ContextConfiguration(classes=…​) in order to specify which Spring @Configuration to load. Alternatively, you might have often used nested @Configuration classes within your test.

相關問題