I have a multimodule springboot project. Here is the architecture:
The class annotated with @SpringBootApplication
is in the top module (webservice). When I run integration test from this top module using @SpringBootTest
in my test classes, it works fine.
But now I'd like to run integration test from the business module. @SpringBootTest
only doesn't work anymore because no config class can be found in the business module. So I created a config class in the business module:
package com.berthoud.p7.webserviceapp.business.config;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.ComponentScan;
@SpringBootConfiguration
@ComponentScan(basePackages = "com.berthoud.p7")
public class TestContextConfiguration {
}
And in my test classes, I pointed to this config class, like this :
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestContextConfiguration.class)
public class BookResearchIT {
//my tests...
}
Doing so, I was hopping that Spring would add to the context all the beans declared in the package com.berthoud.p7 and its subfolders. Indeed, when I autowire spring beans in my test classes it now looks fine (IntelliJ doesnt tell anymore that the @autowired
beans cannot be autowired):
But nevertheless, when I run my tests, Spring fails to load the application context :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field bookReferenceDAO in com.berthoud.p7.webserviceapp.business.BookResearchManager required a bean of type 'com.berthoud.p7.webserviceapp.consumer.contract.BookReferenceDAO' 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.berthoud.p7.webserviceapp.consumer.contract.BookReferenceDAO' in your configuration.
I don't understand this. Here is how I declared the beans concerned :
package com.berthoud.p7.webserviceapp.business;
@Service
public class BookResearchManager {
@Autowired
BookReferenceDAO bookReferenceDAO;
@Autowired
LibrairyDAO librairyDAO;
@Autowired
BookDAO bookDAO;
package com.berthoud.p7.webserviceapp.consumer.contract;
public interface BookReferenceDAO {
// method signatures
}
package com.berthoud.p7.webserviceapp.consumer.repositories.SpringDataJPA;
public interface BookReferenceRepository extends CrudRepository<BookReference, Integer>, BookReferenceDAO {
What did I do wrong?
EDIT: after changing my config class like this :
package com.berthoud.p7.webserviceapp.business.config;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootConfiguration
@ComponentScan(basePackages = "com.berthoud.p7.webserviceapp")
@EnableJpaRepositories(basePackages = "com.berthoud.p7.webserviceapp.consumer")
@EntityScan(basePackages = "com.berthoud.p7.webserviceapp")
public class TestContextConfiguration {
}
I now have a different error :
Field bookReferenceDAO in com.berthoud.p7.webserviceapp.business.BookResearchManager required a bean named 'entityManagerFactory' that could not be found.