java - Autowire jdbcTemplate in ObjectMother for a junit 4 integration test -
i wondering if can somehow autowire jdbctemplate in objectmother junit 4 integration test.
the purpose of test test if attach document employee, cannot attach afterwards document same name. made employeemother has method creates employee , method inserts it, using jdbctemplate. if try autowire jdbctemplate in employeemother null(test returns npe on first update). seems have access applicationcontexts test itself.
currently set test itself, not to, because create more objectmothers different objects , not set jdbctemplate of them.
here 2 classes (i deleted company name package , imports):
employeemother:
package com.domain.objmother; import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory; import org.springframework.jdbc.core.jdbctemplate; import com.domain.vo.company; import com.domain.vo.contact; import com.domain.vo.employee; import com.domain.vo.identification; import com.domain.vo.role; public class employeemother { private final log log = logfactory.getlog(getclass()); protected jdbctemplate jdbctemplate; private employee empjohndoe; /** * * @return returns admin user username djohn */ public employee getempjohndoe() { empjohndoe = new employee(); empjohndoe.setusername("djohn"); role role = new role(); //as id of role not nullable set 1 = admin role.setid(new long(1)); empjohndoe.setrole(role); empjohndoe.setcompany(new company()); identification identity = new identification(); identity.setfirstname("john"); identity.setlastname("doe"); identity.setcontact(new contact()); empjohndoe.setidentity(identity); return empjohndoe; } public void setempjohndoe(employee empjohndoe) { this.empjohndoe = empjohndoe; } /** * important! insert not cover details of employee: * inserts null in following columns: * pswd, * image, * cnt_id, - should list of associated contacts * salt, * is_active, * default_work_hours * insert in tab_identifications triggers trig_idnt inserts stuff in audit table * work need register logged user * that's why call pac_security.pro_setctx('emp_user_name','adminusername'); (i used admin) * preferred doing rather inserting djohn in tab_employees, * registering djohn logged inserting identity in tab_identifications * , updating djohn new identity * @param emp - employee inserted */ public void insert(employee emp){ jdbctemplate.update("call pac_security.pro_setctx('emp_user_name','adminusername')"); long identityid = jdbctemplate.queryforobject("select max(ti.id)+1 tab_identifications ti", long.class); emp.getidentity().setid(identityid); jdbctemplate.update(""+ " insert tab_identifications ("+ " id, first_name, middle_name, last_name, cnp, ci_char, ci_number, birth_date, invalidity,"+ " cas_name, ci_issue_date, ci_issuer, cnt_id"+ " )" + " values (?,?,?,?,?,?,?,?,?,?,?,?,?)", new object[]{emp.getidentity().getid(), emp.getidentity().getfirstname(), emp.getidentity().getmiddlename(), emp.getidentity().getlastname(), emp.getidentity().getcnp(), emp.getidentity().getidcardserial(), emp.getidentity().getidcardnumber(), emp.getidentity().getbirthdate(), emp.getidentity().getinvalidity(), emp.getidentity().getcas(), emp.getidentity().getciissuedate(), emp.getidentity().getciissuer(), emp.getidentity().getcontact().getid()} ); long id = jdbctemplate.queryforobject("select max(te.id)+1 tab_employees te", long.class); emp.setid(id); jdbctemplate.update(""+ " insert tab_employees (id, user_name, code, pswd, idnt_id, role_id, comp_id, image, "+ " hire_date, cnt_id, salt, is_expired, is_active, default_work_hours "+ " )" + " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)", new object[]{emp.getid(), emp.getusername(), emp.getcode(), null, emp.getidentity().getid(), emp.getrole().getid(), emp.getcompany().getid(), null, emp.gethiredate(), null, null, emp.getisexpired(), null, null } ); } public jdbctemplate getjdbctemplate() { return jdbctemplate; } public void setjdbctemplate(jdbctemplate jdbctemplate) { this.jdbctemplate = jdbctemplate; } }
homeemployeeserviceimplintegrationtest:
package com.employee.service.impl; import org.junit.before; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.jdbc.core.jdbctemplate; import org.springframework.test.context.junit4.springjunit4classrunner; import com.domain.vo.document; import com.domain.vo.employee; import com.domain.objmother.employeemother; import com.employee.service.homeemployeeservice; import com.util.annotations.transactionaldevtest; import static org.junit.assert.*; @runwith(springjunit4classrunner.class) @transactionaldevtest public class homeemployeeserviceimplintegrationtest { @autowired protected jdbctemplate jdbctemplate; @autowired homeemployeeservice homeemployeeservice; employeemother empmother = new employeemother(); employee empjohndoe; @before public void beforeeachtest() throws exception { empmother.setjdbctemplate(jdbctemplate); empjohndoe = empmother.getempjohndoe(); empmother.insert(empjohndoe); } /** * should not able add document same name * <code>uploaddocument</code> should not insert document if has same name */ @test public void shouldnotbeabletoaddsamedoc(){ document doc = new document(); long id = jdbctemplate.queryforobject("select max(td.id)+1 tab_documents td", long.class); doc.setid(id); doc.setfilename("sameolddocument"); homeemployeeservice.uploaddocument(empjohndoe.getidentity(), doc); id = jdbctemplate.queryforobject("select max(td.id)+1 tab_documents td", long.class); doc.setid(id); homeemployeeservice.uploaddocument(empjohndoe.getidentity(), doc); long docno = jdbctemplate.queryforobject("select count(id) tab_documents td doc_file_name = '" + doc.getfilename() + "'", long.class); if(docno.compareto(new long(2)) == 0){ assertequals("i able add document twice same name!", new long(1), docno); } else{ assertequals("something went wrong when adding 2 documents same name! document should added once or twice, result different!", new long(1), docno); } }
transactionaldevtest define applicationcontexts used. code above works, separate employeemother's code , add identificationmother , documentmother each it's object , insert. not set jdbctemplate each objectmother (things can become ambiguous, setting jdbctemplate test, setting objectmother).
thanks in advance.
i helped collegue , did:
i made annotation objectmothers:
package com.util.annotations; import java.lang.annotation.elementtype; import java.lang.annotation.retention; import java.lang.annotation.retentionpolicy; import java.lang.annotation.target; @target(elementtype.type) @retention(retentionpolicy.runtime) public @interface objectmother {}
i added annotation employeemother:
@objectmother public class employeemother {
and autowired jdbctemplate in employeemother (also deleted getter , setter)
@autowired protected jdbctemplate jdbctemplate;
i added applicationcontext:
the xmlns:
xmlns:context="http://www.springframework.org/schema/context"
with schema location
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
and
<context:component-scan base-package="com.domain.objmother"> <context:include-filter type="annotation" expression="com.util.annotations.objectmother"/> </context:component-scan>
this searches classes annotated objectmother , adds them applicationcontext (so don't have add them 1 one).
and finnaly used employeemother autowired in homeemployeeserviceimplintegrationtest , deleted reference jdbctemplate employeemother in same class:
@autowired employeemother empmother;
final classes:
employeemother:
package com.domain.objmother; import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.jdbc.core.jdbctemplate; import com.domain.vo.company; import com.domain.vo.contact; import com.domain.vo.employee; import com.domain.vo.identification; import com.domain.vo.role; import com.util.annotations.objectmother; @objectmother public class employeemother { private final log log = logfactory.getlog(getclass()); @autowired protected jdbctemplate jdbctemplate; private employee empjohndoe; /** * * @return returns admin user username djohn */ public employee getempjohndoe() { empjohndoe = new employee(); empjohndoe.setusername("djohn"); role role = new role(); //as id of role not nullable set 1 = admin role.setid(new long(1)); empjohndoe.setrole(role); empjohndoe.setcompany(new company()); identification identity = new identification(); identity.setfirstname("john"); identity.setlastname("doe"); identity.setcontact(new contact()); empjohndoe.setidentity(identity); return empjohndoe; } public void setempjohndoe(employee empjohndoe) { this.empjohndoe = empjohndoe; } /** * important! insert not cover details of employee: * inserts null in following columns: * pswd, * image, * cnt_id, - should list of associated contacts * salt, * is_active, * default_work_hours * insert in tab_identifications triggers trig_idnt inserts stuff in audit table * work need register logged user * that's why call pac_security.pro_setctx('emp_user_name','adminusername'); (i used admin) * preferred doing rather inserting djohn in tab_employees, * registering djohn logged inserting identity in tab_identifications * , updating djohn new identity * @param emp - employee inserted */ public void insert(employee emp){ jdbctemplate.update("call pac_security.pro_setctx('emp_user_name','adminusername')"); long identityid = jdbctemplate.queryforobject("select max(ti.id)+1 tab_identifications ti", long.class); emp.getidentity().setid(identityid); jdbctemplate.update(""+ " insert tab_identifications ("+ " id, first_name, middle_name, last_name, cnp, ci_char, ci_number, birth_date, invalidity,"+ " cas_name, ci_issue_date, ci_issuer, cnt_id"+ " )" + " values (?,?,?,?,?,?,?,?,?,?,?,?,?)", new object[]{emp.getidentity().getid(), emp.getidentity().getfirstname(), emp.getidentity().getmiddlename(), emp.getidentity().getlastname(), emp.getidentity().getcnp(), emp.getidentity().getidcardserial(), emp.getidentity().getidcardnumber(), emp.getidentity().getbirthdate(), emp.getidentity().getinvalidity(), emp.getidentity().getcas(), emp.getidentity().getciissuedate(), emp.getidentity().getciissuer(), emp.getidentity().getcontact().getid()} ); long id = jdbctemplate.queryforobject("select max(te.id)+1 tab_employees te", long.class); emp.setid(id); jdbctemplate.update(""+ " insert tab_employees (id, user_name, code, pswd, idnt_id, role_id, comp_id, image, "+ " hire_date, cnt_id, salt, is_expired, is_active, default_work_hours "+ " )" + " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)", new object[]{emp.getid(), emp.getusername(), emp.getcode(), null, emp.getidentity().getid(), emp.getrole().getid(), emp.getcompany().getid(), null, emp.gethiredate(), null, null, emp.getisexpired(), null, null } ); } }
homeemployeeserviceimplintegrationtest:
package com.employee.service.impl; import org.junit.before; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.jdbc.core.jdbctemplate; import org.springframework.test.context.junit4.springjunit4classrunner; import com.domain.vo.document; import com.domain.vo.employee; import com.domain.objmother.employeemother; import com.employee.service.homeemployeeservice; import com.util.annotations.transactionaldevtest; import static org.junit.assert.*; @runwith(springjunit4classrunner.class) @transactionaldevtest public class homeemployeeserviceimplintegrationtest { @autowired protected jdbctemplate jdbctemplate; @autowired homeemployeeservice homeemployeeservice; @autowired employeemother empmother; employee empjohndoe; @before public void beforeeachtest() throws exception { empjohndoe = empmother.getempjohndoe(); empmother.insert(empjohndoe); } /** * should not able add document same name * <code>uploaddocument</code> should not insert document if has same name */ @test public void shouldnotbeabletoaddsamedoc(){ document doc = new document(); long id = jdbctemplate.queryforobject("select max(td.id)+1 tab_documents td", long.class); doc.setid(id); doc.setfilename("sameolddocument"); homeemployeeservice.uploaddocument(empjohndoe.getidentity(), doc); id = jdbctemplate.queryforobject("select max(td.id)+1 tab_documents td", long.class); doc.setid(id); homeemployeeservice.uploaddocument(empjohndoe.getidentity(), doc); long docno = jdbctemplate.queryforobject("select count(id) tab_documents td doc_file_name = '" + doc.getfilename() + "'", long.class); if(docno.compareto(new long(2)) == 0){ assertequals("i able add document twice same name!", new long(1), docno); } else{ assertequals("something went wrong when adding 2 documents same name! document should added once or twice, result different!", new long(1), docno); } } }
Comments
Post a Comment