java - JOOQ MockDataProvider - how to return different mocks depending on some conditions? -
recenty implementing unit test using jooq mockdataprovider. when wanted use mock provider in dao many selects had use many if-else statements.
according to: http://www.jooq.org/doc/latest/manual/tools/jdbc-mocking, need check if sql starts query. , sqls used in dao start 3 different ways, used pretty complex if-else construct.
second thing - wanted mockdataprovider return mock result when sql executed first time, , not return results - in dao iterated in loop 5 times, , each time dao should check in database. had no idea how mock such behaviour, used simple counter - looks awful, , want implemented in way. here code:
public class someprovider implements mockdataprovider { private static final string statement_not_supported_ = "statement not supported: "; private static final string select_meta = "select \"myschema\".\"meta\"."; private static final string select_client = "select \"myschema\".\"client\"."; private static final string select_key = "select \"myschema\".\"key\"."; private static final string test_secret_key = "some key"; private static final string key = "40sld"; private static final string drop = "drop"; private static final string some_url = "something"; private static final string monkey = "monkey"; private static final int first_iterarion_counter_value = 0; private final long keyid; int counter = 0; public someprovider(long keyid) { this.keyid = keyid; } @override public mockresult[] execute(mockexecutecontext ctx) throws sqlexception { dslcontext create = dsl.using(sqldialect.postgres); mockresult[] mock = new mockresult[3]; string sql = ctx.sql(); if (sql.touppercase().startswith(drop)) { throw new sqlexception(statement_not_supported_ + sql); } else if (sql.startswith(select_client)) { result<clientrecord> result = create.newresult(client); result.add(create.newrecord(client)); result.get(0).setvalue(client.id, 1l); result.get(0).setvalue(client.secret_key, test_secret_key); mock[0] = new mockresult(1, result); } else if (sql.startswith(select_meta)) { result<metarecord> metaresult = create.newresult(meta); metaresult.add(create.newrecord(meta)); metaresult.get(0).setvalue(meta.id, 1l); metaresult.get(0).setvalue(meta.url, some_url); metaresult.get(0).setvalue(meta.key, key); metaresult.get(0).setvalue(meta.option, keyid); mock[0] = new mockresult(1, metaresult); } else if (sql.startswith(select_key)) { result<keyrecord> keyresult = create.newresult(key); if (counter == first_iterarion_counter_value) { // first select returns monkey, rest return no results keyresult.add(create.newrecord(key)); keyresult.get(0).setvalue(key.id, 1l); keyresult.get(0).setvalue(key.value, monkey); mock[0] = new mockresult(1, keyresult); } else { mock[0] = new mockresult(0, keyresult); } counter++; } return mock; } }
it works looks bad. sum question is: how return (using 1 provider) different results depending on query , number of query executions. maybe class simple dslcontext mocking, not mock whole dao uses many queries many times using 1 dslcontext.
i have done , no fun - mock in end needs tests of own... approach, enable mock provider configurable expect , return.
provider.when(..).times(5).return(...)
and on... return can take generator literal values. when use regex strings
Comments
Post a Comment