【问题标题】:Using @SpringApplicationConfiguration in test throws Exception?在测试中使用@SpringApplicationConfiguration 会引发异常?
【发布时间】:2015-05-26 18:44:03
【问题描述】:

我正在尝试执行 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);
    }

}

还有我的 ServicesConfiguration:

    @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

【问题讨论】:

  • 您缺少@Autowired ClientDAO 否则在ClientServiceImpl 中添加默认构造函数
  • 这是为什么呢?我想通过构造函数注入它,在 Bean 方法中这就是我正在做的。我不明白的是为什么不进入那种方法,你知道原因吗?谢谢你的回答
  • 然后注解构造函数

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


【解决方案1】:

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

【讨论】:

  • 太棒了!但是为什么在我运行服务时不会发生这种情况?感谢您的回答!
  • 你认为哪个更正确?保留@Service 还是保留 Bean 方法创建?
  • 至于为什么该应用程序工作我不能说,也许它是“意外”由于不同的顺序而工作的,但我只是在猜测,看不出一个明显的原因。
  • 至于更喜欢哪个版本,两者之间存在差异(至少在您的情况下)。 IIRC 原型范围意味着您可以在任何时候将其注入某个地方的服务的新实例,而简单的 @Service 注释将创建一个单例。如果您想要的只是单例并且您的 @Bean 注释方法除了将依赖项结合在一起之外不包含任何逻辑,我会使用 @Service 并让 spring 为我处理依赖项。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多