Ich habe eine Spring Boot-Anwendung mit Repository
(s).Warum kann ich meinen Dienst nicht direkt in meinem Test starten und warum muss ich das Repository @Autowired?
Ich verwende auch @Service
und erweitern Sie die repository
darin.
Wenn ich versuche, den Dienst @Autowired ich habe:
Verursacht durch: org.springframework.beans.factory.NoSuchBeanDefinitionException: Keine Warte Bohnen vom Typ 'com.api.core.service.CountryService' verfügbar: erwartet mindestens 1 Bean, die sich als Autowire-Kandidat qualifiziert. Dependency Anmerkungen: {@ org.springframework.beans.factory.annotation.Autowired (required = true)}
es funktioniert, wenn ich das Repository @Autowired
.
Mein Test wie folgt aussehen muss:
@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);
}
}
Dies ist, wie ich den Kontext zu konfigurieren:
@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.**";
}
annotate'CountryService' Klasse mit ' @ Komponente'? – flakes
@flakes ist es ein transaktionaler '@ Service' und' @ Service' wird auch von '@ ComponentScan' nach Frühlingsdokumentation eingefangen – BigDong