0

I am trying to unit test this code block in a method in my service layer. Could anyone suggest what can (/should) I test here and how to test them (esp. the insert to db part). Any pointers (/example code/doc) will be very helpful.

if (element != null) {
    id = iplDAO.loadGames(element, batchVO.getId());
    iplPartyDetailsVO = element.getParty();
    if iplPartyDetailsVO != null) {
    try {
        iplDAO.insertPartyDetails(iplPartyDetailsVO, id, batchVO.getId());
    } catch (Exception e) {

        logger.logp("className");
        String err = "blah";
        iplDAO.insertIntoError(err_t);
    }
}
Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
user2666282
  • 381
  • 1
  • 2
  • 15
  • Do you own the DAO code or is it proprietary? – Compass Jul 26 '16 at 14:59
  • 1
    Can you share how the `iplDAO` is created/initialized? – Mureinik Jul 26 '16 at 15:01
  • @Compass Yes we own DAO code - its created as below: 'public class IPLDAO extends BaseDAO implements TTDAO { At-Autowired private ITXXDAO txxDao; At-Override public int loadGames(..) { return id; } At-Override public boolean insertPartyDetails(a, b, c) { return x; } }' And in the service class its Autowired – user2666282 Jul 26 '16 at 16:45

1 Answers1

0

I think you might be confusing the testing with the mocking, if you are trying to write tests for this service layer code then you need to decide what are your expectations, for example:

  • Load some games from a data source
  • Save something (it's not clear in your code what you are actually saving but writing some unit tests might make this more obvious).

So if these are your expectations for your code then you would need to write a test that can verify that they have been met.

This means mocking the dependency that you are calling (in your case the iplDAO object) so that each method call returns something that can be used to test your functionality - in your case these two calls:

 iplDAO.loadGames(element,batchVO.getId())

and

iplDAO.insertPartyDetails(iplPartyDetailsVO, id,batchVO.getId());

There are lots of Java mocking libraries to choose from and they are all well documented but this blog post is a good starting point.

Community
  • 1
  • 1
tchambers
  • 456
  • 3
  • 5
  • Thanks - so you recommend testing those to dao calls? now my questions is how do I test iplDAO.insertPartyDetails(iplPartyDetailsVO, id,batchVO.getId()); without an actual insert? (Can a db mock help? if so can you please point me to an example) – user2666282 Jul 26 '16 at 16:51
  • @user2666282 By using a mock iplDAO object in your class (for example by using Mockito library) you are replacing what the 'real' iplDAO object normally does (presumably the read and insert to the DB). So there will be no interaction with the database. If you post the whole class then it will be easier to give an example. – tchambers Jul 27 '16 at 08:38