【发布时间】:2019-01-15 10:27:35
【问题描述】:
您好,我正在使用 spring boot 和 Junit 进行一些单元测试,结果出现此错误:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ch.hcuge.dpi.dpidata.exporter.clinerion.ExtractDataFromPushTest': Unsatisfied dependency expressed through field 'extractDataFromPush'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'ch.hcuge.dpi.dpidata.exporter.clinerion.ExtractDataFromPush' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我的 Java 测试类:
package ch.hcuge.dpi.dpidata.exporter.clinerion;
import ch.hcuge.dpi.dpidata.common.util.DateUtil;
import java.util.Date;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:testApplicationContextClinerion.xml")
public class ExtractDataFromPushTest {
@Autowired
private ExtractDataFromPush extractDataFromPush;
@Test
public void getCaseIdsForDatesTest() {
Date from = DateUtil.of(2019, 1, 1);
Date to = DateUtil.of(2019, 1,10);
List<String> caseIds = extractDataFromPush.getCaseIdsForDates(from, to);
Assert.assertEquals(74, caseIds.size());
Assert.assertTrue( caseIds.contains("45698"));
}
}
我的代码有什么问题?
这是我的应用程序 xml 上下文:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<bean id="DejaProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath*:ch/hcuge/deja/ConfigTests.properties</value>
<value>classpath*:application.properties</value>
</list>
</property>
</bean>
<!-- Expose the properties so other parts of the spring config can use them -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true"/>
<property name="ignoreUnresolvablePlaceholders" value="false"/>
<property name="properties" ref="DejaProperties"/>
</bean>
<import resource="classpath*:/META-INF/deja-test-L2-services-application-context.xml"/>
<context:component-scan base-package="ch.hcuge.dpi">
<!--<context:exclude-filter type="regex"-->
<!--expression="ch.hcuge.dpi.dpidata.exporter.config.ExporterWebConfig"></context:exclude-filter>-->
<!--<context:exclude-filter type="regex"-->
<!--expression="ch.hcuge.dpi.dpidata.exporter.test.config.TestConfig"></context:exclude-filter>-->
<!--<context:exclude-filter type="regex"-->
<!--expression="ch.hcuge.dpi.dpidata.exporter.swisssos.core.AneuxPatientStudyService"></context:exclude-filter>-->
<!--<context:exclude-filter type="regex"-->
<!--expression="ch.hcuge.dpi.dpidata.exporter.swisssos.core.AneuxGeneralConsentService"></context:exclude-filter>-->
</context:component-scan>
<context:annotation-config/>
<task:annotation-driven executor="taskExecutor" scheduler="taskScheduler"/>
<task:executor id="taskExecutor" pool-size="5"/>
<task:scheduler id="taskScheduler" pool-size="10"/>
我对测试非常陌生,尤其是使用 java 和 spring-boot; 我是否必须进行一些构建以重新安装某些软件包,或者我是否必须导入其他一些 spring 软件包?
【问题讨论】:
-
你能发布
ExtractDataFromPush类吗? -
由于您使用应用程序上下文将依赖项连接到您的 lass,这不是单元测试,而是集成测试。单元测试只测试一个类并提供依赖作为模拟。例如,请参阅为单元测试提供支持的 Mockito 框架。
-
那么如何在
ExtractDataFromPush类中测试我的getCaseIdsForDates?
标签: java unit-testing spring-boot junit