mongodbspring-mvcexceptionintegration-testingfongo

UnsatisfiedDependencyException in SpringMVC and MongoDB project


I'm working on a Spring MVC and MongoDB backed application.

My api is working fine,but i would like to create an integration test with Fongo in order to test saving a file and after getting this file by my services.

I have already tried, but when i added @Autowired in my Service class, it returned an UnsatisfiedDependencyException. My test fixture class is the following:

@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");
}
}

and i am receiving this log message:

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)}

I'm using xml configuration.

How can i solve my testing error? In my service class FileService i have put @Service, however it seems that Spring expected at least 1 bean which qualifies as autowire candidate.

Also if i remove the @Autowired, the log returns NullpointerException when saving of getting the file,as it was expected.

PS: I use springFramework 4.3.3.RELEASE, spring-data-mongodb 1.9.5.RELEASE

Thank you in advance!


Solution

  • I had to configure Mock Database (Fongo), in order to @Autowired FileService. This, I made it via @ContextConfiguration(classes = {MockDatasourceConfig.class})

    So, the correct test fixture class is the following:

    @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");
    }
    }
    

    and the MockDatasourceConfig class is the following:

    @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: With helpful advice of @M.Deinum