简体   繁体   中英

Running test with Spring Boot Test

Hello I am doing some unit testing with spring boot and Junit and I turn into this error:

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)}

My Java test class:

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"));
    }

}

What's wrong with my code?

This is my application xml context:

<?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"/>

I am pretty new to testing especially with java and spring-boot; Do I have to do some build to reinstall some packages or maybe I have to import some other spring packages?

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;
}

}

I resolved the issue thanks a lot; the error was in my context .xml due to some package path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM