【问题标题】:Why can't I @Autowired my service directly in my test and why do I need to @Autowired the repository?为什么我不能在我的测试中直接@Autowired 我的服务,为什么我需要@Autowired 存储库?
【发布时间】:2018-05-30 06:34:36
【问题描述】:

我有一个带有 Repository(s) 的 Spring Boot 应用程序。

我也使用@Service 并在其中扩展repository

当我尝试@Autowired 我拥有的服务时:

原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“com.api.core.service.CountryService”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

如果我 @Autowired 存储库,它工作正常。

我的测试必须是这样的:

@SpringBootTest
@ActiveProfiles("core")
@ContextConfiguration(classes = { UserManagementConfig.class, UserManagementServiceConfig.class })
@UserManagementTx
@RunWith(SpringRunner.class)
public class BlogPostContentImplTest {
    @Autowired
    private CountryService countryService;
    @Autowired
    private BlogPostContentRepository blogPostContentRepository;
    private BlogPostContentServiceImpl blogPostContentService;
    private BlogPostContent entity = new BlogPostContent();
    @Before
    public void setUp() throws Exception {
        blogPostContentService = new BlogPostContentServiceImpl(blogPostContentRepository);
        List<Country> countryList = countryService.findAll(null);
        entity = new BlogPostContent();
        entity.setId(1L);
        entity.setDescription("test");
        entity.setCountry(countryList.get(0));
        blogPostContentService.insert(entity);
    }

    @After
    public void tearDown() throws Exception {
        blogPostContentService.delete(entity.getId());
    }

    @Test
    public void findAll() throws Exception {
        assertThat(blogPostContentService.findAll(null).size()).isGreaterThan(0);
    }

}

这是我配置上下文的方式:

@Configuration
@ComponentScan({
        UserManagementConfig.CONTEXT_CLASSPATH
})
public class UserManagementConfig {
    public static final String CONTEXT_CLASSPATH = "com.api.userManagement.config.context.**";
}

@Configuration
@ComponentScan({
        UserManagementServiceConfig.CLASSPATH,
})
public class UserManagementServiceConfig {

    public static final String CLASSPATH = "com.api.userManagement.service.**";

}

【问题讨论】:

  • annotateCountryService class with @Component?
  • @flakes 它是一个事务性的 @Service@Service 根据 spring 文档也被 @ComponentScan 捕获

标签: java spring hibernate spring-mvc


【解决方案1】:

根据您发布的代码,您可能需要在您的一个测试上下文类UserManagementConfigUserManagementServiceConfig 中对com.api.core.service.CountryService Bean 进行组件扫描

@ComponentScan({
     basePackages = {"com.api.core.service"}
})

要测试应用上下文是否加载您的 bean,您可以创建一个 TestContext 类,如下所示

@Configuration
@ComponentScan(basePackages = "com.api.core.service")
public class TestContext implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(TestContext.class, args);
    }
}

在您的测试中配置TestContext,如下所示

@ContextConfiguration(classes = {TestContext.class})

【讨论】:

  • 哦,这是因为我是从另一个我没有支付意图的包中导入的,但是如果它来自本地包,它会做同样的事情
  • 您是否在测试中加载应用程序上下文,您的 bean 需要初始化才能自动装配它们?
  • 我没有在任何地方做SpringApplication.run(),但没有它,其他一些@Service 测试工作正常
  • 你确定他们没有通过任何基类加载应用上下文吗?
  • 他们是同一种测试
猜你喜欢
  • 1970-01-01
  • 2014-06-21
  • 2017-09-25
  • 1970-01-01
  • 2022-01-04
相关资源
最近更新 更多