5

I am experiencing a strange problem working on a Spring Boot 2.2.5.RELEASE project that uses JUnit.

I try to explain my problem in details:

1) I defined a service. First I defined an interface named OrderService, like this:

package com.dgs.soc.service;

import java.util.List;

import com.dgs.soc.excelapi.dto.Order;

public interface OrderService {

    public List<Order> getOrdersList();

}

Then I defined its implementation, at the moment something very simple named OrderServiceImpl:

package com.dgs.soc.service;

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import com.dgs.soc.excelapi.dto.Order;
import com.dgs.soc.repository.OrderRepository;

@Service
public class OrderServiceImpl {

    public List<Order> getOrdersList() {
        List<Order> result = new ArrayList<Order>();

        return result;
    }

}

As you can see this class is annoted by the @Service annotation.

The problem is using JUnit, I have this test class:

package com.dgs.soc.excelapi.integration;

// IMPORTS LIST

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { Application.class })
@WebAppConfiguration
@ActiveProfiles(profiles = { "no-liquibase" })
public class ExcelResourceIntegrationTest {

    @Autowired
    OrderServiceImpl orderService;

    @Test
    public void getOrdersListRepositoryTest() { 
        List<Order> ordersList = orderService.getOrdersList();
        assertThat(ordersList).isNotEmpty();
    }

}

And here I am experiencing a strange behavior: performing the getOrdersListRepositoryTest() test method I obtain this exception

2020-03-23 04:42:24.783 ERROR 5281 --- [           main] o.s.test.context.TestContextManager      : Caught exception while allowing TestExecutionListener [org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@7fbdb894] to prepare test instance [com.dgs.soc.excelapi.integration.ExcelResourceIntegrationTest@2ad6895a]

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.dgs.soc.excelapi.integration.ExcelResourceIntegrationTest': Unsatisfied dependency expressed through field 'orderService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.dgs.soc.service.OrderServiceImpl' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:393) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:119) ~[spring-test-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) ~[spring-test-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:43) ~[spring-boot-test-autoconfigure-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244) ~[spring-test-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) ~[spring-test-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) ~[spring-test-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) ~[junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) ~[spring-test-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246) ~[spring-test-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) ~[spring-test-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) ~[junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) ~[junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) ~[junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) ~[junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) ~[junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) ~[spring-test-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) ~[spring-test-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) ~[junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) ~[spring-test-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) ~[junit-4.12.jar:4.12]
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115) ~[junit-4.12.jar:4.12]
    at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:40) ~[junit-vintage-engine-5.5.2.jar:5.5.2]
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183) ~[na:na]
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195) ~[na:na]
    at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133) ~[na:na]
    at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
    at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150) ~[na:na]
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
    at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497) ~[na:na]
    at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:80) ~[junit-vintage-engine-5.5.2.jar:5.5.2]
    at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:71) ~[junit-vintage-engine-5.5.2.jar:5.5.2]
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229) ~[junit-platform-launcher-1.5.2.jar:1.5.2]
    at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197) ~[junit-platform-launcher-1.5.2.jar:1.5.2]
    at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211) ~[junit-platform-launcher-1.5.2.jar:1.5.2]
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191) ~[junit-platform-launcher-1.5.2.jar:1.5.2]
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:137) ~[junit-platform-launcher-1.5.2.jar:1.5.2]
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:89) ~[.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41) ~[.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542) ~[.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770) ~[.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464) ~[.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210) ~[.cp/:na]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.dgs.soc.service.OrderServiceImpl' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1695) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1253) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    ... 49 common frames omitted

2020-03-23 04:42:24.816  INFO 5281 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2020-03-23 04:42:24.819  INFO 5281 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-03-23 04:42:24.846  INFO 5281 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-03-23 04:42:24.875  INFO 5281 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

It says that No qualifying bean of type 'com.dgs.soc.service.OrderServiceImpl' available but, as you can see in the prevuous code this bean exist and it is annoted by the @Service annotation to allow autowiring !!!

Into this project there is defined a class annotated by @RestController that exposes API. If into this class I try to autowire the same service class, something like this:

@Description(value = "Resource layer for handling REST requests.")
@RestController
@RequestMapping("api")
public class ExcelResource {

    @Autowired
    OrderServiceImpl orderService;

    .......................................................
    .......................................................
    .......................................................
}

and now I try to perform the application as Spring Boot Application I obtain the same problem:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field orderService in com.dgs.soc.excelapi.resources.ExcelResource required a bean of type 'com.dgs.soc.service.OrderServiceImpl' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.dgs.soc.service.OrderServiceImpl' in your configuration.

But again the com.dgs.soc.service.OrderServiceImpl bean exist and it is annotated by @Service.

Why am I obtaining this issue? What am I missing? How can I try to fix this problem? I was thinking that I have some references problem or something like this but I have no idea

EDIT-1: Putting the @ComponentScan("com.dgs.soc.service") on the topo of my Application class it seems to work but I have some doubts !!!

So this is my Application class now:

package com.dgs.soc.excelapi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.dgs.soc.service")
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }
}

It works but int thwory it should works also without the ** @ComponentScan("com.dgs.soc.service")** annotation because this class was annoted by @SpringBootApplication that as you can read here:

https://docs.spring.io/spring-boot/docs/2.1.12.RELEASE/reference/html/using-boot-using-springbootapplication-annotation.html

automatically enable @ComponentScan: @Component scan on the package where the application is located (see the best practices)

So the application is located into com.dgs.soc.excelapi and in theory I expected that the component scan have to work popperly also withot the explicit defntion of @ComponentScan

Why? Some idea?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • In what package is your `Application` class? Generally spring only auto-scans the package where the `Application`class resides and any of its child package. Try adding explicit list of packages to scan on the `Application` class. – madteapot Mar 23 '20 at 12:02
  • May be you don't have the Spring loader scan that package where we have your serviceClass Implementation, may be you can try to use @ComponentScan("") – Vaibhav Kaushik Mar 23 '20 at 12:03
  • @Setu My Application class is into the "com.dgs.soc.excelapi" package – AndreaNobili Mar 23 '20 at 12:06
  • @AndreaNobili in that case you definitely need to explicitly scan the packages. You can use `ComponentScan` annotation on spring boot application class or provide `scanBasePackages` attribute on the `SpringBootApplication` annotation. – madteapot Mar 23 '20 at 12:14
  • @Setu in this way works, I added some new info at the end of my original post because in theory it should work also without the use of **ComponentScan** because I am using **SpringBootApplication** annotation. What am I missing? – AndreaNobili Mar 23 '20 at 12:16
  • Put your `Application` class in `com.dgs.soc` package. – Michał Krzywański Mar 23 '20 at 12:23

3 Answers3

12

The answer to your question is that you do not have proper package structure for spring-boot auto scan to pick up your service class.

SpringBootApplication annotation only enabled scanning of the package it resides in and its child packages.

If you have SpringBootApplication class in com.acme.app package then all the classes with spring boot annotation will get scanned in the package com.acme.app and any of its child packages i.e. com.acme.app.services, com.acme.app.controllers, etc. However if you have a package com.acme.services then any spring annotations are not auto-scanned from this package.

You have two options;

  • You either modify your package structure to allow spring-boot to auto scan all of the annotations. In your case move your Application class from com.dgs.soc.excelapi to com.dgs.soc. Or you could move all other packages i.e. service under com.dgs.soc.excelapi.

  • You explicitly list packages to scan using either @ComponentScan or scanBasePackages attribute on the SpringBootApplication annotation.

madteapot
  • 2,208
  • 2
  • 19
  • 32
9

Try putting @ComponentScan annotation on top of main application class

@ComponentScan("base.package.name")
Gaurav Dhiman
  • 953
  • 6
  • 11
  • In this way works but in theory it have to work also without the use of **ComponentScan** annotation because I am use **SpringBootApplication** annotation on my Application class. I added some additional info at the end of my original post. What am I missing? – AndreaNobili Mar 23 '20 at 12:17
  • 1
    If you want it work without @ComponentScan, then all other packages must be sub packages of com.dgs.soc.excelapi ex. com.dgs.soc.excelapi.service, com.dgs.soc.excelapi.dao or all classes could be in same package as of main application class – Gaurav Dhiman Mar 23 '20 at 12:20
3

The problem is the following:

Your Application class is in package "com.dgs.soc.excelapi"

@SpringBootApplication scans that package + all its subpackages for @Services/@Components/@Repositories.

You put all your services in completely different packages though ("com.dgs.soc.service"), so Spring Boot won't find them. And hence the @ComponentScan annotation works.

Fix: Reorder your package structure.

Marco Behler
  • 3,627
  • 2
  • 17
  • 19