0
votes

I have a test class that make me crazy with this error :

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name .imprimirRelatorio: Unsatisfied dependency expressed through field repository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type v1.teste.Service available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

After search in the web by a resolution, I couldn't run the test. The test class was like that:

@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();
        }
    }




}

/* Update */

So I forgot to say that I already tried the annotation @service in the pautaService interface. And the same on the test class I've tried to use the @SpringBootTest annotation. So the error messages was different.

The App.class

 @SpringBootApplication 
@ComponentScan({"br.com.empresa123.sistema123.*"}) 
public class App { public static void main(String[] args) { 
SpringApplication.run(App.class, args);
 }
 } 

The interface PautaService:

@Service
public interface PautaService {

    Page<Pauta> findAll(Pageable page);

    Pauta findOne(UUID uuid);

    Pauta save(Pauta pauta);
} 

This is the first approach of my test , the nested exception were :

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'subjectController': Unsatisfied dependency expressed through field 'subjectService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'subjectService': Unsatisfied dependency expressed through field 'domainSvc'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dominio': Unsatisfied dependency expressed through field 'domainRepo'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'domainReposImpl': Unsatisfied dependency expressed through field 'sqlGenericPager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=sql-Generic-Pager)}

3
Is your PautaService annotated with @Service and does it implement the v1.teste.Service? I believe you'll get similar error when just starting an application. Share more code - itwasntme
No, the PautaService hasn't the annotation but the PautaServiceImpl has. I tried to annotate the interface PautaService with @Service but still not working. - alexandre monassa Moreira

3 Answers

4
votes

For integration test, add @SpringBootTest over your class.

In case where your file is not in the same packaging of your app, @ComponentScan() your app packaging.

3
votes

I think you have forgot to annotate the service with the @Service annotation. If so, please tag the Service CLASS with @Service.

0
votes

I solve this problem when I search by a annotation lost in my code that I never used it :

public abstract class BaseCustomRepository {

    // the class code 

    @Autowired
    @Qualifier("sql-Generic-Pager")
    protected String sqlGenericPager;

} 

I never used a Custom repository implementation so I will rethink what I will do with this class. Thanks a lot of the coders that lost their time to help me and the stack over flow to share my concerns.

I was laughing about this question, all the trouble was caused by me, like you do a git blame command in a class and see your user that made the wrong code.