Search code examples
javaspring-boottestnghsqldbdatabase-testing

How to test database calls with testng in spring boot


I have created small web application in spring boot.I am new for TestNG. I am trying to test my services with testng which calls dao for database operations. I am trying to do it using inmemory database HSQL.

Following is my UserService

@Service
class UserServiceImpl implements UserService
{
    public void save(User user)
    { 
        userDao.save(user);
    }

    public User update(user)
    {
        userDao.update(user);
    }
}

Following is my UserTest class

@Test
class UserTest
{
    ?
}

What is good way to use HSQL to test save and update methods in UserService using TestNG with DataProvider? Please let me know if need some more information regarding question ;)

Your response will be greatly appreciated!!


Solution

  • If you just want to test if the dao is called correctly, mock it with a mocking framework (i'd choose Mockito) and just verify that the correct methods were called by your Service. This is more "unit" like, since you tests reflect the clear separation of dao and service.

    If you are interested in real DB communication to create/load instances and so on, you can use an in memory database like h2 and let your dao run against that database. This becomes more of an integration test, but can be useful none the less.

    Either way, you would set up a test application context that cares about datasources and mocks.