Spring MVC 和 MongoDB 项目中的 UnsatisfiedDependencyException
UnsatisfiedDependencyException in SpringMVC and MongoDB project
我正在开发 Spring MVC 和 MongoDB 支持的应用程序。
我的 api 工作正常,但我想创建一个与 Fongo 的集成测试,以测试保存文件并在我的服务获取此文件后进行测试。
我已经尝试过了,但是当我在我的服务 class 中添加 @Autowired
时,它返回了一个 UnsatisfiedDependencyException
。我的测试夹具 class 如下:
@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");
}
}
我收到了这条日志消息:
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 配置。
如何解决我的测试错误?在我的服务 class FileService
中,我已经放置了 @Service
,但是似乎 Spring 预计至少有 1 个符合自动装配候选条件的 bean。
此外,如果我删除了@Autowired,日志 returns NullpointerException
在保存获取文件时,正如预期的那样。
PS: 我使用springFramework 4.3.3.RELEASE, spring-data-mongodb 1.9.5.RELEASE
提前致谢!
为了 @Autowired FileService
,我必须配置模拟数据库 (Fongo)。这个,我通过 @ContextConfiguration(classes = {MockDatasourceConfig.class})
做到了
因此,正确的测试夹具 class 如下:
@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
class 如下:
@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
的有用建议
我正在开发 Spring MVC 和 MongoDB 支持的应用程序。
我的 api 工作正常,但我想创建一个与 Fongo 的集成测试,以测试保存文件并在我的服务获取此文件后进行测试。
我已经尝试过了,但是当我在我的服务 class 中添加 @Autowired
时,它返回了一个 UnsatisfiedDependencyException
。我的测试夹具 class 如下:
@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");
}
}
我收到了这条日志消息:
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 配置。
如何解决我的测试错误?在我的服务 class FileService
中,我已经放置了 @Service
,但是似乎 Spring 预计至少有 1 个符合自动装配候选条件的 bean。
此外,如果我删除了@Autowired,日志 returns NullpointerException
在保存获取文件时,正如预期的那样。
PS: 我使用springFramework 4.3.3.RELEASE, spring-data-mongodb 1.9.5.RELEASE
提前致谢!
为了 @Autowired FileService
,我必须配置模拟数据库 (Fongo)。这个,我通过 @ContextConfiguration(classes = {MockDatasourceConfig.class})
因此,正确的测试夹具 class 如下:
@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
class 如下:
@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
的有用建议