简体   繁体   English

用于DAO JUnit测试的SpringBootTest

[英]SpringBootTest for DAO JUnit test

I am attempting the following unit test of a DAO. 我正在尝试以下DAO的单元测试。

I am unable to get the DataSource recognized. 我无法识别数据源。

Can I get a tip on how to resolve this? 我可以获取有关如何解决此问题的提示吗?

Details below 详情如下

@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);
    }
}

The relevant aspects of the DAO class are as follows DAO类的相关方面如下

@Repository
public class EntityDao extends GenericDao<Entity>{

    public EntityDao(DataSource dataSource) {/.../}
}

My src/test/resources/application.properties file is as follows 我的src / test / resources / application.properties文件如下

# Database
spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.username=dbuser
spring.datasource.password=dbpass

Trace from running as JUnit test in Eclipse 在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. ...`

Application structure 应用结构

-src -src

--main - 主要

---java -Java

----Application.java ----Application.java

----com ---- com

----hitstpa ---- hitstpa

-----controller -----控制器

-----dao -----道

------EntityDao.java ------EntityDao.java

-----model - - -模型

---resources -资源

----application.properties ----application.properties

--test - 测试

---java -Java

----hitstpa ---- hitstpa

-----dao -----道

------EntityDaoTestDOTjava ------ EntityDaoTestDOTjava

---resources -资源

----applicationDOTproperties ---- applicationDOT属性

For the record, I believe this is not a good unit test. 作为记录,我认为这不是一个好的单元测试。 This test requires that a mysql databases exists on localhost. 此测试要求localhost上存在mysql数据库。


Anyhow, the errors suggest that the Spring Context isn't loaded correctly . 无论如何, 这些错误表明Spring Context未正确加载 When using SpringBootTest , Spring looks for the configuration using the test's package as root. 当使用SpringBootTest ,Spring使用测试包作为根来查找配置。 So, if it's lower than your Configuration classes, it won't them. 因此,如果它lower您的Configuration类,则不会。

Take a look at Spring's documentation : 看一下Spring的文档

The search algorithm works up from the package that contains the test until it finds a @SpringBootApplication or @SpringBootConfiguration annotated class. 搜索算法从包含测试的程序包开始工作,直到找到@SpringBootApplication或@SpringBootConfiguration注释的类。 As long as you've structure your code in a sensible way your main configuration is usually found. 只要您以合理的方式构造代码,通常就可以找到您的主要配置。

Solution: 解:

You can either move your tests to the same level as your SpringBoot Main class or change it to: @SpringBootTest(classes = YourSpringBootMainClass.class) 您可以将测试移至与SpringBoot Main类相同的级别,也可以将其更改为: @SpringBootTest(classes = YourSpringBootMainClass.class)

First of all for integration tests you need an integration Db with some fixed data. 首先,对于集成测试,您需要具有一些固定数据的集成Db。

  • Now you need to create a configuration class which will create the integration test specific dependencies(I have named it as DbConfig .java ) 现在您需要创建一个配置类,该类将创建集成测试特定的依赖项(我将其命名为DbConfig .java
  • Next is add @ContextConfiguration annotation to the integration test class and provide DbConfig.java , so that when test runs it will create the datasource dependency and inject it to the container 接下来是在集成测试类中添加@ContextConfiguration批注并提供DbConfig.java ,以便在测试运行时将创建datasource依赖项并将其注入到容器中

Sample Code 样例代码

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM