带有注释的类@SpringBootApplication
位于顶部模块(webservice)中。@SpringBootTest
当我在我的测试类中使用这个顶级模块运行集成测试时,它工作正常。
但现在我想从业务模块运行集成测试。@SpringBootTest
only 不再起作用,因为在业务模块中找不到配置类。所以我在业务模块中创建了一个配置类:
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 {
}
在我的测试类中,我指向了这个配置类,如下所示:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestContextConfiguration.class)
public class BookResearchIT {
//my tests...
}
这样做,我希望 Spring 会将包 com.berthoud.p7 及其子文件夹中声明的所有 bean 添加到上下文中。事实上,当我在我的测试类中自动装配 spring bean 时,它现在看起来很好(IntelliJ 不再告诉@autowired
bean 不能自动装配):
但是,当我运行测试时,Spring 无法加载应用程序上下文:
***************************
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.
我不明白这一点。以下是我声明相关 bean 的方式:
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 {
我做错了什么?
编辑:像这样更改我的配置类后:
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 {
}
我现在有一个不同的错误:
Field bookReferenceDAO in com.berthoud.p7.webserviceapp.business.BookResearchManager required a bean named 'entityManagerFactory' that could not be found.