用于DAO JUnit测试的SpringBootTest

时间:2017-01-12 23:42:48

标签: java spring-boot junit

我正在尝试以下DAO的单元测试。

我无法识别DataSource。

我可以获得有关如何解决此问题的提示吗?

以下详细信息

@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

-----控制器

- - - - DAO

------ EntityDao.java

-----模型

---资源

---- application.properties

- 测试

--- java的

---- hitstpa

- - - - DAO

------ EntityDaoTestDOTjava

---资源

---- applicationDOTproperties

2 个答案:

答案 0 :(得分:1)

为了记录,我相信这不是一个好的单元测试。此测试要求localhost上存在mysql数据库。

无论如何,错误表明Spring Context未正确加载。使用SpringBootTest时,Spring会以root身份使用测试包来查找配置。因此,如果它是lower而不是您的配置类,则不会是它们。

看看Spring's documentation

  

搜索算法从包含测试的包开始工作   直到找到@SpringBootApplication或@SpringBootConfiguration   注释类。只要你在合理的情况下构建代码   通常可以找到您的主要配置。

解决方案:

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

答案 1 :(得分:1)

首先,对于集成测试,您需要集成Db和一些固定数据。

  • 现在您需要创建一个将创建的配置类 集成测试特定依赖项(我将其命名为df = pd.concat([ts1, ts2], axis = 1, keys=['ts1','ts2']) df.index = df.index.values print (df) ts1 ts2 (a, A) 0.407183 0.866382 (b, B) 0.069167 0.975522 (c, C) 0.697429 0.855803 (d, D) 0.453543 0.011714 (e, E) 0.722056 0.359978 (f, F) NaN 0.729991
  • 接下来是在集成测试中添加DbConfig .java注释 class并提供@ContextConfiguration,以便在测试运行时它会 创建DbConfig.java依赖项并将其注入容器

示例代码

datasource