Java Spring测试集成未运行我如何处理此错误?
我有一个测试类,这个错误让我抓狂: org.springframework.beans.factory.unsatifiedDependencyException: 创建名为的bean时出错。imprimirRelatorio:未满足 通过字段存储库表示的依赖关系;嵌套异常是 org.springframework.beans.factory.noSuchBean定义异常:否 v1.teste.Service类型的合格bean可用:至少应为 1个符合autowire候选资格的bean。依赖项批注: {@org.springframework.beans.factory.annotation.Autowiredrequired=true} 通过解析在web上搜索后,我无法运行测试。测试课是这样的:Java Spring测试集成未运行我如何处理此错误?,java,spring,spring-boot,spring-test,Java,Spring,Spring Boot,Spring Test,我有一个测试类,这个错误让我抓狂: org.springframework.beans.factory.unsatifiedDependencyException: 创建名为的bean时出错。imprimirRelatorio:未满足 通过字段存储库表示的依赖关系;嵌套异常是 org.springframework.beans.factory.noSuchBean定义异常:否 v1.teste.Service类型的合格bean可用:至少应为 1个符合autowire候选资格的bean。依赖项批注
@RunWith(SpringJUnit4ClassRunner.class)
public class imprimirRelatorio {
@Autowired
PautaService pautaRepository;
@Test
public void imprimirCabecalho(){
PautaReportBuilder pautaReportBuilder = new PautaReportBuilder();
//Reuniao reuniao = reuniaoService.findOne(UUID.fromString("4c7d3fc8-f78d-4ed3-a1ad-83c5da822ea7"));
try {
pautaReportBuilder.cabecalhoRelatorios(reuniao);
} catch (Exception e)
{
e.printStackTrace();
}
}
}
/*
使现代化
*/
所以我忘了说我已经在PautService接口中尝试了注释@service。在测试类上也是一样,我尝试使用@SpringBootTest注释。因此,错误消息是不同的
应用程序类
@SpringBootApplication
@ComponentScan({"br.com.empresa123.sistema123.*"})
public class App { public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
接口PautService:
@Service
public interface PautaService {
Page<Pauta> findAll(Pageable page);
Pauta findOne(UUID uuid);
Pauta save(Pauta pauta);
}
这是我测试的第一种方法,嵌套异常是:
org.springframework.beans.factory.unsatifiedDependencyException:
创建名为“subjectController”的bean时出错:未满足
通过字段“subjectService”表示的依赖关系;嵌套异常
是org.springframework.beans.factory.unsatifiedDependencyException:
创建名为“subjectService”的bean时出错:未满足依赖关系
通过字段“domainSvc”表示;嵌套异常是
org.springframework.beans.factory.unsatifiedDependencyException:
创建名为“dominio”的bean时出错:未满足依赖关系
通过字段“domainRepo”表示;嵌套异常是
org.springframework.beans.factory.unsatifiedDependencyException:
创建名为“domainReposImpl”的bean时出错:不满意
通过字段“sqlGenericPager”表示的依赖关系;嵌套异常
是org.springframework.beans.factory.NoSuchBean定义异常:否
类型为“java.lang.String”的限定bean可用:应位于
至少1个符合autowire候选资格的bean。附属国
注释:
{@org.springframework.beans.factory.annotation.Autowiredrequired=true,
@org.springframework.beans.factory.annotation.Qualifiervalue=sql通用分页器}
对于集成测试,在类上添加@SpringBootTest
如果您的文件与您的应用程序不在同一个包中,@component扫描您的应用程序包。我想您忘了用@service注释对服务进行注释。如果是这样,请用@Service标记服务类。当我通过代码中丢失的注释进行搜索时,我解决了这个问题,因为我从未使用过它:
public abstract class BaseCustomRepository {
// the class code
@Autowired
@Qualifier("sql-Generic-Pager")
protected String sqlGenericPager;
}
我从未使用过自定义存储库实现,因此我将重新考虑如何使用这个类。感谢许多耽误时间帮助我的程序员,并感谢堆栈溢出来分享我的担忧
我笑着问这个问题,所有的麻烦都是我造成的,就像你在一个类中执行git-burn命令,看到你的用户编写了错误的代码 您的PautService是否用@Service注释,它是否实现了v1.teste.Service?我相信在刚启动应用程序时,您也会遇到类似的错误。共享更多代码否,PautService没有注释,但PautServiceImpl有注释。我试图用@Service注释接口PautaService,但仍然不起作用。谢谢Gabbleg,但我尝试使用@SpringBootTest注释,发现了很多嵌套异常。另外,我的App.class有@ComponentScan注释如下:@SpringBootApplication@ComponentScan{br.com.empresa123.sistema123.*}公共类App{public static void mainString[]args{SpringApplication.runApp.class,args;}}我说的是在类测试中添加@ComponentScan。很抱歉给你带来了困惑。如果您的测试类位于文件夹br.com.empresa123.somethingthesis下,则@SpringBootTest没有找到您的所有bean是正常的,因为他试图在br.com.empresa123.somethingthesis下找到它们。这就是为什么需要在测试类上添加@ComponentScanbr.com.empresa123.sistema123。也许您可以添加在添加@SpringBootTest时出现的stacktrace。