2017-09-15 10 views
0

Spring Initializrを使用して、埋め込みTomcatとパッケージを実行可能なJARファイルとして使用して、SpringブートWebアプリケーションを生成しました。SpringBoot injection:アプリケーションでOKですが、テストではありません。

これはメインアプリケーションである:

package com.tdk; 


import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.CommandLineRunner; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

import com.tdk.repository.JdbcRemoteUnitRepository; 


@SpringBootApplication 
public class TdkDeviceManagerApp implements CommandLineRunner { 


    @Autowired 
    private JdbcRemoteUnitRepository jdbcRemoteUnitRepository; 

    public static void main(String[] args) { 
     SpringApplication app = new SpringApplication(TdkDeviceManagerApp.class); 
     app.run(args); 
    } 

    @Override 
    public void run(String... arg0) throws Exception { 

     jdbcRemoteUnitRepository.getUnitsWithoutFirmwareInfo(); 
    } 
} 

この1つのリポジトリクラス:

package com.tdk.repository; 

    /** 
    * @author tdk © 
    * @version 1.0 
    * @since 15 sept. 2017 
    */ 
    @Transactional 
    @Service 
    public class JdbcRemoteUnitRepository { 
    .. 
    } 



    @ContextConfiguration(classes={ProductionPersistenceConfig.class}) 
    @RunWith(SpringRunner.class) 
    public class JdbcRemoteUnitRepositoryTests { 

     @Autowired 
     private JdbcRemoteUnitRepository repository; 

     @Test 
     public void testGetAllGuardians() throws DataAccessException, SQLException {   
      assertEquals(2, 2); 
     } 

    } 

、これはテストクラスの1:

@ContextConfiguration(classes={ProductionPersistenceConfig.class}) 
@RunWith(SpringRunner.class) 
public class JdbcRemoteUnitRepositoryTests { 

    @Autowired 
    private JdbcRemoteUnitRepository repository; 

    @Test 
    public void testGetAllGuardians() throws DataAccessException, SQLException {   
     assertEquals(2, 2); 
    } 

} 

Iアプリを起動します。 Eclipseからは問題はありません。私はEclipseでテストを実行すると、私はこのエラーを得た:

3:24:05.680 [main] ERROR org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframewor[email protected]e45f292] to prepare test instance [[email protected]] 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.tdk.repository.JdbcRemoteUnitRepositoryTests': Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.tdk.repository.JdbcRemoteUnitRepository' 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) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.tdk.repository.JdbcRemoteUnitRepository' 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.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) 
    ... 28 common frames omitted 
+1

テストを実行するためにSpring Bootを使用していないためです。 –

答えて

1

は注釈@SpringBootTestを使用してみてください。

また、便利なアノテーション@Profile("test")を見つけることができます。これにより、テスト環境用に別のデータベースを設定することができます。

関連する問題