java - Using jOOQs MockResult class in an UPDATE statement for jUnit tests -
i working set unit test cases of dao layer make sure responding in way need to, , need unit test jooq code. running jooq 3.6.1 @ time, , trying use jdbc mocking described in link jooq documentation.
my unit testing environment leveraging junit 4.12 , mockito.
unfortunately, while works select queries, struggling figure out doing wrong update queries. in non-junit unit testing, know function working (it pretty basic not shocking), able have automated unit tests on piece of code, too.
my unit test set in following manner:
public class userdaotest { private static userdao userdao; private static logger log = logger.getlogger(userdaotest.class.getname()); private class mymockdataprovider implements mockdataprovider { @override public mockresult[] execute ( mockexecutecontext ctx ) throws sqlexception { dslcontext create = dsl.using(sqldialect.mysql); mockresult[] result = new mockresult[1]; string sql = ctx.sql(); if ( sql.touppercase().startswith("update") ) { result<userrecord> userrecordresult = create.newresult(user); userrecordresult.add(create.newrecord(user)); userrecordresult.get(0).setvalue(user.id, 1000); userrecordresult.get(0).setvalue(user.username, "brumbyupdatetest"); userrecordresult.get(0).setvalue(user.created_on, new timestamp((new date()).gettime())); userrecordresult.get(0).setvalue(user.updated_on, new timestamp((new date()).gettime())); userrecordresult.get(0).setvalue(user.user_type, "poster"); userrecordresult.get(0).setvalue(user.password, bcrypt.hashpw("12341234", bcrypt.gensalt())); result[0] = new mockresult(1, userrecordresult); } log.info("[execute] :: mockresult.rows=" + result[0].rows); log.info("[execute] :: mockresult.data=" + result[0].data); log.info("[execute] :: result=" + result.tostring()); return result; } @before public void setuptest() { datasourcefactory mockdatasource = mock(datasourcefactory.class); manageddatasource ds = mock(manageddatasource.class); when(mockdatasource.build(any(), any())).thenreturn(ds); environment environment = mock(environment.class); try { when(ds.getconnection()).thenreturn(connection); } catch (sqlexception sqle) { log.warning("[setuptest] :: sqle=" + sqle.getmessage()); } userdao = new userdao(mockdatasource.build(environment.metrics(), "ds")); } @test public void testupdatepassword() { boolean out = userdao.updatepassword(1000, "12341234"); assertthat(out).isequalto(true); } }
my understanding reading jooq javadocs should end using mymockdataprovider , respond when update query executed result created. believe creating new mockresult has row count of 1 , user object in specified. when @ logging statements put in there, happening.
jul 23, 2015 9:23:04 com.mycompany.dao.userdaotest$mymockdataprovider execute info: [execute] :: mockresult.rows=1 jul 23, 2015 9:23:04 com.mycompany.dao.userdaotest$mymockdataprovider execute info: [execute] :: mockresult.data={expected data based on mymockprovider}
but when execute unit test, userdao object seeing response jooq record count of 0.
jul 23, 2015 9:23:04 com.mycompany.dao.userdao updatepassword warning: [updatepassword] :: count=0
the code trying test following:
public class userdao { public boolean updatepassword ( integer id, string newpassword ) { try ( connection conn = ds.getconnection() ) { dslcontext updatepassword = dsl.using(conn, sqldialect.mysql); int count = updatepassword.update(user) .set(user.password, bcrypt.hashpw(newpassword, bcrypt.gensalt())) .where(user.id.eq(id)) .execute(); log.warning("[updatepassword] :: count=" + count); if ( count != 1 ) return false; return true; } catch ( sqlexception sqle ) { log.warning("[updatepassword] :: sqle=" + sqle.getmessage()); return false; } } }
am incorrectly using mockresult object? maybe misunderstanding, believe update returns me record count impacted, based on mockresult returning should 1. userdao object returning 0 instead, , test failing.
thanks in advance help, or direction in can solve this.
i don't know if doing right since answering own question, think am. spent last 2 days looking , stepping through jooq code see issue was. turns out issue in providing data within mockresult object in line of test:
result[0] = new mockresult(1, userrecordresult);
since not fetching data after update, shouldn't have included it. basically, can remove of lines of code of building userrecordresult object, , instead set mockresult object follows:
result[0] = new mockresult(1, null);
i believe says me there result, , no data. since there no data returned now, mockstatement.execute0() function returns false instead of true. , when in abtractquery.execute() function, if block lines 410 - 413 triggered , result count set value.
if (!stmt.execute()) { result = stmt.getupdatecount(); ctx.rows(result); }
before, when had data within mockresult object, mockstatement.execute0() function returning true, , if-block above never executed, , result
int never changed when created zero. null data, line result = stmt.getupdatecount();
triggered , value set.
Comments
Post a Comment