java - testng suite second test fail -> nullpointerexception -
i have dao tests want execute using testng suite initializing daos in @beforesuite
first test can use daos , others tests generate nullpointer when calling dao
any appreciated thank much
base test :
public class basetest { protected documentdaoimpl documentdao; protected userdaoimpl userdao; @beforesuite public void beforesuite() { entitymanagerutil.setpersistenceunitname("db_test"); //initialize entitymanagerfactory once tests daos documentdao = daofactory.getdocumentdao(); userdao = daofactory.getuserdao(); } @aftersuite public void aftersuite() { entitymanagerutil.closeentitymanagerfactory(); } }
test 1 :
public class documentdaoimpltest extends basetest{ @test(dataproviderclass = documentdataprovider.class, dataprovider = documentdataprovider.one_document_provider) public void findbymatricule(document document) { documentdao.create(document); document result = documentdao.findbymatricule(document.getmatricule()); assert.assertnotnull(result); assert.assertequals(result.getmatricule(), document.getmatricule()); } }
test 2 :
public class userdaoimpltest extends basetest{ @test(dataproviderclass = userdataprovider.class, dataprovider = userdataprovider.one_user_provider) public void findbydocumentid(user user) { // nullpointer userdao userdao.create(user); user result = userdao.findbydocumentid(user.getdocumentid()); assert.assertnotnull(result); assert.assertequals(result.getdocumentid(), user.getdocumentid()); } }
testng.xml
<test name="daotest"> <packages> <package name="com.dev.test.dao"/> </packages> </test>
stacktrace :
java.lang.nullpointerexception @ com.dev.test.dao.userdaoimpltest.findbydocumentid(userdaoimpltest.java:14) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ org.testng.internal.methodinvocationhelper.invokemethod(methodinvocationhelper.java:86) @ org.testng.internal.invoker.invokemethod(invoker.java:643) @ org.testng.internal.invoker.invoketestmethod(invoker.java:820) @ org.testng.internal.invoker.invoketestmethods(invoker.java:1128) @ org.testng.internal.testmethodworker.invoketestmethods(testmethodworker.java:129) @ org.testng.internal.testmethodworker.run(testmethodworker.java:112) @ org.testng.testrunner.privaterun(testrunner.java:782) @ org.testng.testrunner.run(testrunner.java:632) @ org.testng.suiterunner.runtest(suiterunner.java:366) @ org.testng.suiterunner.runsequentially(suiterunner.java:361) @ org.testng.suiterunner.privaterun(suiterunner.java:319) @ org.testng.suiterunner.run(suiterunner.java:268) @ org.testng.suiterunnerworker.runsuite(suiterunnerworker.java:52) @ org.testng.suiterunnerworker.run(suiterunnerworker.java:86) @ org.testng.testng.runsuitessequentially(testng.java:1244) @ org.testng.testng.runsuiteslocally(testng.java:1169) @ org.testng.testng.run(testng.java:1064) @ org.testng.remote.abstractremotetestng.run(abstractremotetestng.java:132) @ org.testng.remote.remotetestng.initandrun(remotetestng.java:236) @ org.testng.remote.remotetestng.main(remotetestng.java:81)
what doing wrong ? tests works if run independently , fails when run @ once
thank much
you have rework tests:
@beforesuite
called once suite. have 2 classes first has dao instanced, other not (npe in case).
one option use static
dao. solution put documentdao = daofactory.getdocumentdao();
@beforeclass
. can find many other (better) solutions.
Comments
Post a Comment