Ole*_*eev 10
作为Mocking framewroks的一部分,有一些"void"JDBC驱动程序,例如 Mockrunner的MockDriver.
但使用它需要一些编码.
那是因为当Java应用程序连接到数据库时,它会以表单形式提供JDBC URL jdbc:mysql://localhost.系统正在搜索在其中注册的驱动程序以处理此类URL并选择正确的驱动程序.有关哪种URL类型驱动程序支持的信息包含在驱动程序本身中,并且模拟驱动程序不可能在其中保存所有已知的URL类型 - 那里没有通配符,任何列表都不会满.
因此,如果您能够在连接到数据库之前在应用程序中调用JDBCMockObjectFactory.registerMockDriver(),它将完成这项工作.如果不是 - 我不认为这是可能的.但是,稍微修改一下驱动程序代码就可以了......但是再次 - 需要编码.
jOOQ附带一个MockConnection可以提供的a MockDataProvider,它比完整的JDBC API更容易实现.这篇博客文章展示了如何使用MockConnection:http://blog.jooq.org/2013/02/20/easy-mocking-of-your-database/
一个例子:
MockDataProvider provider = new MockDataProvider() {
// Your contract is to return execution results, given a context
// object, which contains SQL statement(s), bind values, and some
// other context values
@Override
public MockResult[] execute(MockExecuteContext context)
throws SQLException {
// Use ordinary jOOQ API to create an org.jooq.Result object.
// You can also use ordinary jOOQ API to load CSV files or
// other formats, here!
DSLContext create = DSL.using(...);
Result<MyTableRecord> result = create.newResult(MY_TABLE);
result.add(create.newRecord(MY_TABLE));
// Now, return 1-many results, depending on whether this is
// a batch/multi-result context
return new MockResult[] {
new MockResult(1, result)
};
}
};
// Put your provider into a MockConnection and use that connection
// in your application. In this case, with a jOOQ DSLContext:
Connection connection = new MockConnection(provider);
DSLContext create = DSL.using(connection, dialect);
// Done! just use regular jOOQ API. It will return the values
// that you've specified in your MockDataProvider
assertEquals(1, create.selectOne().fetch().size());
Run Code Online (Sandbox Code Playgroud)
还有MockFileDatabase,通过编写如下文本文件,可以帮助您将伪结果与SQL字符串进行匹配:
# This is a sample test database for MockFileDatabase
# Its syntax is inspired from H2's test script files
# When this query is executed...
select 'A' from dual;
# ... then, return the following result
> A
> -
> A
@ rows: 1
# Just list all possible query / result combinations
select 'A', 'B' from dual;
> A B
> - -
> A B
@ rows: 1
select "TABLE1"."ID1", "TABLE1"."NAME1" from "TABLE1";
> ID1 NAME1
> --- -----
> 1 X
> 2 Y
@ rows: 2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8508 次 |
| 最近记录: |