Feed4Junit官方地址:
http://databene.org/feed4junit.html
Feed4Junit测试数据来自数据库:
Feed4JUnit 1.1.2 发布了,该版本支持数据来自数据库
利用Feed4JUnit能够很方便用随机但校验过的数据执行冒烟测试来提高代码 代码覆盖率和发现由非常特殊的数据结构产生的Bug。此外还可以利用Feed4JUnit轻松定义等价类测试。
官方文档:
Retrieving test data from a database
Databases can be declared and configured with a @Database annotation. When applying the annotation to a class, the defined database is available to all test methods. When annotating a method, the database is available only in this method. A basic configuration can be performed by specifying the typical JDBC connection settings: url, driver, user,password. The database declaration must specify an id, by which the database can be referred to as @Source of a method. The @Source annotation must refer to the database idand define a SQL query as selector. The number of query result columns must match the number of method parameters:
@RunWith(Feeder.class) |
Alternatively to the explicit database connection configuration, you can place database environment configurations in your user directory and refer to them in the test:
@Database(id = "db", environment = "f4jdb") |
Read more about environment files in the DB Sanity environment files documentation.
Environment Files
An environment file carries the identifier of the environment in its name: An environment 'mydb' is configured in a properties file 'mydb.env.properties'. The environment name is the only required argument for command line execution of DB Sanity.
The related properties file first is searched in the current working directory. If it is not found there, DB Sanity looks up the environment in a central configuration directory below your user's home directory: $USER.HOME/databene/. This way you do not need to copy environment files for each DBSanity project you are using and can reuse them in other Databene applications like Benerator. You can as well specify an environment name that refers to another directory. For example, an environment name 'config/test' refers to a file'config/test.env.properties'.
In the file, you need to specify details for connecting your database with a JDBC driver:
Name | Description | |
db_url | JDBC URL of the database | required |
db_driver | Java class name of the JDBC driver | required |
db_user | user name for database login | optional |
db_password | password for database login | optional |
db_catalog | the (JDBC) catalog to use | optional |
db_schema | the (JDBC) schema to use | optional |
If you are not familiar with JDBC, you can look up driver archive names, class names and url formats in a table in the Benerator documentation: http://databene.org/databene-benerator/116-my-first-ide-and-maven-based-benerator-project.html.
@Database注释在类头部:
package com.easyway.feed4junit; import static org.junit.Assert.assertEquals; import org.databene.benerator.anno.Database; import org.databene.benerator.anno.Source; import org.databene.feed4junit.Feeder; import org.junit.Test; import org.junit.runner.RunWith; import com.easyway.junit4.JunitSample.UserAccess; /* * Feed4JUnit - Get Data from Database, all test methods can use the database data */ @RunWith(Feeder.class) @Database( id = "testdb", url = "jdbc:oracle:thin:@192.168.xxx.xxx:1521:ticket", driver = "oracle.jdbc.driver.OracleDriver", user = "xxx", password = "xxx") public class F4JfromDB { @Test @Source(id = "testdb", selector = "select * from test_feed4junit") public void testAccessCheck(String userName, String pw, String expected) { Boolean bSucess = UserAccess.accessCheck(userName.trim(), pw.trim()); assertEquals(expected.trim(), bSucess.toString()); } }
@Database注释在方法:
package com.easyway.feed4junit; import static org.junit.Assert.assertEquals; import org.databene.benerator.anno.Database; import org.databene.benerator.anno.Source; import org.databene.feed4junit.Feeder; import org.junit.Test; import org.junit.runner.RunWith; import com.easyway.junit4.JunitSample.UserAccess; /* * Feed4JUnit - Get Data from Database, only the specified method can use the database data */ @RunWith(Feeder.class) public class F4JfromDB_Method { @Test @Database( id = "testdb", url = "jdbc:oracle:thin:@192.168.45.171:1521:ticket", driver = "oracle.jdbc.driver.OracleDriver", user = "tbs", password = "tbs") @Source(id = "testdb", selector = "select * from test_feed4junit") public void testAccessCheck(String userName, String pw, String expected) { Boolean bSucess = UserAccess.accessCheck(userName.trim(), pw.trim()); assertEquals(expected.trim(), bSucess.toString()); } }
@Database注释基于配置文件的方式:
在项目src的统计目录下文件必须是同级别的下文件:
dbtest.env.properties文件名称的格式为Database的注解属性中environment+“env.properties”
内容如下:
db_url=jdbc:oracle:thin:@192.168.xxxx.xxx:1521:ticket
db_driver=oracle.jdbc.driver.OracleDriver
db_user=xxxx
db_password=xxxx
测试代码:
package com.easyway.feed4junit; import static org.junit.Assert.assertEquals; import org.databene.benerator.anno.Database; import org.databene.benerator.anno.Source; import org.databene.feed4junit.Feeder; import org.junit.Test; import org.junit.runner.RunWith; import com.easyway.junit4.JunitSample.UserAccess; /* * Feed4JUnit - Get Data from Database, only the specified method can use the database data */ @RunWith(Feeder.class) public class F4JfromConfiguration { @Test @Database(id = "testdb", environment = "dbtest") @Source(id = "testdb", selector = "select * from test_feed4junit") public void testAccessCheck(String userName, String pw, String expected) { Boolean bSucess = UserAccess.accessCheck(userName.trim(), pw.trim()); assertEquals(expected.trim(), bSucess.toString()); } }
相关推荐
Feed4JUnit能够让编写JUnit的参数化测试变得简便,并为这些测试提供预定义或随机测试数据。它能够从业务分析人员定义好的CVS或 Excel文件读取测试用例数据并在构建/单元测试框架中报告测试成功。利用Feed4JUnit能够...
Feed4JUnit makes it easy to write parameterized tests for the JUnit framework and feed them with predefined or random test data
ssm配置多个数据源、自己写的一个demo,按照所写的代码去修改相应的代码即可使用,配置文件只需该数据库连接,这里使用了两个数据库连接,若使用多个链接时可仿照此方法在继续添加即可,另附junit4测试类供大家测试...
包涵了junit4新特性assertThat断言和mysql,oracle俩种数据库的增删改查测试
Junit4使用方法
spring3+hibernate4+maven+junit 多库多数据源实现,非常好的一个学习多数据分点管理,分别连接不同类型数据源的方法
JUnit4
水平分表是在同一个数据库内,把同一个表的数据按一定规则拆到多个表中,多数据源采用 mybatis-plus的dynamic-datasource 分库分表采用sharding-jdbc 数据库连接池管理是alibaba的druid-spring-boot-starter 项目使用...
JUnit银行数据使用Junit 5进行数据库测试的简单示例不要忘记将Junit 5 jar添加到类路径中。 不要忘记在类路径中添加所选库的jdbc jar。 测试中使用的银行是Oracle,只需改用您的另一家银行即可。
junit测试JDBC错误源代码,使用该源代码运行理论上插入数据库会插入两条数据,不知道是哪里出问题
eclipse上通过一个简单例子演示使用JUnit4进行测试
在Eclipse中使用JUnit4进行单元测试!
Junit的简单使用,Junit的简单使用
基于Feed4JUnit架构的单元测试技术研究与应用.docx
该文档介绍了如何使用JUNIT4进行单元测试,给出了一个详细的例子
junit4教程(《Junit4初探》)
junit总结使用例子junit总结使用例子junit总结使用例子
单元测试工具Junit的简单使用
Junit4使用教程详解+源码下载.rar
因 jdk5 中的新特性,JUnit4 也因此有了很大的改变。下面详细介绍 JUnit4 的使用方法,只是举个例子说明junit4 的使用