当前位置: 首页 > 知识库问答 >
问题:

使用SpringBoot中的Testcontainers进行Spring数据弹性搜索集成测试

祁权
2023-03-14

我尝试编写一个联调的Spring Data Elastisearch存储库在SpringBoot使用测试容器和junit5

org . spring framework . beans . factory . unsatisfieddependencyexception:创建名为“com . example . demo . addressrepositorytest”的bean时出错:通过字段“repository”表示的未满足的依赖关系;嵌套异常为org . spring framework . beans . factory . nosuchbeandetification异常:没有类型为“com . example . demo . address repository”的合格bean可用:应至少有一个符合autowire候选资格的bean。依赖项批注:{ @ org . spring framework . beans . factory . annotation . auto wired(required = true)}

我如何解决这个问题?我尝试谷歌搜索,但找不到任何合适的东西。

数据传输对象

@Data
@Document(indexName = "addresses")
public class Address {
    String city;
    String street;
    GeoJsonPoint locathtml" target="_blank">ion;
}

存储库

@Repository
public interface AddressRepository extends ElasticsearchRepository<Address, String> {

}

测试地址存储库测试.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 ElasticsearchContainer(DockerImageName
            .parse("docker.elastic.co/elasticsearch/elasticsearch-oss")
            .withTag(ELASTICSEARCH_VERSION));

    @Autowired
    AddressRepository repository;

    @Autowired
    private Config esConfig;

    @BeforeEach
    void setUp() {
        container.start();
        System.setProperty("elasticsearch.host", container.getContainerIpAddress());
        System.setProperty("elasticsearch.port", String.valueOf(container.getMappedPort(9300)));
        assertTrue(container.isRunning());
    }

    @AfterEach
    void tearDown() {
        container.stop();
    }

    @Test
    void save() {
        final Address address = new Address();
        address.setCity("Some city");
        address.setStreet("Some street");

        address.setLocation(GeoJsonPoint.of(0, 0));
        final Address save = repository.save(address);

    }
}

共有1个答案

邵锐
2023-03-14

在Spring中,你必须以某种方式初始化一个上下文,否则没有什么可以自动连接的。这通常是通过< code>@Import注释来完成的,其中您设置了一个特定的配置,或者通过< code>@SpringBootTest注释来完成(在这种情况下,您不需要< code>@ExtendWith),如果这是一个测试的话。

请在谷歌搜索“春开机测试”或“春开机测试jpa”。也许类似于https://www.baeldung.com/spring-boot-testing.

因为默认情况下没有创建上下文,所以没有要自动关联的内容。< code > @ extend with(spring extension . class)不创建上下文。在生产应用程序中,@SpringBootApplication可以做到这一点,对于测试来说,有一个@SpringBootTest选项。

我也建议读一本书,Spring在行动,前两章可能就足够了。

 类似资料:
  • 我通过扩展ElasticsearchIntegrationTest为ElasticSearch编写了一个集成测试。测试顶部如下: 在尝试通过maven运行测试时,我从JUnit中看到了这个错误 JAVAlang.NoSuchMethodError:com。胡萝卜搜索。随机测试。随机化上下文。运行Privaterandomness(Lcom/carrotsearch/randomizedtestin

  • 升级到Spring boot 2.3和Spring data elasticsearch 4.0.9后的问题。我有这样的文档: 这在spring data 3.0中与Jackson配合得很好,但升级到4.0后,Jackson不再可用,现在我收到了一个来自spring的实例化异常,无法实例化URL对象。 例外情况: 任何关于解决方案的想法都将受到赞赏。

  • 我有一个Spring Boot应用程序,我想在其中单独使用弹性搜索2.2.0(而不是嵌入式服务器),我想使用Spring Data弹性搜索,那么Spring Data支持的弹性搜索版本是什么,我如何配置它以连接到运行在localhost:9200中的弹性搜索实例? 实际上,我尝试在application.properties文件中添加以下选项: 后来,我创建了这个配置类: 我得到了这个StackT

  • 我试着遵循这里列出的Nutch+ES指南 https://gist.github.com/xrstf/b48a970098a8e76943b9 https://qbox.io/blog/scring-the-web-wit-nutch-for-elasticsearch 然而,我无法让他们的组合工作。基本上,我在Nutch上执行了以下命令: 现在,我想将获取的数据索引到ES中,我按照指南进行了操作

  • 我正在用弹性搜索和PostgreSQL构建一个SpringBoot应用程序。我使用PostgreSQL进行写作,使用弹性搜索进行阅读。但我坚持了一些观点 我们是否需要为弹性搜索和PostgreSQL编写单独的模型(POJO)类?因为我们正在使用Elasticsearch chRepostory进行弹性搜索和JpaRepostory。 弹性搜索的注释也不同,PostgreSQL的注释在模型类上也不同