我想要使用testng套件执行一些dao测试
我正在初始化@BeforeSuite
中的所有daos,但只有第一个测试可以使用daos,其他测试在调用dao时生成nullpointer
任何帮助将不胜感激 非常感谢你
基础测试:
public class BaseTest {
protected DocumentDaoImpl documentDao;
protected UserDaoImpl userDao;
@BeforeSuite
public void beforeSuite() {
EntityManagerUtil.setPersistenceUnitName("db_test"); //initialize entityManagerFactory once for all tests as well as daos
documentDao = DAOFactory.getDocumentDAO();
userDao = DAOFactory.getUserDao();
}
@AfterSuite
public void afterSuite() {
EntityManagerUtil.closeEntityManagerFactory();
}
}
测试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());
}
}
测试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
at com.dev.test.dao.UserDaoImplTest.findByDocumentId(UserDaoImplTest.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
我做错了什么? 如果独立运行,测试可以工作,只有当我一次运行时才会失败
非常感谢
答案 0 :(得分:3)
你必须重新测试你的测试:
@BeforeSuite
只能按套件调用一次。
你有两个类,然后第一个有dao实例,另一个没有(在这种情况下是NPE)。
一种选择是为你的dao使用static
。
另一种解决方案是将documentDao = DAOFactory.getDocumentDAO();
放入@BeforeClass
。
但是你可以找到许多其他(更好的)解决方案。