我的理解是SpringBootApplication注释包括 ComponentScan
已发现bean并将其打印在Application.main()中,为什么单元测试找不到它?
该单元测试失败,并显示:
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为'com.pds.pdssr.etlfile.EtlFileServicesTest'的bean时出错:通过字段'etlFileServices'表示的不满意的依赖关系;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为com.pds.pdssr.etlfile.EtlFileServices的合格Bean:应该至少有1个有资格作为自动装配候选的Bean。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
通过适当级别的调试,即“ org.springframework.context.annotation” =“ debug”,我可以看到在组件扫描期间发现了该bean。
尽管如此,单元测试的结果是:
[错误] getAll(com.pds.pdssr.etlfile.EtlFileServicesTest)经过的时间:0.007 s <<<错误! org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为'com.pds.pdssr.etlfile.EtlFileServicesTest'的bean时出错:通过字段'etlFileServices'表示的不满意依赖关系;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为com.pds.pdssr.etlfile.EtlFileServices的合格Bean:应该至少有1个有资格作为自动装配候选的Bean。依赖项注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)} 由以下原因导致:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为“ com.pds.pdssr.etlfile.EtlFileServices”的合格Bean:预计至少有1个有资格作为自动装配候选的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);
}
}
}
组件:
包com.pds.pdssr.bootstrap;
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);
}
}
答案 0 :(得分:1)
原始答案:
您需要
echo '<td><img src="' . base_url('uploads/' . $row->filename) . '" alt="" width="100%" height="100%"></td>';
在您的测试课程中。
基于评论的其他答案
如果您确实想在项目中使用nota_35="Acoustic Bass Drum"
nota_36="Bass Drum 1"
nota_37="Side Stick"
nota_38="Acoustic Snare"
nota_39="Hand Clap"
Notas=((nota_35,'Acoustic Bass Drum'),(nota_36,'Bass Drum 1'),(nota_37,'Side Stick'),(nota_38,'Acoustic Snare'),(nota_39,'Hand Clap'))
notas=models.CharField(max_length=20, choices=Notas)
,则需要告诉spring框架明确拥有SessionContext。可以通过添加
@RunWith(SpringRunner.class)
@SpringBootTest(classes = YourMainClass.class)
到您的配置文件。 Here's a working example for your problem。
HTH