0

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!

don.lia
  • 23
  • 9
  • So you are creating a new instance of `Fongo` inside your test class and expect Spring to use that for auto wiring... That is obviously not going to happen, you have to configure this in your spring context so Spring can auto wire it. – M. Deinum Oct 06 '17 at 09:39
  • Thank you for the immediate response. I can understand this, and so i have tried another solution,which is in this [link](https://stackoverflow.com/questions/46585790/spring-mvc-with-mongodb-integration-test-for-saving-and-getting-file-with-fong) but when i don't add the @Autowired, i am receining `NullPointerException`, and when i use it, i receive `UnsatisfiedDependencyException` – don.lia Oct 06 '17 at 10:00
  • Ofcourse it will lead to a `NullPointer`... You will need to configure `Fongo` and use that as the driver (or db) in your mongo setup. As stated you need a test configuration. – M. Deinum Oct 06 '17 at 10:03
  • With`@ContextConfiguration(classes = {MockDatasourceConfig.class})` doesn't make it up? – don.lia Oct 06 '17 at 10:11
  • It will be up but as long as you don't let it override your default mongo instance it won't work, and removing `@Autowired` will break things. – M. Deinum Oct 06 '17 at 10:13
  • so using `@Autowired` and `MockDatasourceConfig.class` as in my solution of [link](https://stackoverflow.com/questions/46585790/spring-mvc-with-mongodb-integration-test-for-saving-and-getting-file-with-fong) how can override my default mongo? How can i resolve it?Sorry for many questions,but i am a newbie in SpringFramework. – don.lia Oct 06 '17 at 10:46
  • By giving it the same id/name as your regular `mongo` bean... – M. Deinum Oct 06 '17 at 10:49
  • Where have to do this?In my xml file where are the Mongo's and Fongo's beans? There, the duplicate ids are not valid.In `MockDatasourceConfig.class` and `SpringMongoConfig.class` take the same names.Can you check my `SpringConfig.xml` file? – don.lia Oct 06 '17 at 11:27
  • @M.Deinum thank you a lot for your help, i follow your advice and i configure Fongo in my Test Class and it works fine! – don.lia Oct 06 '17 at 11:44

1 Answers1

0

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

don.lia
  • 23
  • 9