我决定放弃使用旧的skool xml样式配置spring bean到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存储库似乎被我的config类中定义的@ComponentScan选中。日志中没有其他内容,我似乎没有做任何异国情调。
我使用以下版本:
我的测试用例:
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;
}
}
我的spring-data存储库:
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> {
}
答案 0 :(得分:1)
咨询了spring-data github项目并查看了单元测试。有an annotation:
@EnableMongoRepositories(basePackages = {"com.x.repository"})
当它添加到配置类时,它整理了我定义的存储库的接线。