首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UnsatisfiedDependencyException in SpringMVC和MongoDB项目

UnsatisfiedDependencyException in SpringMVC和MongoDB项目
EN

Stack Overflow用户
提问于 2017-10-06 08:39:37
回答 1查看 292关注 0票数 0

我正在开发Spring和MongoDB支持的应用程序。

我的api运行良好,但我想用Fongo创建一个集成测试,以便测试保存文件以及通过我的服务获得该文件之后。

我已经尝试过了,但是当我在Service中添加@Autowired时,它返回了一个UnsatisfiedDependencyException。我的测试夹具类如下:

代码语言:javascript
复制
@ActiveProfiles({"test"})
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
    locations = {
            "classpath*:config/servlet-config.xml",
            "classpath*:config/SpringConfig.xml"})
@WebAppConfiguration
public class IntegrationTest {
@Autowired   // returns UnsatisfiedDependencyException
private FileService fileService;

@Before
@Test
public void setUp() throws IOException {
Fongo fongo = new Fongo("dbTest");
DB db = fongo.getDB("myDb");
}

@Test
public void testInsertAndGetFile() throws IOException{

    //create a file document in order to save
    File file = new File();
    file.setId("1");
    file.setFileName("test1");
    file.setVersionId("1");

    //save the file
    String id = fileService.storeFile(file);

    //get the file
    File fileDocument= fileService.getFileById("1");

    //check if id is the file id which is expected
    assertEquals(fileDocument.getId(), "1");
}
}

我收到了这条日志信息:

代码语言:javascript
复制
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.ots.openonefilesystem.integration.IntegrationTest': Unsatisfied dependency expressed through field 'fileService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [com.myproject.service.FileService]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [com.myproject.service.FileService]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

我正在使用xml配置。

如何解决测试错误?在我的服务类FileService中,我使用了@Service,但是Spring似乎期望至少有一个bean可以作为自动测试候选。

另外,如果我删除@Autowired,日志在保存文件时返回NullpointerException,正如所期望的那样。

PS:我使用springFramework 4.3.3.mongodb,spring MongoDB1.9.5

提前谢谢你!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-09 06:44:29

为了实现@Autowired FileService,我不得不配置模拟数据库(Fongo)。这个,我是通过@ContextConfiguration(classes = {MockDatasourceConfig.class})做的

因此,正确的测试夹具类如下:

代码语言:javascript
复制
@ActiveProfiles({"test"})
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {MockDatasourceConfig.class})
@WebAppConfiguration
public class IntegrationTest{

@Autowired
private FileService fileService;

@Test
public void testInsertAndGetFile() throws IOException{

    //create a file document in order to save
    File file = new File();
    file.setId("1");
    file.setFileName("test1");
    file.setVersionId("1");
    //save the file and return his id
    String id = fileService.storeFileGeneral(file);
    //get the file
    FileDocument fileDocument= fileService.getFileById(id);
    //check that the file isn't null
    assertNotNull(fileDocument);
    //check if id is the file id which is expected
    assertEquals(fileDocument.getId(), id);
    assertEquals(fileDocument.getFileName(), "test1");
}
}

MockDatasourceConfig类如下所示:

代码语言:javascript
复制
@Profile({"test"})
@Configuration
@PropertySource("classpath:mongo.properties")
@ComponentScan(basePackageClasses = {File.class})
@EnableMongoRepositories(basePackageClasses = RepositoryPackageMarker.class)
public class MockDatasourceConfig extends AbstractMongoConfiguration {

@Autowired
Environment env;

@Override
protected String getDatabaseName() {
    return env.getProperty("mongo.dbname", "myDb");
}

@Override
public Mongo mongo() throws Exception {
    return new Fongo(getDatabaseName()).getMongo();
}

@Bean
public GridFsTemplate gridFsTemplate() throws Exception {
    return new GridFsTemplate( mongoDbFactory(),mappingMongoConverter());
}
}

P.S:@M.Deinum的有益建议

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46601699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档