![](/img/trans.png)
[英]Spring boot can not autowire repository bean for service test class
[英]Spring boot test unable to autowire service class
我正在嘗試創建一個 Spring Boot 測試類,它應該創建 Spring 上下文並自動裝配服務類供我測試。
這是我得到的錯誤:
引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有可用的“com.gobsmack.gobs.base.service.FileImportService”類型的合格bean:預計至少有1個符合自動裝配候選資格的bean。 依賴注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
文件結構:
測試類:
package com.example.gobs.base.service;
import com.example.gobs.base.entity.FileImportEntity;
import com.example.gobs.base.enums.FileImportType;
import lombok.val;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@DataJpaTest
@RunWith(SpringRunner.class)
public class FileImportServiceTest {
@Autowired
private FileImportService fileImportService;
private FileImportEntity entity;
Main
應用類:
package com.example.gobs.base;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Used only for testing.
*/
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
FileImportService
接口:
package com.example.gobs.base.service;
import com.example.gobs.base.entity.FileImportEntity;
import com.example.gobs.base.enums.FileImportType;
import java.util.List;
public interface FileImportService {
/**
* List all {@link FileImportEntity}s.
這是通過以下方式實現的:
package com.example.gobs.base.service.impl;
import com.example.gobs.base.entity.FileImportEntity;
import com.example.gobs.base.enums.FileImportType;
import com.example.gobs.base.repository.FileImportRepository;
import com.example.gobs.base.service.FileImportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class FileImportServiceImpl implements FileImportService {
@Autowired
private FileImportRepository repository;
@Override
public List<FileImportEntity> listAllFileImportsByType(FileImportType type) {
return repository.findAllByType(type.name());
}
為什么找不到實現?
@DataJpaTest
注釋不會使服務加載到應用程序上下文。 來自 Spring 文檔: https : //docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test
您可以使用 @DataJpaTest 批注來測試 JPA 應用程序。 默認情況下,它會掃描 @Entity 類並配置 Spring Data JPA 存儲庫。 如果在類路徑上有一個嵌入式數據庫可用,它也會配置一個。 常規 @Component bean 不會加載到 ApplicationContext 中。
您可以使用@SpringBootTest
批注而不是DataJpaTest
。 希望有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.