3

I have spring boot application sample with package layout:

main:
-com.foo
     Application.java       
-com.foo.services
      ItemService.java
      ItemRepository.java    
-com.foo.config
     Configuration.java
test:
-com.foo.services 
     ItemServiceIngegrationTest.java

My integration test fails to run being not able to find ItemRepository bean if put

  @ComponentScan(basePackageClasses = { ItemService.class })

but works if I put

  @ComponentScan(basePackageClasses = { Application.class })

where is the trick ?

spec says :

Either basePackageClasses() or basePackages() (or its alias value()) may be specified to define specific packages to scan. If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.

@EnableAutoConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { Configuration.class })
public class ItemServiceIntegrationTest { 
    . . .
}

@org.springframework.context.annotation.Configuration
@PropertySource("classpath:application.properties")
@ComponentScan(basePackageClasses = { ItemService.class })
public class Configuration extends AbstractMongoConfiguration {
   .  .  .
}   
alexbt
  • 16,415
  • 6
  • 78
  • 87
magulla
  • 499
  • 1
  • 9
  • 22

1 Answers1

2

The javadoc says:

If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.

So, it scans from the package that declares ComponentScan unless a specific package class is defined. Can you put it in the Application class?

K.Nicholas
  • 10,956
  • 4
  • 46
  • 66
  • It works if you define it as Application, which resides in a lower level package, but not if you define it in a higher level package. The package you define it as is the `parent` package and it scans all the `children` packages. – K.Nicholas Feb 21 '16 at 12:59
  • yes, i know it works with Application - that what is stated in my description:). My point that according to spec it should work even with sub-packages. – magulla Feb 22 '16 at 21:38
  • 1
    Yea, I agree. According to the documentation it seems that both your annotations should find the ItemRepository bean. – K.Nicholas Feb 22 '16 at 21:55