2

I'm having this problem with DBUnit causing a SQL insert error. Say I have this in my dbunit testdata.xml file:

<myschema.mytable id="1" value1="blah" value2="foo" />

I have a table like this (postgres)

myschema.mytable has an id, value1, value2, and a date field, say "lastmodified." The lastmodified column is timestamp with modifiers "not null default now()"

It appears that dbunit reads the table metadata and attempts to insert nulls for any column that isn't specified in my testdata.xml file. So the above xml results in an insert like this:

insert into myschema.mytable (id,value1,value2,lastmodified) values (1,'blah','foo',null) 

When running tests (dbunit/maven plugin) I get an error like this:

Error executing database operation: REFRESH: org.postgresql.util.PSQLException: ERROR: null value in column "lastmodified" violates not-null constraint

Is there some way to tell DBUnit to NOT INSERT null values on fields that I don't specify?

Edit: Using dbunit 2.5.3, junit 4.12, postgressql driver 9.4.1208

MTR
  • 1,411
  • 4
  • 14
  • 23

1 Answers1

1

Use the dbUnit "exclude column" feature: How to exclude some table columns at runtime?

The FilteredTableMetaData class introduced in DbUnit 2.1 can be used in combination with the IColumnFilter interface to decide the inclusion or exclusion of table columns at runtime.

FilteredTableMetaData metaData = new FilteredTableMetaData(originalTable.getTableMetaData(), new MyColumnFilter());
ITable filteredTable = new CompositeTable(metaData, originalTable);
Danilo Piazzalunga
  • 7,590
  • 5
  • 49
  • 75
Jeff
  • 956
  • 8
  • 10