我的理解是SpringBootApplication注释包括ComponentScan
https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-using-springbootapplication-annotation.html
bean是在application.main()中发现并打印的,为什么单元测试没有找到它?
此单元测试失败的原因是:
org.springframework.beans.factory.unsatisfieddependencyException:创建名为“com.pds.pdssr.etlfile.etlfileServiceStest”的bean时出错:通过字段“etl fileServices”表示的不满足的依赖项;嵌套异常为org.springframework.beans.factory.noSuchBeanDefinitionException:没有“com.pds.pdssr.etlfile.etlfileServices”类型的合格bean可用:应至少有一个合格的自动候选bean。依赖项注释:{@org.SpringFramework.Beans.Factory.Annotation.AutoWired(required=true)}
[错误]getAll(com.pds.pdssr.etlfile.etlfileServiceStest)已用时间:0.007s<<<错误!org.springframework.beans.factory.unsatisfieddependencyException:创建名为“com.pds.pdssr.etlfile.etlfileServiceStest”的bean时出错:通过字段“etl fileServices”表示的不满足的依赖项;嵌套异常为org.springframework.beans.factory.noSuchBeanDefinitionException:没有“com.pds.pdssr.etlfile.etlfileServices”类型的合格bean可用:应至少有一个合格的自动候选bean。依赖项批注:{@org.springframework.beans.factory.annotation.AutoWired(required=true)}导致得原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“com.pdssr.etlfile.etlfileServices”类型得合格bean可用:应至少有一个合格得自动连接候选bean.依赖项注释:{@org.SpringFramework.Beans.Factory.Annotation.AutoWired(required=true)}
应用程序:
package com.pds.pdssr.bootstrap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
//@EnableJpaRepositories("com.pds.pdsssr.jpa")
@SpringBootApplication
// @EntityScan("com.pds.pdssr.models")
public class Application {
private static final Logger logger = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(Application.class, args);
for (String name : applicationContext.getBeanDefinitionNames()) {
logger.info("bean: " + name);
}
}
}
组件:
import java.util.List;
import javax.persistence.EntityManagerFactory;
import javax.transaction.Transactional;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.pds.pdssr.models.EtlFile;
@Repository
public class EtlFileServices {
@Autowired
private static EntityManagerFactory entityManagerFactory;
public SessionFactory getSessionFactory() {
SessionFactory sessionFactory = null;
if (entityManagerFactory == null) {
throw new IllegalStateException("entityManagerFactory is null");
}
sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
if (sessionFactory == null) {
throw new NullPointerException("factory is not a hibernate factory");
}
return sessionFactory;
}
@SuppressWarnings("unchecked")
public List<EtlFile> getAll() {
return getAll("etlFile",getSessionFactory().getCurrentSession());
}
@SuppressWarnings("rawtypes")
protected List getAll(String tableName, Session session) {
String queryText = "from " + tableName;
return getList(tableName, queryText, session);
}
@SuppressWarnings("rawtypes")
protected List getList(String tableName, String queryText, Session session) {
long start = System.nanoTime();
Query query = session.createQuery(queryText);
List result = query.list();
long end = System.nanoTime();
long millis = (end - start) / 1000000;
//logger.debug("table: " + tableName + " millis " + millis + " rows: " + result.size());
return result;
}
}
测试类:
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringRunner;
import com.pds.pdssr.bootstrap.EtlFileServices;
import com.pds.pdssr.models.EtlFile;
@RunWith(SpringRunner.class)
public class EtlFileServicesTest {
@Autowired
private EtlFileServices etlFileServices;
@Test
public void getAll() {
List<EtlFile> etlFiles = etlFileServices.getAll();
assertNotNull(etlFiles);
}
}
原答案:
你需要
@RunWith(SpringRunner.class)
@SpringBootTest(classes = YourMainClass.class)
在您的测试类中。
spring.jpa.properties.hibernate.current_session_context_class = org.springframework.orm.hibernate5.SpringSessionContext
错误:-->org.springframework.beans.factory.unsatisfieddependencyexception:创建名为“home controller”的bean时出错:通过字段“user tokenservice”表示的不满足的依赖项;嵌套异常为org.springframework.beans.factory.unsatisfieddependencyexcep
无法解决测试Spring Boot应用程序的问题: 主控制器: 应用程序属性:
大家好,每个人都是spring的新手,正在尝试为CRUD创建一个简单的应用程序,但是我得到了下面的错误。我正在使用STS 创建名为“Client ServiceImpli”的bean时出错:通过字段“Client Repository”表示的不满足的依赖关系;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“clie
org.springframework.beans.factory.unsatisfiedDependencyException:创建名为“com.sonos.arcor.service.AddressServiceTest”的bean时出错:通过字段“service”表示的未满足的依赖项;嵌套异常为org.springframework.beans.factory.noSuchBeanDefin
以下是例外情况: 我的代码: 我评论了setCacheable(true),因为它给出了一个错误。有人能帮忙吗?
正在尝试测试我的。。。编译器请求注入,但在为创建bean后,它返回通过字段 代码: 错误: