使用Spring Boot Test运行测试

时间:2019-01-15 10:27:36

标签: java unit-testing spring-boot junit

您好,我正在使用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软件包?

2 个答案:

答案 0 :(得分:0)

package ch.hcuge.dpi.dpidata.exporter.clinerion;

import ch.hcuge.deja.sv.SVException;
import ch.hcuge.deja.sv.SVStatusException;
import ch.hcuge.dpi.dpidata.common.repository.model.Eds;
import ch.hcuge.dpi.dpidata.exporter.swisssos.core.AneuxGeneralConsentService;
import ch.hcuge.dpi.dpidata.mongo.dao.EdsMongoDBDao;
import ch.hcuge.deja.util.logging.LogFactory;

import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class ExtractDataFromPush {
private static Logger LOG = LogFactory.getLogger(ExtractDataFromPush.class);

@Autowired
private EdsMongoDBDao edsRepository;
@Autowired
private AneuxGeneralConsentService aneuxGeneralConsentService;

// base class to handle data pushes

// remark: it is assumed that to each case loaded, a reference to patientId is also available
// and stored in objects, so that it is later possible to query patientInformation


public List<String> getCaseIdsForDates(Date startDate, Date endDate) {
    List<String> caseIds = edsRepository.findByDpiDate(startDate, endDate).stream()
            .filter(eds -> hasGeneralConsent(eds))
            .map(eds -> eds.getId()).collect(Collectors.toList());
    // Only patient 10493746
    // caseIds.add("10429904"); // EDS from Albus DUMBLEDORE 97005400

    return caseIds;
}

private boolean hasGeneralConsent(Eds eds)  {
    try {
        return aneuxGeneralConsentService.hasGeneralConsent(eds.getPatient().getId());
    } catch (SVException | SVStatusException e) {
        return false;
    }
}

private Eds getEdsWithId(String caseId) {
    Eds eds = this.edsRepository.findById(caseId);
    LOG.info("getEDS returns {}", eds);

    return eds;
}

}

答案 1 :(得分:0)

我解决了这个问题,非常感谢;由于某些软件包路径,该错误是在我的上下文.xml中