英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论