0

私は Spring MVC と MongoDB に裏打ちされたアプリケーションに取り組んでいます。

私のAPIは正常に動作していますが、ファイルの保存をテストするためにFongoとの統合テストを作成し、サービスでこのファイルを取得したいと考えています.

私はすでに試しまし@Autowiredたが、 Service クラスに追加すると、UnsatisfiedDependencyException. 私のテストフィクスチャクラスは次のとおりです。

@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構成を使用しています。

テスト エラーを解決するにはどうすればよいですか? 私のサービスクラスFileServiceには を入れまし@Serviceたが、Spring はオートワイヤー候補として適格な Bean を少なくとも 1 つ期待しているようです。

また、@Autowired を削除すると、NullpointerException予想どおり、ファイルを取得して保存するときにログが返されます。

PS: springFramework 4.3.3.RELEASE、spring-data-mongodb 1.9.5.RELEASE を使用します。

前もって感謝します!

4

1 に答える 1