【问题标题】:Running test with Spring Boot Test使用 Spring Boot Test 运行测试
【发布时间】: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


【解决方案1】:
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;
}

}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2017-07-28
      • 2016-12-22
      • 1970-01-01
      • 2017-07-30
      • 2021-01-16
      • 2020-02-10
      • 2014-11-28
      • 2015-02-10
      • 1970-01-01
      相关资源
      最近更新 更多