gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:38:28 30 4
gpt4 key购买 nike

我正在尝试执行 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 中)中放置了断点,并在调试中运行它,并且它没有加入任何这些断点。

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

<小时/>

编辑

抱歉,我完全忘记了异常(exception):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

最佳答案

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

关于java - 在测试中使用@SpringApplicationConfiguration会抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30466447/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com