在测试中使用@SpringApplicationConfiguration抛出异常?

时间:2015-05-26 18:44:04

标签: java spring spring-boot spring-data spring-test

我正在尝试执行DAO测试,所以我希望Spring Boot构建实现,所以我有这个测试:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = FakeServiceRunner.class)
public class ClientDAOTests {

    @Autowired
    private ClientDAO dao;

    @Test
    public void testFindAllClients() {
        ....
        Page<Client> clients = this.dao.findAll(new PageRequest(0, 30, null));
        // Asserts..
    }
}

这是我的FakeServiceRunner。我这样调用它是因为这不是我运行服务的真正类,(我无法访问该类),所以我构建了这个“FakeServiceRunner”,以获得所有Spring Boots功能。所以这就是那个课:

@SpringBootApplication
@Import({ServicesConfiguration.class})
public class FakeServiceRunner {

    public static void main(String[] args) {
        SpringApplication.run(FakeServiceRunner.class, args);
    }

}

我的服务配置:

    @Configuration
    @Import({PersistenceConfiguration.class, TransformersConfiguration.class})
    public class ServicesConfiguration {

        @Autowired
        private ClientDAO clientDAO; //Comes from PersistenceConfiguration

        @Autowired
        @Qualifier("domainMapper")
        private MapperFacade mapper; //Comes from TransfomersConfiguration

        @Bean
        @Scope(BeanDefinition.SCOPE_PROTOTYPE)
        public ClientService clientService() {
            ClientServiceImpl clientService = new ClientServiceImpl(this.clientDAO);
            clientService.setMapper(this.mapper);
            return clientService;
        }

    }

最后,我的ClientServiceImpl类:

@Service
public class ClientServiceImpl
    implements ClientService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ClientServiceImpl.class);

    private ClientDAO dao;
    private MapperFacade mapper;

    public ClientServiceImpl(ClientDAO dao) {
        this.dao = dao;
    }

    public void setMapper(MapperFacade mapper) {
        this.mapper = mapper;
    }

    // Service methods...
}

所以我在构造函数和clientService方法(在ServicesConfiguration.class中)中放置了断点,并在Debug中运行它,并且它没有加入任何这些断点。

你知道发生了什么吗?问候。

修改

对不起,我完全忘记了例外:P在这里:

ERROR (TestContextManager.java:215) - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@4a8f5f75] to prepare test instance [com.example.movies.domain.ClientDAOTests@1be0c344]
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:94)
    at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:72)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:200)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:259)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:261)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:219)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientServiceImpl' defined in file [/home/despubuntu/Documents/Workspace/example-backend-development/example-backend-development/example-backend-development-domain/target/classes/com/example/movies/domain/feature/client/service/ClientServiceImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.movies.domain.feature.client.service.ClientServiceImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.example.movies.domain.feature.client.service.ClientServiceImpl.<init>()
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1101)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.test.SpringApplicationContextLoader.loadContext(SpringApplicationContextLoader.java:100)
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:68)
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:86)
    ... 25 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.movies.domain.feature.client.service.ClientServiceImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.example.movies.domain.feature.client.service.ClientServiceImpl.<init>()
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:85)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1094)
    ... 40 more
Caused by: java.lang.NoSuchMethodException: com.example.movies.domain.feature.client.service.ClientServiceImpl.<init>()
    at java.lang.Class.getConstructor0(Class.java:2892)
    at java.lang.Class.getDeclaredConstructor(Class.java:2058)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80)
    ... 41 more

1 个答案:

答案 0 :(得分:2)

您尝试将ClientServiceImpl定义为bean两次,一次使用config类中的@Bean方法,一次使用@Service注释在类本身中(后者将由@SpringBootApplication触发的组件扫描获取)。因此,要么按照iamiddy的建议添加@Autowired注释,要么使用@Service,要么删除注释并在config类中创建。