why is spring boot's DataJpaTest scanning @Component -
confident hasn't been asked reading through spring docs , testing utilities found annotation , thought i'd start using it. reading through fine print read:
regular @component beans not loaded applicationcontext.
sounded , liked idea of using h2 except found entity wanted use had catalog , schema modifiers , default h2 couldn't figure out how support that. made h2 datasource test branch , use , override replace. wound
@runwith(springrunner.class) @contextconfiguration(classes=abch2congfiguration.class) @datajpatest @autoconfiguretestdatabase(replace= autoconfiguretestdatabase.replace.none) public class statusrepositorytest { }
however tests fails fro caused by: org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type. leads to: error creating bean name 'customerserviceimpl': unsatisfied dependency.
however customerserviceimpl bean:
@component public class customerserviceimpl implements customerservice { }
that says @component. fine print datajpatest says doesn't load @components. why doing , failing test?
as kyle , eugene asked below here's rest:
package com.xxx.abc.triage; @component public interface customerservice { } configuration @componentscan("com.xxx.abc") @enablejparepositories("com.xxx.abc") //@profile("h2") public class abmh2congfiguration { @primary @bean(name = "h2source") public datasource datasource() { embeddeddatabase build = new embeddeddatabasebuilder().settype(embeddeddatabasetype.h2).setname("abc").addscript("init.sql").build(); return build; } @bean public jpavendoradapter jpavendoradapter() { hibernatejpavendoradapter bean = new hibernatejpavendoradapter(); bean.setdatabase(database.h2); bean.setshowsql(true); bean.setgenerateddl(true); return bean; } @bean public localcontainerentitymanagerfactorybean entitymanagerfactory( datasource datasource, jpavendoradapter jpavendoradapter) { localcontainerentitymanagerfactorybean bean = new localcontainerentitymanagerfactorybean(); bean.setdatasource(datasource); bean.setjpavendoradapter(jpavendoradapter); bean.setpackagestoscan("com.xxx.abc"); return bean; } @bean public jpatransactionmanager transactionmanager(entitymanagerfactory emf) { return new jpatransactionmanager(emf); } }
and clarify question, why @component being loaded context within @datajpatest?
@componentscan
automatically inject found @component
, @service
context. override separate @bean
:
@bean customerservice customerservice{ return null; }
or remove @component
annotation customerservice
, customerserviceimpl
, should add @bean
@ production @configuration
Comments
Post a Comment