【问题标题】:SpringBootTest for DAO JUnit testSpringBootTest 用于 DAO JUnit 测试
【发布时间】: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


    【解决方案1】:

    郑重声明,我认为这不是一个好的单元测试。此测试要求 localhost 上存在 mysql 数据库。


    无论如何,错误表明 Spring 上下文未正确加载。当使用SpringBootTest 时,Spring 使用测试包作为根来查找配置。因此,如果它是 lower 而不是您的配置类,则不会。

    看看Spring's documentation

    搜索算法从包含测试的包开始 直到找到 @SpringBootApplication 或 @SpringBootConfiguration 注释类。只要你的代码结构合理 通常找到您的主要配置的方式。

    解决办法:

    您可以将测试移动到与 SpringBoot Main 类相同的级别,也可以将其更改为:@SpringBootTest(classes = YourSpringBootMainClass.class)

    【讨论】:

    • 同意这不是一个好的单元测试 - 只是想暂时设置一下。
    • 我将 classes 属性添加到 SpringBootTest 注释中,并且行为没有改变。更多关于上面添加的应用程序结构
    【解决方案2】:

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

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

    示例代码

        @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 {
    
        }
    

    【讨论】:

    • 这些步骤也让我更进一步——需要一些内部修复——需要添加 tomcat jdbc 依赖项——需要在我的所有 DAO 上声明零参数构造函数。
    • youtube.com/watch?v=ixIxXRoCr5w -- 在我解决了这些初始问题后,这帮助我进入了一个新的水平
    猜你喜欢
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 2019-03-12
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多