ulquiorra
ulquiorra

Reputation: 945

testng suite second test fail -> nullpointerexception

I have some dao tests i want to execute using testng suite I am initializing all daos in @BeforeSuite but only the first test can use the daos , others tests generate nullpointer when calling to a dao

Any help would be appreciated Thank you very much

Base test :

 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();
  }
}

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
    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)

What i am doing wrong ? The tests works if run independently , only fails when i run all at once

Thank you very much

Upvotes: 1

Views: 1234

Answers (1)

juherr
juherr

Reputation: 5740

You have to rework your tests:

@BeforeSuite is only called once by suite. You have 2 classes then the first has the dao instanced, the other not (NPE in this case).

One option is to use static for your dao. Another solution could be to put documentDao = DAOFactory.getDocumentDAO(); into @BeforeClass. But you can find many other (better) solutions.

Upvotes: 3

Related Questions