-1

I have this service that I need to create Unit test for:

            SomeObject obj = new SomeObject();

            String objConfig = "<root id=%id% param1="something">....</root>"

            obj.setField1("value1");
            obj.setField2("value2");

            someObjectDao.create(obj);

            Long objId = obj.getId();
            obj.setConfig(objConfig.replace("%id%", objId.toString()));
            someObjectDao.update(obj);

Now, the problem is that in a real case, the DAO create will assign ID on the object, but how do I set the object ID through a Unit test?

Xstian
  • 8,184
  • 10
  • 42
  • 72

1 Answers1

0

You can do this in two ways (if you are using JUnit 4):

  1. Use real DAO and mark test class as @Transactional and @Rollback. Every test method will be opened in separate transaction, record will be inserted into database and processed as in real system, but after method exit rollback will occure.

  2. Use some mocking framework, i.e. mockito, mock create() method of someObjectDao, setting id as you want.

mdziob
  • 1,116
  • 13
  • 26