我正在尝试使用 Testcontainers 和 junit5 在 SpringBoot 中为 Spring Data Elastisearch 存储库编写集成测试。但测试失败了
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“com.example.demo.AddressRepositoryTest”的bean时出错:通过字段“repository”表达的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“com.example.demo.AddressRepository”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
我该如何解决这个问题?我尝试谷歌搜索但找不到任何合适的东西。
数据传输组织
@Data
@Document(indexName = "addresses")
public class Address {
String city;
String street;
GeoJsonPoint location;
}
Run Code Online (Sandbox Code Playgroud)
存储库
@Repository
public interface AddressRepository extends ElasticsearchRepository<Address, String> {
}
Run Code Online (Sandbox Code Playgroud)
测试AddressRepositoryTest.java
@ExtendWith(SpringExtension.class)
@Testcontainers
class AddressRepositoryTest {
private static final String ELASTICSEARCH_VERSION = "7.10.1";
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(final ConfigurableApplicationContext configurableApplicationContext) {
}
}
@Container
public static ElasticsearchContainer container = new …Run Code Online (Sandbox Code Playgroud)