gpt4 book ai didi

java - 用于 DAO JUnit 测试的 SpringBootTest

转载 作者:行者123 更新时间:2023-12-01 21:08:56 25 4
gpt4 key购买 nike

我正在尝试对 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

----hitstpa

----- Controller

-----dao

------EntityDao.java

-----型号

---资源

----application.properties

--测试

---java

----hitstpa

-----dao

------EntityDaoTestDOTjava

---资源

----应用程序DOT属性

最佳答案

首先,对于集成测试,您需要一个包含一些固定数据的集成数据库。

  • 现在您需要创建一个配置类来创建集成测试特定依赖项(我将其命名为 DbConfig
    .java
    )
  • 下一步是向集成测试添加@ContextConfiguration注释类并提供DbConfig.java,这样当测试运行时它将创建datasource依赖项并将其注入(inject)到容器

示例代码

    @Configuration
public class DbConfig {

@Bean
public DataSource dataSource() {

//Create the DataSource with integration-DB properties

return dataSource;
}
}

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@ContextConfiguration(classes=DbConfig.class)
public class EntityDaoTest {

}

关于java - 用于 DAO JUnit 测试的 SpringBootTest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41625049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com