gpt4 book ai didi

java - testng 套件第二次测试失败 -> nullpointerexception

转载 作者:行者123 更新时间:2023-11-29 04:37:46 36 4
gpt4 key购买 nike

我有一些要使用 testng 套件执行的 Dao 测试我正在 @BeforeSuite 中初始化所有 daos 但只有第一个测试可以使用 daos ,其他测试在调用 dao 时生成空指针

任何帮助将不胜感激非常感谢

基础测试:

 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>

堆栈跟踪:

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)

我做错了什么?如果独立运行,测试工作正常,只有当我一次运行所有测试时才会失败

非常感谢

最佳答案

你必须重做你的测试:

@BeforeSuite 只被套件调用一次。您有 2 个类,然后第一个类实例化了 dao,另一个没有实例化(在本例中为 NPE)。

一种选择是为您的 dao 使用 static。另一种解决方案是将 documentDao = DAOFactory.getDocumentDAO(); 放入 @BeforeClass 中。但是您可以找到许多其他(更好的)解决方案。

关于java - testng 套件第二次测试失败 -> nullpointerexception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40531669/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com