2017-12-16 16 views
0

私はRepository(s)のスプリングブートアプリケーションを持っています。なぜ私は自分のサービスを@Autowiredにできないのですか?なぜ私は@Autowiredリポジトリにする必要がありますか?

また、@Serviceを使用し、repositoryを拡張します。

私は私が持っているサービス@Autowiredしよう:org.springframework.beans.factory.NoSuchBeanDefinitionException:によって引き起こさ

タイプのない予選豆 'をcom.api.core.service.CountryService'利用可能:少なくとも1つのbeanがautowire候補になります。依存関係のアノテーション:{@ org.springframework.beans.factory.annotation.Autowired(必要=真)}

私はリポジトリを@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.**"; 

} 
+0

@ContextConfiguration(classes = {TestContext.class}) 

以下annotate'CountryService'クラスのようなあなたのテストで設定したTestContextクラスを作成することができますテストする

@コンポーネント '? – flakes

+0

@flakesそれはトランザクションの '@Service'であり、@Serviceは' @ ComponentScan'でもキャッチされています。 – BigDong

答えて

1

、あなたがcom.api.core.service.CountryServiceをスキャンコンポーネントが必要になることがありますようですテストコンテキストクラスのBean UserManagementConfigまたはUserManagementServiceConfig

@ComponentScan({ 
    basePackages = {"com.api.core.service"} 
}) 
アプリコンテキストがあなたの豆をロードした場合、あなたは

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

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

以下のようにTestContextは `と

+0

これは私が別のパッケージからインポートしようとしているからです。ローカルパッケージの場合は同じもの – BigDong

+0

テストでアプリケーションコンテキストをロードしていますか?あなたのBeanをオートワイヤリングするために初期化する必要がありますか? –

+0

どこでも 'SpringApplication.run()'は行っていませんが、他の '@Service'テストではうまく動作していません。 – BigDong

関連する問題