18

I use DbUnit for unit-testing of my DAO objects. It works great so far.

I have a problem, I have field ob type byte[] which is stored as BLOB in the database. The column is not-null. How can I specify the value for this column in the XML dataset file, that DbUnit uses? The value can be nothing fancy, 5 bytes will be enough. I would like to avoid necessity to create extra binary files just for this.

Any suggestions?

Ula Krukar
  • 12,549
  • 20
  • 51
  • 65

1 Answers1

26

After all I solved it like that:

XML dataset file:

<?xml version="1.0" encoding="UTF-8"?>
<dataset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
    <!-- image_content is string '12345' Base64 encoded -->
    <IMAGE IMAGE_ID="1" IMAGE_CONTENT="MTIzNDU="/>
</dataset>

DbUnit has built-in support for Base64 encoded data, it transformes correctly into byte array.

Test case code:

assertEquals("12345".getBytes(), image.getContent());
Ula Krukar
  • 12,549
  • 20
  • 51
  • 65
  • 1
    This works with NDbUnit as well if you define it in your XML data file: `MjBxdxxY7NbME2Ha6DKhepVpwio=` – Evan M Apr 14 '11 at 21:24
  • For example, base64 encoding in Postgres: `select encode('12345', 'base64');` – Markus Pscheidt Feb 01 '16 at 14:47
  • Just a heads up: when converting between strings and bytes and vica versa, such as in the assertEquals method example in this answer, always specify a charset to use. I just had a unit test that worked in my IDE (which defaulted to UTF-8) but failed when building with Maven (which used the windows default encoding, resulting in a string to not match) – user1884155 Nov 20 '18 at 19:38