私は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.**";
}
TestContext
クラスを作成することができますテストする@コンポーネント '? – flakes
@flakesそれはトランザクションの '@Service'であり、@Serviceは' @ ComponentScan'でもキャッチされています。 – BigDong