1

Is there a way to create entirely new schema on every case? Using @DatabaseTearDown annotation is not the case here because i need to reset id generators as some of my test expectation rely on them (maybe it's a bad practice)

Update on rearrange my tests:

In one of my expected datasets i have:

<field oid="1" type="enumerated" name="simple enum field"
     dict_oid="1" required="0"
     level="1"/>

<field_enum_element field_oid="1"></field_enum_element>
<field_enum_element field_oid="1"></field_enum_element>
<field_enum_element field_oid="1"></field_enum_element>

where oid in term table is the generated id. I want to be sure that there are 3 rows were created in field_enum_element table but if i omit generated ids from expected data set as follows:

<field type="enumerated" name="simple enum field"
     dict_oid="1" required="0"
     level="1"/>

<field_enum_element></field_enum_element>
<field_enum_element></field_enum_element>
<field_enum_element></field_enum_element>

spring-test-db-unit thinks that there are 0 rows in the table

UPDATE:

  @Test
  @DatabaseSetup(value = "fieldServiceImplTest/testCreateEnumField.xml")
  @ExpectedDatabase(value = "fieldServiceImplTest/testCreateEnumField.expected.xml",
      assertionMode = DatabaseAssertionMode.NON_STRICT)
  @DatabaseTearDown(value = "fieldServiceImplTest/clear.xml", type = DELETE_ALL)
  public void testCreateEnumField() {
    FieldDTO fieldDTO = new FieldDTO();
    fieldDTO.setName("simple enum field");
    fieldDTO.setType("enumerated");
    fieldDTO.setLevel("term");
    fieldDTO.setIsValueRequired(false);
    fieldDTO.setDictionaryId(dictionaryService.findByOid(1L).get().returnIdentity());

    List<ItemDTO> itemDTOs = Arrays.asList(new ItemDTO(null, "complete"), new ItemDTO(null, "draft"), new ItemDTO(null, "deleted"));
    fieldDTO.setItems(new HashSet<>(itemDTOs));

    fieldService.createField(fieldDTO);
  }

testCreateEnumField.xml

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
  <dictionary changed="2014-01-31 18:11:54" oid="1" client_oid="1" descr="descr" name="dictionary"/>

</dataset>

testCreateEnumField.expected.xml

<?xml version="1.0" encoding="UTF-8"?>
<dataset reset_sequences="hibernate_sequence">
  <dictionary changed="2014-01-31 18:11:54" oid="1" client_oid="1" descr="descr" name="dictionary"/>
  <field client_oid="1" oid="1" type="enumerated" name="simple enum field"
         dict_oid="1" required="0"
         level="1"></field>

  <enum_element oid="11" client_oid="1" value="deleted"></enum_element>
  <enum_element oid="12" client_oid="1" value="draft"></enum_element>
  <enum_element oid="13" client_oid="1" value="complete"></enum_element>

  <field_enum_element field_oid="1"></field_enum_element>
  <field_enum_element field_oid="1"></field_enum_element>
  <field_enum_element field_oid="1"></field_enum_element>

</dataset>

Ideally i would like to be able to drop sequence between tests and test cases.

stanislav.chetvertkov
  • 1,620
  • 3
  • 13
  • 24

2 Answers2

1

Although I don't exactly know what your use case is, it sounds like it's a good fit for trying the new declarative SQL feature in Spring 4.1 RC1. Here is the JIRA issue that describes what this new feature.

Your code would look something like:

@Test
@Sql("fix-sequence.sql")
public void test() {
   //whatever
}

You can find the Javadoc of @Sql here.

Inside fix-sequence.sql you would provide the SQL needed for resetting the db for the tests

geoand
  • 60,071
  • 24
  • 172
  • 190
  • Thanks, i probably will use this feature when 4.1 Release will arrive. As for now i think i'll get by using injection of @PersistenceContext into my tests and executing drop sequence query from it. – stanislav.chetvertkov Jul 22 '14 at 07:15
  • @stanislav.chetvertkov Your welcome! I would suggest that you give the feature a try since Spring 4.1 is in RC1, and when the Release is available you can actually migrate your tests to it. It seems like the feature will save you a lot of ceremony code. – geoand Jul 22 '14 at 07:17
  • although it solves the problem it is still a workaround. Ideally i want to give dbunit a hint on number of rows in the result set. – stanislav.chetvertkov Jul 22 '14 at 07:19
  • I see your point. I haven't used dbunit before, so I can't give you any pointers in that direction – geoand Jul 22 '14 at 07:22
0

I was able to manage the problem by doing a little workaround by dropping sequence table in @Before method with simple native sql query: "drop table hibernate_sequence".

Actually, the better solution is just to include <hibernate_sequence next_val="1"/> into test dataset at @DatabaseSetup phase

stanislav.chetvertkov
  • 1,620
  • 3
  • 13
  • 24