我决定从配置 spring bean 的旧 skool xml 样式转移到 javaconfig 方式。我查看了很多示例,但是每次运行单元测试时似乎都遇到了问题。我收到以下信息:
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)}
我的问题是我的 spring-data 存储库似乎被我的配置类中定义的 @ComponentScan 拾取。日志中没有太多其他内容,而且我似乎没有做任何异国情调的事情。
我正在使用以下版本:
- 爪哇 7
- spring-data-mongodb 1.4.1
- 春天 3.2.8
我的测试用例:
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());
}
}
我的配置类:
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;
}
}
我的弹簧数据存储库:
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> {
}