5

Is it possible to create an entirely new pattern on each case?

 3 years ago
source link: https://www.codesd.com/item/is-it-possible-to-create-an-entirely-new-pattern-on-each-case.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Is it possible to create an entirely new pattern on each case?

advertisements

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.


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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK