简体   繁体   English

如何使用Spring框架测试JDBC代码?

[英]How can I test JDBC code using spring framework?

I was looking through some of the framework for testing this and from what I can tell, it seems like there is a lot of depreciation. 我一直在研究一些测试框架,据我所知,似乎存在很多折旧。 Most of the code seems to be pointing to this SimpleJdbcTemplate class which was depreciated as of Spring 3.1. 大多数代码似乎都指向此SimpleJdbcTemplate类,该类从Spring 3.1开始已弃用。 Is there an alternative to using this when mocking database connections? 在模拟数据库连接时,还有其他替代方法吗?

My goal is to be able to write test cases that do not depend on the existence of a database. 我的目标是能够编写不依赖于数据库存在的测试用例。 I am working with Spring 3.1 and Java 7. The database I was given was a SQL database. 我正在使用Spring 3.1和Java7。给出的数据库是SQL数据库。

You can try something like H2 . 您可以尝试使用H2之类的方法 http://www.h2database.com/html/main.html http://www.h2database.com/html/main.html

db.driverClassName=org.h2.Driver
db.url=jdbc:h2:mem:test
db.username=
db.password=
db.dialect=org.hibernate.dialect.H2Dialect

or HSQL . HSQL http://hsqldb.org/ http://hsqldb.org/

db.dialect=org.hibernate.dialect.HSQLDialect

For speed up my tests on my DAO Layer implemented with Spring JDBC, I use an in-memory database. 为了加快使用Spring JDBC实现的DAO层上的测试速度,我使用了内存数据库。

If you are using maven these are the dependencies that you need: 如果您使用的是maven,则需要以下依赖项:

<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <version>2.3.2</version>
    <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>${org.springframework-version}</version>
  <scope>test</scope>
</dependency>

The first one is the embedded database and the second one is for spring test jars. 第一个是嵌入式数据库,第二个是用于弹簧测试罐。

Well, I will introduce how I do the test and the configuration. 好吧,我将介绍如何进行测试和配置。

MyApplicationStandaloneConfiguration.java MyApplicationStandaloneConfiguration.java

@Configuration
@Profile("local")
@ComponentScan("cat.mypackages")
public class MyApplicationStandaloneConfiguration {

    @Bean
    public DataSource myApplicationDataSource() throws NamingException {

        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
        dataSource.setUrl("jdbc:hsqldb:mem:test");
        dataSource.setUsername("sa");
        dataSource.setPassword("");

        ResourceDatabasePopulator pop = new ResourceDatabasePopulator();
        pop.addScript(new ClassPathResource("sql/schema.sql"));
        pop.addScript(new ClassPathResource("sql/one_table_inserts.sql"));
        pop.addScript(new ClassPathResource("sql/another_table_inserts.sql"));
        return dataSource;
    }

}

Note that I annotated this @Profile as a local, this means that I can create multiple configuration files for different environments, for example, imagine that you want to run an integration tests against your development / qa / uat database. 请注意,我已将此@Profile注释为本地,这意味着我可以为不同的环境创建多个配置文件,例如,假设您要针对您的development / qa / uat数据库运行集成测试。

See that I'm loading the schema.sql and some other sql scripts. 看到我正在加载schema.sql和其他一些sql脚本。 This contains my schema for my tables to create and the inserts that I want to expect on my test cases. 这包含要创建的表的架构以及希望在测试用例中插入的内容。

And then it's easy, create a test case like that: 然后很容易地创建一个如下的测试用例:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyApplicationStandaloneConfiguration.class)
@ActiveProfiles("local")
public class MyDaoTestCase {

    @Autowired
    private MyDao dao;

    @Test
    public void testFindId() {
        SomeDto dto = dao.findById(123);
        assertNotNull(dto);
        ...
    }

}

Hope it hepls! 希望它帮助!

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

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