【发布时间】:2017-05-28 06:15:58
【问题描述】:
我正在尝试对 DAO 进行以下单元测试。
我无法识别数据源。
我可以得到有关如何解决此问题的提示吗?
详情如下
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class EntityDaoTest {
@Autowired
protected EntityDao entityDao;
@Before
public void setup()
{
}
@Test
public void test() throws InternalServerException
{
List<Entity> entities = entityDao.list();
assert(entities.size()==0);
}
}
DAO类的相关方面如下
@Repository
public class EntityDao extends GenericDao<Entity>{
public EntityDao(DataSource dataSource) {/.../}
}
我的 src/test/resources/application.properties 文件如下
# Database
spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.username=dbuser
spring.datasource.password=dbpass
在 Eclipse 中作为 JUnit 测试运行的跟踪
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityController': Unsatisfied dependency expressed through field 'entityDao': Error creating bean with name 'entityDao' defined in file .../target/classes/hitstpa/dao/EntityDao.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {};
...
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityDao' defined in file [/home/fmason/workspace/hitstpa/target/classes/hitstpa/dao/EntityDao.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency [javax.sql.DataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. ...`
应用结构
-src
--主要
---java
----Application.java
----com
----命中率
-----控制器
-----道
-----EntityDao.java
-----型号
---资源
----application.properties
--测试
---java
----命中率
-----道
-----EntityDaoTestDOTjava
---资源
----应用DOT属性
【问题讨论】:
标签: java spring-boot junit