我有一个spring boot应用程序,我正在编写集成测试类。 当我运行我的测试类时,我得到以下异常
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.whot.dao.HotspotDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1466) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1097) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1059) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:589) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
我已经搜索了一段时间,并寻找可能导致这种情况的线索,但我无法想出这种情况发生的原因。帮助将不胜感激。我已经包含了我的代码的一些细节。
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>${spring.boot.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>${spring.boot.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${psql.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
</dependencies>
我在Application.Java类中有这个
package com.whot;
@SpringBootApplication
@ComponentScan(basePackages = {"com.whot.controller"})
@EntityScan(basePackages = {"com.whot.entity"})
@EnableJpaRepositories(basePackages = {"com.whot.repository", "com.whot.dao"})
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
并在我的集成类
中package com.whot.dao;
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class HotspotDaoIntegrationTest {
@Autowired
private HotspotDao hotspotDao;
@Autowired
private TestEntityManager entityManager;
private String hotspotName = "Mavericks";
@Test
public void givenHotspotNameReturnHotspotSuccessfully(){
Address address = new Address("Allen Mgba Crescent", 30L);
entityManager.persist(address);
Hotspot hotspot = new Hotspot(hotspotName);
entityManager.persist(hotspot);
entityManager.flush();
HotspotLocation hsLocation = new HotspotLocation(address);
hsLocation.setHotspotId(hotspot.getId());
entityManager.persist(hsLocation);
hotspot.getHotspotLocations().add(hsLocation);
HotspotDTO hotspotDto =((ArrayList<HotspotDTO>) hotspotDao.getHotspotByName(hotspotName)).get(0);
assertEquals(hotspotDto.getId(), hotspot.getId());
assertEquals(hotspotDto.getName(), hotspot.getName());
}
}
和这里讨论的课程
package com.whot.dao;
@Component
public class HotspotDao {
@Autowired
HotspotRespository hsRepository;
public Collection<HotspotDTO> getHotspotByName(String hotspotName) {
Collection <HotspotDTO> hotspots = getHotspotsByName(Arrays.asList(hotspotName));
return hotspots;
}
public Collection<HotspotDTO> getHotspotsByName(Collection <String> names) {
Collection<Hotspot> hotspots = hsRepository.findHotspotsByName(names);
Collection<HotspotDTO> result = new ArrayList<>();
for(Hotspot hotspot: hotspots){
result.add(new HotspotDTO().toHotspotDto(hotspot));
}
return result;
}
}
答案 0 :(得分:0)
Spring Boot默认扫描将启动具有启动应用程序类的相同包和子包。
如果其他注释不在同一个包中,则无法扫描,解决方法是在启动应用程序类上使用@ComponentScan注释,声明包扫描路径。像...
@ComponentScan(basePackages={"com.exp.package1","com.exp.package2"})