0

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

fredt
  • 24,044
  • 3
  • 40
  • 61
Prashant Shilimkar
  • 8,402
  • 13
  • 54
  • 89

2 Answers2

2

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.

Jan Galinski
  • 11,768
  • 8
  • 54
  • 77
0

Acolyte provides a JDBC driver & tools, designed for such purposes (mock up, testing, ...): https://github.com/cchantep/acolyte

It's already used in several open source projects (Anorm, Youtube Vitess, ...), either in vanilla Java, or using its Scala DSL:

handler = handleStatement.withQueryDetection(...).
  withQueryHandler(/* which result for which query */).
  withUpdateHandler(/* which result for which update */).

// Register prepared handler with expected ID 'my-unique-id'
acolyte.Driver.register("my-unique-id", handler);
// then ...
Connection con = DriverManager.getConnection(jdbcUrl);
// ... Connection |con| is managed through |handler|

Note: I am the author of Acolyte.

cchantep
  • 9,118
  • 3
  • 30
  • 41