我使用Spring Initializr生成了一个Spring Boot Web应用程序,使用嵌入式Tomcat和包作为可执行的JAR文件。
这是主要的应用程序:
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);
}
}
这是测试类中的一个:
@ContextConfiguration(classes={ProductionPersistenceConfig.class})
@RunWith(SpringRunner.class)
public class JdbcRemoteUnitRepositoryTests {
@Autowired
private JdbcRemoteUnitRepository repository;
@Test
public void testGetAllGuardians() throws DataAccessException, SQLException {
assertEquals(2, 2);
}
}
当我启动应用程序时。从Eclipse一切都很好,没有问题。但是当我在Eclipse中运行测试时出现了这个错误:
3:24:05.680 [main] ERROR org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@e45f292] to prepare test instance [com.tdk.repository.JdbcRemoteUnitRepositoryTests@5383967b]
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
答案 0 :(得分:1)
尝试使用注释@SpringBootTest
。
此外,您还可以找到有用的注释@Profile("test")
,利用它可以为测试环境设置另一个数据库。