2017-12-16 17 views
0

나는 스프링 부트 응용 프로그램이 Repository (s)입니다.왜 내 서비스에서 @Autowired 서비스를 직접 사용할 수 없으며 왜 @Autowired 리포 지 토리가 필요합니까?

@Service도 사용하고 repository을 확장하십시오. 나는 서비스를 @Autowired하려고하면

나는이 없다 : org.springframework.beans.factory.NoSuchBeanDefinitionException :

에 의한 유형 'com.api.core.service.CountryService'의 어떠한 자격 콩을 available : 적어도 하나의 bean이 autowire 후보가 될 것으로 예상된다. 종속성 주석 : 나는 저장소를 @Autowired 경우 잘 작동

{org.springframework.beans.factory.annotation.Autowired @는 (= TRUE 필수)}.

내 시험은 다음과 같이해야합니다

@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.**"; 

} 
+0

annotate'CountryService' 클래스 다음과 같이 테스트에 구성되어 아래처럼 TestContext 클래스를 만들 수 있습니다, 테스트하려면' @ 컴포넌트 '? – flakes

+0

@flakes 트랜잭션 문서 인'@Service'와'@Service'도 스프링 문서에 따라'@ ComponentScan'에 잡혀 있습니다. – BigDong

답변

1

을, 당신이 com.api.core.service.CountryService를 스캔 구성 요소에해야 할 것 테스트 컨텍스트 클래스 중 하나에있는 콩 UserManagementConfig 또는 UserManagementServiceConfig

@ComponentScan({ 
    basePackages = {"com.api.core.service"} 
}) 
응용 프로그램 컨텍스트가 당신의 bean을로드하는 경우


당신이
@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}) 
+0

오, 이것은 다른 패키지에서 가져 오기 때문에 의도 한 것이지만 실제로는 그렇지 않습니다. 동일한 패키지가 로컬 패키지 인 경우 – BigDong

+0

테스트 환경에서 응용 프로그램 컨텍스트를로드하고 있습니까? bean을 autowire하기 위해 초기화해야합니까? –

+0

어디서나 SpringApplication.run()을하지는 않지만, 다른 '@Service' 테스트는 아무런 문제없이 작동합니다 – BigDong