简体   繁体   中英

Spring unit test issue with javaconfig

I've decided to move away from the old skool xml style of configuring spring beans to the javaconfig way. I have looked at plenty of examples, but I seem to be having an issue each time I run my unit test. I receive the following:

Caused by:
 20:55:37.664 [DEBUG] [TestEventLogger]        org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.x.repository.InvoiceRepo com.x.repository.InvoiceRepoTest.invoiceRepo; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.x.repository.InvoiceRepo] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

My problem is that my spring-data repository does to appear to be picked up by the @ComponentScan defined in my config class. There is not much else in the logs, and I don't seem to be doing anything exotic.

I am using the following versions:

  • Java 7
  • spring-data-mongodb 1.4.1
  • spring 3.2.8

My testcase:

package com.x.repository;

import com.x.config.RepositoryCfg;
import com.x.model.Invoice;
import com.x.model.InvoiceStatus;
import com.x.model.Rate;
import java.math.BigDecimal;
import java.util.Date;
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.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@ActiveProfiles("test")
@ContextConfiguration(classes = {MongoTestCfg.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class InvoiceRepoTest {

    private static final BigDecimal SAVE_TEST_TOTAL = new BigDecimal("800.20");
    private static final Long SAVE_TEST_ISSUER_ID = 686L;
    private static final Long SAVE_TEST_RECIPIENT_ID = 852L;
    private static final int SAVE_TEST_DISCOUNT_DAYS = 2;
    private static final BigDecimal SAVE_TEST_DISCOUNT_RATE = new BigDecimal("5.00");
    private static final Date SAVE_TEST_ISSUED_DATE = new Date();
    private static final Date SAVE_TEST_PAYMENT_DATE = new Date();

    @Autowired
    private InvoiceRepo invoiceRepo;

    @Test
    public void successSave() {
        Invoice invoice = new Invoice();
        invoice.setTotal(SAVE_TEST_TOTAL);
        invoice.setIssuer(SAVE_TEST_ISSUER_ID);
        invoice.setRecipient(SAVE_TEST_RECIPIENT_ID);
        invoice.setDiscount(new Rate(SAVE_TEST_DISCOUNT_DAYS, SAVE_TEST_DISCOUNT_RATE));
        invoice.setIssued(SAVE_TEST_ISSUED_DATE);
        invoice.setPaymentDate(SAVE_TEST_PAYMENT_DATE);
        invoice.setStatus(InvoiceStatus.PAYMENT_DATE_SELECTED);

        Invoice saved = invoiceRepo.save(invoice);
        Assert.assertNotNull(saved);
        Assert.assertNotNull(saved.getId());
        Assert.assertEquals(invoice.getDiscount(), saved.getDiscount());
        Assert.assertEquals(SAVE_TEST_ISSUED_DATE, saved.getIssued());
        Assert.assertEquals(SAVE_TEST_TOTAL, saved.getTotal());
        Assert.assertEquals(InvoiceStatus.PAYMENT_DATE_SELECTED, saved.getStatus());
        Assert.assertEquals(SAVE_TEST_RECIPIENT_ID, saved.getRecipient());
        Assert.assertEquals(SAVE_TEST_ISSUER_ID, saved.getIssuer());
    }
}

My configuration class:

package com.x.repository;

import com.x.config.MongoAppCfg;
import com.github.fakemongo.Fongo;
import com.mongodb.Mongo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;

@Configuration
@ComponentScan(basePackages = {"com.x.repository"})
@Profile("test")
public class MongoTestCfg extends AbstractMongoConfiguration {

    @Override
    public Mongo mongo() throws Exception {
        return new Fongo("test-db").getMongo();
    }

    @Override
    protected String getDatabaseName() {
        return "test";
    }

    @Override
    protected String getMappingBasePackage() {
        return MongoAppCfg.ENTITY_PACKAGE;
    }
}

My spring-data repository:

package com.x.repository;

import com.x.model.Business;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface InvoiceRepo extends CrudRepository<Invoice, Long> {
}

Consulted the spring-data github project and looked at the unit tests. There is an annotation :

@EnableMongoRepositories(basePackages = {"com.x.repository"})

Which when added to the configuration class, it sorted out the wiring in of the repositories I defined.

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