无法在 spring-data-elasticsearch 的测试中注入仓库。

huangapple go评论74阅读模式
英文:

Unable to inject repository in test for spring-data-elasticsearch

问题

无法自动装配FooRepository,它始终设置为null

Config.java类

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.foo.repository")
public class Config extends AbstractElasticsearchConfiguration {

测试类

@SpringIntegrationTest
@ContextConfiguration(classes = { FooRepositoryTest.ConfigTest.class })
public class FooRepositoryTest {

    @Configuration
    @Import({ Config.class })
    @EnableElasticsearchRepositories(basePackages = "com.company.repository")
    static class ConfigTest {}

    @Autowired 
    FooRepository repository;
    ...
}
英文:

Unable to Autowired FooRepository, its always set to null

Config.java class

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.foo.repository")
public class Config extends AbstractElasticsearchConfiguration {

Test class

@SpringIntegrationTest
@ContextConfiguration(classes = { FooRepositoryTest.ConfigTest.class })
public class FooRepositoryTest {

    @Configuration
    @Import({ Config.class })
    @EnableElasticsearchRepositories(basePackages = "com.company.repository")
    static class ConfigTest {}

    @Autowired FooRepository repository;
..
}

答案1

得分: 0

看一下注释文档,这是使用方法:

@RunWith(SpringRunner.class)
@SpringIntegrationTest
public class MyIntegrationTests {

  @Autowired
  private MockIntegrationContext mockIntegrationContext;

}

在你的情况下,缺少了注释 @RunWith(SpringRunner.class)

英文:

Looking at the annotation documentation, this is how it is used:

@RunWith(SpringRunner.class)
@SpringIntegrationTest
public class MyIntegrationTests {

  @Autowired
  private MockIntegrationContext mockIntegrationContext;

}

In your case the annotation @RunWith(SpringRunner.class) is missing

huangapple
  • 本文由 发表于 2020年10月20日 22:00:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64446777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定