How to initialise and create a ResultSet and Recor

2019-08-14 11:34发布

I need to write some unit tests for that I have to mock the Result set and Record with some dummy data. I don't know how to initialize and instantiate them. Please help

Thanks in advance.

1条回答
虎瘦雄心在
2楼-- · 2019-08-14 12:06

jOOQ has a few built-in mock features, see the chapter JDBC mocking for unit testing of the manual, it might be what you are looking for.

However, to simply create a jOOQ's Result or Record, you may use the DSLContext for that:

// Create the record using the jOOQ generated classes and set a property
MyTableRecord record1 = DSL.using(configuration).newRecord(MY_TABLE);
record1.setValue(MY_TABLE.MY_PROPERTY, "value");

// or simply...
MyTableRecord record2 = new MyTableRecord();
record2.setMyProperty("value");

// Then you can populate the Result
Result<MyTableRecord> result = DSL.using(configuration).newResult(MY_TABLE);
result.add(record1);
result.add(record2);

You also mention ResultSet, if by that you mean the JDBC ResultSet, then it might be a bit more complicated to mock that. Instead, I would suggest DbUnit, that is not a mock for JDBC classes, but you'll assist you to setup your database for tests, which might help you to get the same effects you would have by mocking JBDC classes.

查看更多
登录 后发表回答